Manually expire low level cache

caching, ruby-on-rails-3

Solution

You can manually expire the cache using the `.delete` method:

Rails.cache.delete("recent_news")

Problem

I'm using the following low-level caching for the five most recent news articles in my 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 keep this query cached until a new news article is created? I was thinking of manually expiring the cache using an observer, but wasn't sure if there was a way to actually do this, e.g: ``` class NewsObserver < ActiveRecord::Observer def after_create #expire recent_news cache end end ```

Original source