Project

cached_at

0.0
No commit activity in last 3 years
No release in over 3 years
Use cached_at for ActiveRecord cache key
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

 Project Readme

CachedAt

Build Status Code Climate

Use cached_at for ActiveRecord cache key instead of updated at

Installation

Add this line to your application's Gemfile:

gem 'cached_at'

And then execute:

$ bundle

Or install it yourself as:

$ gem install cached_at

Usage

Too add to any class, just include CachedAt

class User
  include CachedAt
end

You will need to add the cached_at column to the table

rails generate migration add_cached_at_to_users cached_at:datetime

The cache_at column will be update when a record is created or updated.

user = User.create # sets the cached_at
user.save          # updates cached_at if another attribute on the record has changed

When a records associations are created/updated, the cached_at column will be updated but the updated_at will not change.

class Post
  belongs_to :user, touch: true
end

post = Post.create user: user # updates the user cached_at

Gem also provides class method cache_key for your model, that will generate cache key string from maximum(:cached_at)

Post.cache_key
# SELECT MAX(`posts`.`cached_at`) AS max_id FROM `posts`
# => "Post-1377768216"

This way you can pass your model class to Rails.cache.fetch as an argument to monitor when any record was changed.

Rails.cache.fetch [Post, 'last_posts'] do
  Post.last_posts
end
# => Cache fetch_hit: Post-1377768216/last_posts

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request