user545139
user545139

Reputation: 935

Rails 3.1 wildcard expire cache for action with query string

I have a page where on the where the index action shows a list of Posts, with custom sort columns, pagination, etc. Although I can cache every individual page / sort option with

cache(:direction => params[:direction], :sort => params[:sort], :page => params[:page]) do

I can't expire all of these at once using a single call to expire_action (which is a problem). I know expire_action has a regex option, but that is messy (using a regex to search for keys created with a hash), and I am using memcached which will not work.

How can I expire all the cache members of an action with a single call to expire_action? If this is not possible, are there any other caching options you could recommend?

Upvotes: 3

Views: 749

Answers (1)

joshhepworth
joshhepworth

Reputation: 3066

I would recommend looking through this post on caching in Rails, it's a tremendously thorough post that goes through various strategies that may provide the outcome that you're looking for in this situation.

Though he doesn't mention it in the post, adding some sort of cache busting parameter (like a cache version id) to the arguments list might provide you with a way to expire the cache in a more universal way. For example:

cache(:version => Posts.cache_version, :direction => params[:direction], :sort => params[:sort], :page => params[:page]) do

# Later on to bust the cache
Posts.cache_version = 2

The details of how you would want to implement cache_version and cache_version= could vary depending on how you're handling other data in the application. There may also be a more elegant solution than this, but it's what came to mind.

Upvotes: 1

Related Questions