Reputation: 96897
Say I want to use the dalli store for caching (fragments of) views. Does this mean that doing something like this will also use memcache for possible DB caching?
Rails.cache.fetch("something") { smth }
Also, if I do something like:
Author.all
the Rails console will show me that it's querying the database, but if I run Author.all
again, it will show me that the results are retrieved from cache. When do I want to explicitly use Rails.cache
and when should I rely on ActiveRecord to do the caching?
Upvotes: 0
Views: 731
Reputation: 28245
Fetch and read operations get items from the
cache store which is currently configured,
for example from the file system if the cache
store is :file_store
, or from the Memcached
server if the store is :mem_cache_store
.
Therefore if you want to use Memcached for
fragment caching, you have to configure the
cache_store accordingly:
ActionController::Base.cache_store = :mem_cache_store, "cache-1.example.com"
It is recommendable to use fragment caching if one has large, complex views involving many queries which change rarely or slowly. Fragement caching is a good trade-off between completely static pages (fast, but fixed) and dynamic pages (slow, but variable). If you need to cache a certain section of a page instead of the entire page, fragment caching is the way to go, as Ryan Bates said in his Railscast about fragment caching.
Page and action caching are even better, they are great for speeding up the performance of a page, but problematic if it contains user-specific content. In this case it is possible to use dynamic page caching. Stackoverflow uses a similar technique.
SQL Caching persists only for the duration of a single action.
Upvotes: 1