Rails.cache.fetch returns nil

2.7k Views Asked by At

I am trying to implement cache in my rails 4 app. I installed redis, setup config according this article but every time I try to cache something in my controller, on first refresh I get data, on second I get nil. When I go to redis cli and I do KEYS * I see that key is there.

I thought it is my problem of redis so I disabled redis and in development.rb put just

config.cache_store = :memory_store, { size: 128.megabytes }

but if I try to

Rails.cache.fetch("dashboard") do
   User.where(:active=>true).size
end

I get nil as response to my action.

What am I doing wrong that cache returns me always nil ?

Edit: this is part of my development.rb file for redis

config.cache_store = :redis_store, {
  expires_in: 1.hour,
  namespace: 'cache',
  redis: { host: 'localhost', port: 6379, db: 0 },
  }
1

There are 1 best solutions below

0
On

This is nothing but the cache is not getting stored. Hence, you have to check the configurations for it. For eg. in your environment file, you should have the following code snippet :

config.action_controller.perform_caching = true
config.cache_store = :media_store

:media_store - depends on where are you going to store your cache.