Rails 3 Activerecord 上的缓存未命中
我遇到这样的情况:对缓存的 ActiveRecord 对象的每个请求都会导致缓存未命中。我试图缓存当前用户的权限结构:
my_permissions = Rails.cache.fetch("#{self.id}.permissions") do
self.permissions.all
end
我发现每个页面请求都在数据库中查询我的权限。如果我将代码更改为任意内容,
my_permissions = Rails.cache.fetch("#{self.id}.permissions") do
"I have no permissions"
end
这将正确地存在于缓存中。我可以证明这一点,因为如果我将任意行更改回 ActiveRecord 调用,由于缓存对象错误,它将不断出错。我还检查了创建的缓存文件,它正确地存储为两个值,我可以看到看起来像我的权限对象的内容,但它继续访问数据库而不是缓存。我缺少什么?
I'm encountering a situation where every request to a cached ActiveRecord object causes a cache miss. I am attempting to cache the permission structure of the current user:
my_permissions = Rails.cache.fetch("#{self.id}.permissions") do
self.permissions.all
end
I find that every page request is querying the db for my permissions. If I change the code to something arbitrary
my_permissions = Rails.cache.fetch("#{self.id}.permissions") do
"I have no permissions"
end
This will correctly live in the cache. I can prove this because if I change the arbitrary line back to my ActiveRecord call, it will keep erroring out due to the cache object being wrong. I have also checked the cache file created and it is correctly being stored as both values, I can see what looks like my permission object, yet it continues to hit the database and not the cache. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论