Reputation: 8730
Using fragment cache digest to cache data. Issue I face is that when the db records changes cache key should be regenerate but it not refreshing and showing the old data.
I get data in json format.
cache "home_index_makes_listing_12_grid",:expires_in => 24.hours do
yield
end
while when I change or add any new html elements then digest key changes and cache refresh. but changes from backend not changes anything.
How to fix that issue?
Upvotes: 0
Views: 228
Reputation: 6841
Cache key doesn't automagically change when the record gets updated in the DB. There is no way for cache to somehow KNOW that the values in DB have changed.
Let's consider the below example to understand how cache key gets updated:
cache "home_index_makes_listing_#{user.id}_#{user.updated_at}_grid", expires_in: 24.hours do
...
end
The key gets updated whenever the user
object gets updated. How it works:
Notice that the dynamic part in our key are user.id
and user.updated_at
. So, the key gets updated (and cache gets busted) only when updated_at
of this particular user is changed.
When the user requests this page (via browser or anything else), Rails tries to render this code fragment.
config.cache_store
. For development env, it is usually set to file
(saved in /tmp). For production, you should use some caching server like Redis or Memcached.Hope this helps.
Upvotes: 1