Rails.cache.read returns nil in rails, but not in console

柔情痞子 提交于 2019-12-13 08:11:20

问题


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.

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)

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.


回答1:


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...




回答2:


Caching is turned off by default for development environment. Adding 'config.action_controller.perform_caching = true' to config/environments/development.rb should fix the problem.

http://guides.rubyonrails.org/caching_with_rails.html#basic-caching



来源:https://stackoverflow.com/questions/6500142/rails-cache-read-returns-nil-in-rails-but-not-in-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!