Manually Finish Low Level Cache

I use the following low-level caching for the five latest news in a Rails application:

@recent_news = Rails.cache.fetch("recent_news", :expires_in => 1.hour) do News.order("created_at desc").limit(5) end 

Is there a way to cache this request until a new news article is created? I was thinking about manually expiring the cache using an observer, but was not sure if there was a way to do this, for example:

 class NewsObserver < ActiveRecord::Observer def after_create #expire recent_news cache end end 
+6
source share
1 answer

You can manually complete the caching using the .delete method:

 Rails.cache.delete("recent_news") 
+10
source

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


All Articles