我使用的是redis-rails。对于缓存键,我使用了一个数组:
Rails.cache.fetch([self.class.name, :translated_attribute, id, field, I18n.locale]) do
self.read_attribute field, locale: I18n.locale
end现在我需要删除与self.class.name,:translated_attribute,id匹配的所有缓存。我知道它有delete_matched,在key之后接受通配符(*)进行部分匹配。
但我不知道生成的确切密钥是什么。现在我需要知道当我们使用数组作为键时,它是如何生成键的。我的意思是,如果我使用:foo,:bar,:dum作为缓存键,那么缓存区中的确切键是什么?
发布于 2013-10-26 14:51:24
缺省的rails缓存键格式为: class/id-timestamp
我通常不使用rails默认的缓存键格式,而是创建自己的键,以便在redis中更容易操作。
cache_key = "#{self.class.name}/#{translated_attribute}/#{id}/#{field}/#{I18n.locale}"
Rails.cache.fetch(cache_key) do
self.read_attribute field, locale: I18n.locale
end
Rails.cache.delete(cache_key)
Rails.cache.delete_matched("#{self.class.name}*#{id}*")https://stackoverflow.com/questions/19603598
复制相似问题