开发者

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

开发者 https://www.devze.com 2023-03-16 05:36 出处:网络
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

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开发者_StackOverflow中文版 this sort of problem? I'm using the dalli gem with compression enabled.


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


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


In dev mode you can toggle caching by running rails dev:cache

0

精彩评论

暂无评论...
验证码 换一张
取 消