I've just started working with ActiveResource, and decided to cache a few bits of the model so I'm not hitting the api incessantly. Ok, fine.
I've looked into expiring caches, and decided to implement a sweeper (which I've not had to do yet). This isn't working.
AR model:
class Myresource < ActiveResource::Base
extend ActiveModel::Callbacks
define_model_callbacks :update
"stuff"
def current
Rails.cache.fetch("/key/#{self.id}", :expires_in => 5.minutes) do
Myresource.find(ID)
end
end
end
Sweeper:
class MyresourceSweeper < ActionController::Caching::Sweeper
observe Myresource
def after_update(myresource)
expire_cache_for_myresource
end
private
def expire_cache_for_myresource
Rails.cache.delete '/key/myresource.id'
end
end
Controller:
cache_sweeper :myresource_sweeper
So having worked with AR and caches and Sweepers only a bit, I can't figure out where to look after trying various combos of things. I can set and expire from the console for a resource, but within the app, the cache gets set, but nothing I've done is triggering a delete.
Suggestions?
The code that you posted has a typo. I kinda doubt this is your real problem, but for what it's worth, I think you meant to write your sweeper as follows:
class MyresourceSweeper < ActionController::Caching::Sweeper
observe Myresource
def after_update(myresource)
expire_cache_for_myresource(myresource)
end
private
def expire_cache_for_myresource(myresource)
Rails.cache.delete "/key/#{myresource.id}"
end
end
E.g. in the original code you posted, you weren't passing the resource to the expire_cache_for_my_resource method, and therefore were expiring the same static key over and over again.