当我跑步时:
IpPermission.blacklist.pluck(:ip)
我得到了结果:
=> ["127.0.0.11012q", "50.36.46.48"]
如果我手动更改ip_permissions表中的任何行,并再次运行IpPermission.blacklist.pluck(:ip)
。它显示更新的结果。它不应该从缓存加载结果而不是直接从db?
我的模型看起来像:
class IpPermission < ActiveRecord::Base
validates_presence_of :ip, :note, :category
validates_uniqueness_of :ip, :scope => [:category]
validates :category, :inclusion => { :in => ['whitelist', 'blacklist'] }
def self.whitelist
Rails.cache.fetch('whitelist', :expires_in => 1.month) { self.where(:category => 'whitelist') }
end
def self.blacklist
Rails.cache.fetch('blacklist', :expires_in => 1.month) { self.where(:category => 'blacklist') }
end
end
答案 0 :(得分:3)
我认为这应该可以解决我们的问题:
更改此
def self.whitelist
Rails.cache.fetch('whitelist', :expires_in => 1.month) { self.where(:category => 'whitelist') }
end
def self.blacklist
Rails.cache.fetch('blacklist', :expires_in => 1.month) { self.where(:category => 'blacklist') }
end
到这个
def self.whitelist
Rails.cache.fetch('whitelist', :expires_in => 1.month) { self.where(:category => 'whitelist').all }
end
def self.blacklist
Rails.cache.fetch('blacklist', :expires_in => 1.month) { self.where(:category => 'blacklist').all }
end
在哪里不进行实际查询,如果您放置.all
然后进行查询,那么您不存储数据,而只是存储查询