Rails.cache.read在rails中返回nil,但在控制台中不返回nil [英] Rails.cache.read returns nil in rails, but not in console

查看:83
本文介绍了Rails.cache.read在rails中返回nil,但在控制台中不返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过其api密钥缓存和读取用户对象.用法可以很好地缓存,我可以在rails控制台中读取缓存,但是出于任何原因,在rails应用程序中执行相同的精确Rails.cache.read总是返回nil.

I'm attempting to cache and read a user object via its api key. The use gets cached fine, and I can read the cache in the rails console, but for whatever reason doing the same exact Rails.cache.read in the rails app always returns nil.

这里有一个我在做什么的例子.这是在before_filter函数中.

Heres an example of what I'm doing. This is in a before_filter function.

def authKey
  #?api=ce6f95a8bf7f9861330ede58f8972981
  key = params[:api]
  cu = Rails.cache.read(key)

  #<do some logic>
  logger.debug("CACHING USER #{key}")
  Rails.cache.write(key, user)

cu始终为nil,但是该对象将存在于内存缓存中.还有其他人遇到这种问题吗?我正在使用启用压缩的dalli gem.

The cu will always be nil, but the object will exist in memcache. Has anyone else run into this sort of problem? I'm using the dalli gem with compression enabled.

推荐答案

我认为,默认情况下,Rails缓存在开发中处于关闭状态,在测试和生产中处于打开状态.检入config/environments/development.rb并查看config.cache_classes设置为...

I believe that rails caching is off by default for development and on in test and production. Check in config/environments/development.rb and see what config.cache_classes is set to...

这篇关于Rails.cache.read在rails中返回nil,但在控制台中不返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆