I read the official guide. It says there are page cache
, action cache
and fragment cache
, but they are not what I want.
I just like to cache an object, not the whole page or fragment of view, like this pseudocode:
def show
cache @ads, :expires_in => 1.hour do
@ads = Advertisement.all
do
end
Is it possible? with memcache
or redis
?
In order to use page and action caching you will need to add actionpack-page_caching and actionpack-action_caching to your Gemfile . By default, caching is only enabled in your production environment. You can play around with caching locally by running rails dev:cache , or by setting config. action_controller.
Sweepers are the terminators of the caching world and responsible for expiring caches when model objects change. They do this by being half-observers, half-filters and implementing callbacks for both roles.
clear will clear your app cache. In that case rake tmp:cache:clear will just try to remove files from "#{Rails. root}/tmp/cache" but probably won't actually do anything since nothing is probably being cached on the filesystem.
The default Rails cache (ActiveSupport::Cache MemoryStore) is thread-safe as of Rails version 3.1: http://api.rubyonrails.org/v3.1.0/files/activesupport/CHANGELOG.html As the CHANGELOG notes: "Make thread safe so that the default cache implementation used by Rails is thread safe."
Try this:
#To cache the object
Rails.cache.write('cache-key', object)
#Load the object from the cache
Rails.cache.read('cache-key')
Check out the lawnchair gem to cache objects in Redis.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With