我使用 Rails 3 和 Memcached 来缓存一些模型。当模型更改时,我想使该记录的缓存失效。对于视图片段,我只是说expire_fragment("blah")
。如何对我的模型执行此操作?我不想说Rails.cache.clear
并失去一切。我想要类似的东西Rails.cache.invalidate("/users/5")
。我怎么做?
您没有提到模型实际上在什么时候添加到缓存中。您可以尝试使用以下方法使模型缓存无效after_save
hook.
class Model < AR::Base
after_save :invalidate_cache
private
def invalidate_cache
Rails.cache.delete("/users/#{self.id}")
return true # recommended to return true, as Rails.cache.delete will return false if no cache is found and break the callback chain.
end
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)