Expiring all caches on a controller

I got a resourceful controller with a custom action. The action is pretty heavy, so I'm working on caching it:

class MyController < ApplicationController
  caches_action :walk_to_mordor

  # GET /my/:id/walk_to_mordor/:direction
  def walk_to_mordor
    # srz bzns
  end
end

It works very nice, caching is done and the page is now fast. However, I want to allow the user to "bust" the cache by clicking on a link on the page. At first I tried:

def bust_cache
  expire_action :action => :walk_to_mordor
end

Rails complained that no route matches my action. Might be because of the parameter. Hmm, let's give it to him:

def bust_cache
  MyEntities.all.each do |e|
    expire_action walk_to_mordor_path(e, ??)
  end
end

Problem, I can't possibly identify all choices of :direction.

Is there a way to clear all action caches that match a certain regular expression, or all action caches from a specific controller?

Answers


The secret is called expire_fragment:

expire_fragment(key, options = nil)

Removes fragments from the cache.

key can take one of three forms:

  • String - This would normally take the form of a path, like "pages/45/notes".

  • Hash - Treated as an implicit call to url_for, like {:controller => "pages", :action => "notes", :id => 45}

  • Regexp - Will remove any fragment that matches, so %r{pages/d*/notes} might remove all notes. Make sure you don’t use anchors in the regex (^ or $) because the actual filename matched looks like ./cache/filename/path.cache. Note: Regexp expiration is only supported on caches that can iterate over all keys (unlike memcached).

http://api.rubyonrails.org/classes/ActionController/Caching/Fragments.html#method-i-expire_fragment

Sadly, it won't work with memcached (if I ever decide to use it). Gotta be a lot more clever to avoid cache in that circunstance. Maybe adding a serial parameter to the request, and increment it when the user presses the 'bust cache' button...


Need Your Help

ASP.NET Gridview refresh with cascading dropdown controls

asp.net gridview drop-down-menu

I have two dropdowns and a gridview (lstDivisions, lstDistricts, gridIncidents) in an asp.net page and I use SqlDataSource ID statements in the ASPX page to retrieve the data from SQL Server for all

Windows Phone 8 - Generating Lockscreen Image in the Background

image background windows-phone-8 lockscreen

I'm trying to create a Windows Phone 8 app (update of my currently published "The Quote") that uses the new Windows Phone 8 Live Lockscreen API. I basically want to choose randomly a image background

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.