1

I have an Ruby on Rails application where fragment caching is used and using memcached for storing the data.Also i have a sweeper which expires the cache when changes are made to the model.

index.html.erb

<% cache 'recent_albums' do %>
contents to be cached
<%end %>


class AlbumsSweeper < ActionController::Caching::Sweeper
  observe Album

  def after_save(album)
    expire_cache(album)
  end

  def after_destroy(album)
    expire_cache(album)
  end

  def expire_cache(album)
    expire_fragment 'recent_albums'
  end

end

I have a requirement where before the user hits the Albums page the expired fragment needs to be refreshed with the new data. Can someone please help on how the fragment cache data refresh can be implemented?

user3428294
  • 77
  • 10

1 Answers1

0

The whole concept of caching is "do something and remember it for next time".

If I understand your questions correct; you're trying to do something else, i.e. "have something always prepared for when something is run" --- so you should probably use something else.

Memcached supports storing key:value pair of data - so if you want something to always be ready and "prebaked" you can skip using cache, and instead just store the key:values to Memcached. Then update it on after_save and after_destroy etc.

Other than that - the fragment cache will automatically renew itself when accessed and not in cache.