MethodCachable
Provides setting specific methods list that must be cached and clearing cache on CRUD operations.
Installation
Add this line to your application's Gemfile:
gem 'method_cachable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install method_cachable
Usage
Basic configuration
class Post < ActiveRecord::Base
include MethodCachable
belongs_to :menu_item
# Add this line for callbacks working
acts_as_cachable
# You want to cache association calling...
def menu_item_title
self.menu_item.title
end
# ...or maybe some evaluations
def some_evaluations
# There must be your code
end
# Specify list of cachable methods.
cached_methods :menu_item_title, :some_evaluations
end
PLEASE NOTE: after save/touch/destroy cache will expire automatically.
Cache storage
For storing cached methods gem using Rails.cache. Therefore you must set in rails application config file cache_store option. I highly recommend using the couple of memcached+dalli because memcached store information in memory and the best solution to integrate it to rails app is dalli. For this, you must add gem dalli to your Gemfile:
gem 'dalli'
And then configurate it in production.rb:
YourApp::Application.configure do
config.cache_store = :dalli_store, { namespace: :your_namespace, expires_in: 2.hours, compress: true }
# Rest of configuration
end
PLEASE NOTE: you don't need to configurate cache_store in development mode because for convenient development caching is off and method is called directly.
Future developing
Now you may to cache only instance methods but caching class methods gem doesn't allow. In future, I will implement this functionality.
Contributing
- Fork it
- 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 new Pull Request