rails how to delete cache key using partial match

前端 未结 1 646
死守一世寂寞
死守一世寂寞 2021-02-12 17:39

I am using redis-rails. For cache key I am using an array:

Rails.cache.fetch([self.class.name, :translated_attribute, id, field, I18n.locale]) do
  self.read_at         


        
1条回答
  •  长情又很酷
    2021-02-12 18:15

    The default rails cache key format is: [class]/[id]-[timestamp]

    i usually dont use rails default cache key format, instead i create my own keys so it would be easier to manipulate in 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}*")
    

    0 讨论(0)
提交回复
热议问题