Gem for caching in Wego Rails apps
Add this line to your application's Gemfile:
gem 'cacheable'
And then execute:
$ bundle
Or install it yourself as:
$ gem install cacheable
To include the caching feature for of the gem in your Rails apps models, create config/initializer/cacheable.rb with the following line:
ActiveRecord::Base.send(:include, Cacheable::ActiveRecordExtensions)
Cacheable::CacheVersion.namespace = 'application_name'
Cacheable::CacheVersion.init
In your model, add the functions that you want to cache the data
caches_method :method_name
caches_method :method_name, expires_in: 1.day #defalut expires_in is 1 day
caches_method :method_name, include_locale: true #default include_locale is false
In order to prevent too many request sent to caching storate (memcache) we will store cache in-memory by default. In-memory storage is only available per request. If you want to disable in-memory can set like this
caches_method :method_name, memoized: false
Need to use two separate define functions for instance and class methods (singeton methods)
class Test
caches_method :a_defined_method
caches_class_method :a_defined_class_method
def a_defined_method
#
end
def self.a_defined_class_method
#
end
end
caches_method :method_name
or caches_class_method :method_name
will only generate cached method with same name method_name
for caching and delete_method_name_cache
for clearing cache.
- Fork it ( https://github.com/[my-github-username]/cacheable/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