开发者

Expiring all caches on a controller

开发者 https://www.devze.com 2023-01-28 02:36 出处:网络
I got a resourceful controller with a custom action. The action is pretty heavy, so I\'m working on caching it:

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

Prob开发者_如何学运维lem, 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?


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...

0

精彩评论

暂无评论...
验证码 换一张
取 消