Cache ActiveRecord objects in memcached!
Add this line to your application's Gemfile:
gem 'cache-object'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cache-object
Add a Rails initializer, for instance at config/initializers/cache-object.rb
# Use the Rails cache.Cache::Object.configuredo |c|
c.cache=Rails.cacheend# Use an arbitrary Dalli connection in your environment.Cache::Object.configuredo |c|
hosts=Array(Settings.caching.memcached.hosts).clonehosts << {namespace: 'my.namespace',expires_in: 1.day,compress: true,keepalive: true,socket_timeout: Settings.caching.memcached.socket_timeout}c.cache=ActiveSupport::Cache::DalliStore.new(hosts)endInclude the Cache::Object::ActiveRecord module into your model. When a record is saved, its attributes will be
marshalled into the cache.
classUser < ActiveRecord::BaseincludeCache::Object::ActiveRecordendBy default the record is cached by the primary id. If another key should be used, object_cache_on can be used:
classUser < ActiveRecord::BaseincludeCache::Object::ActiveRecordobject_cache_on:usernameendIf instance variables that are not ActiveRecord attributes need to be cached, object_cache_include can be used:
classUser < ActiveRecord::BaseincludeCache::Object::ActiveRecordobject_cache_include:shoe_sizeattr_accessor:shoe_sizeend- Fork it ( https://github.com/[my-github-username]/cache-object/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request