Rails Cache Digest and Jbuilder

I am using Jbuilder to output the JSON API, and now I want to add caching using the cache_digest cache for Rails 3.2.13.

It works fine, the cache template is created, and it is read from the cache, but the problem is that if I change the model record, for example, change the "header", it does not expire the cache, and it still shows the old header.

This is my jbuilder template index:

json.cache! "news" do |json| json.array!(@news) do |news| json.id news.id json.title news.title json.excerpt news.excerpt json.content strip_links news.content json.image news.image json.source news.source json.published_at news.published_at json.created_at news.created_at end end 

I am changing attributes through the RailsAdmin interface.

+4
source share
1 answer

The solution was to pass the @ news collection instead of the "news" line as a cache key, for example:

 json.cache! @news do |json| json.array!(@news) do |news| json.id news.id json.title news.title json.excerpt news.excerpt json.content strip_links news.content json.image news.image json.source news.source json.published_at news.published_at json.created_at news.created_at end end 

When I first tried this, I got the error message "Filename too long" when he tried to create a cache file on disk. This was because my @news collection was too large (too many objects), so I changed it to return fewer objects. This will not be a problem for things like memcached, but when saved to disk, the length of the file name is limited by the operating system.

+4
source

Source: https://habr.com/ru/post/1482195/


All Articles