ruby-on-rails-3action-caching

rails3 caching: expire_action with parameters cached by caches_action(GET)


I'm working on a search feature that when typing something make an ajax call to get the results. I wanna caches the search action with search keywords in memory_cache, so i make it like this:

# caches_action with GET parameters

caches_action :search_posts, :cache_path => Proc.new { |c| c.params }

My question is how can i expire the action with parameters too? Is there another way to make my feature works?


Solution

  • You have several options to expire this kind of cache.

    First one (simpler one) is to add :expires_in option to your caches_action statement - for example:

    caches_action :search_posts, :cache_path => Proc.new { |c| c.params }, :expires_in => 16.hours.to_i
    

    This will automatically expire this key after some time.

    Or you can expire this cache using expire_action method from controller. It should be like:

    expire_action :action => 'posts', :q => 'query'
    

    given you have requested this page with one parameter q=query .

    You can read more in the official documentation .