Rails.cache.clear returns nil - ruby-on-rails

I have this setup
config.cache_store = :redis_store, ENV['REDIS_CACHE_URL']
$ redis-cli
127.0.0.1:6379> set random_key 1
OK
Now I go to the console and do Rails.cache.clear which returns nil
And I am still able to access the key random_key in the redis-cli. It did not clear the cache.
I could not read what Rails.cache returns here too ruby/2.3.4/lib/ruby/gems/2.3.0/gems/railties-4.2.8/lib/rails.rb
Is Rails.cache.clear is supposed to return true?
Can someone please help me out if my understanding is wrong?

redis-cache stores data under a particular namespace.
For example, if you've configured redis-store according to Documentation, then cache keys will be stored under cache namespace. That means, that when you Rails.cache.write("random_key", "key") a key cache:random_key will appear in the Redis. Therefore, when you Rails.cache.clear, only keys under cache namespace will be deleted.
Hence, if you manually create random_key in Redis, Rails.cache.clear won't remove it. But if you manually create cache:random_key, it will.

Be careful when using Rails.cache.clear it will invalidate all the keys for the application (source)
[~Not sure if this is the best place for this answer~]
This helpful article was a great way for me to understand caching when changing versions of Rails from 5.1+ to Rails 6.1+. The article talks about options for generating a cache key with or without versioning.
In the instance of my application, versioning was needed but not turned on when upgraded to Rails 6.1:
#in application.rb
config.active_record.collection_cache_versioning = true
Then within the application code where object.cache_key is called, I had to change it to object.cache_key_with_version (source)

Related

Way to examine contents of Rails cache?

I'm trying to debug stale entries in a cached view in a Rails (5.0.0.beta2) running on Heroku. I'd like to look at the entries in the cache to confirm that they are named the way that I expect and are getting expired when they should.
Is there any way to do this? I found this question, HOW do i see content of rails cache, which suggests Rails.cache.read("your_key"). So, using bin/rails c (on Heroku) I tried:
Rails.cache.read(User.find(19).cache_key) => nil
Where 19 is the :id of one of the users for whom I'm seeing stale data. This has me kind of stumped...
If I try:
User.find(19).cache_key => "users/19-20160316151228266421"
But when a cache entry is supposedly expired the log line looks like:
Expire fragment views/users/19-20160316151228266421 (0.2ms)
So I tried doing a Rails.cache.read on that path, this also returned nil – I also tried doing the same with a user that had not be expired, and got nil again.
I'm wondering if that difference in path signals a problem, or if there is a way to see the path of the key that is created (I've been assuming that it matches at least the part after the slash).
Cache has the following instance variables:
[:#options, :#data, :#key_access, :#max_size, :#max_prune_time, :#cache_size, :#monitor, :#pruning]
You can examine the data with:
Rails.cache.instance_variable_get(:#data)

Access the model in production.rb rails 3

I have a model called SystemSettings with a name on and a value. It is where I store the majority of my configuration for my app. I need to be able to access it in my production.rb inside my rails 3.2 app. How would you go about doing this?
Since the Rails config such as production.rbis read before ActiveRecord is initialised you would need to use a callback:
Rails.application.configure do
ActiveSupport.on_load(:active_record) do
config.custom_variable = SystemSettings.find_by(name: "Foo").value
end
end
But since the callback executes later when ActiveRecord is ready you can't immediately use its value which is why your approach may be flawed due to race conditions.
Unless you are building something like a CMS where you need to provide a user interface to edit system settings you will be better off using environmental variables. They are immediately available from memory and do not have the overhead of a database query.
http://guides.rubyonrails.org/v3.2.9/initialization.html

Rails query caching is not working

I use redis_store for caching queries.In development log i couldn't see the cached queries,It again hits the database and gets the records,But in my redis console i can see the keys which i have used to cache the queries.I tested in rails console to read the queries using keys.I could get the cached queries.Help me to solve this.
In my model
Category = Rails.cache.fetch("category") {Category.select(:foo).unique}
In redis
redis 127.0.0.1:6379> KEYS *
"category"
In rails console
Rails.cache.read("category")
Temporarily add this line to /config/environments/development.rb
config.action_controller.perform_caching = true
Don't forget to delete it when you're done!
You shouldn't cache in development, so what #megas says is true, you should rather have a test to check if caching works as it should and in your test environment you could change your config.action_controller.perform_caching to true.

Ruby on rails : memcached read value is nil

I've inherited a Rails project that looks like it's using memcached.
I have a controller, in one method I'm storing a value in the cache with:
Rails.cache.write("key", #var)
in another method subsequently called in the same controller I read it back with:
#var = Rails.cache.read("key")
and the variable is nil. Why is caching not working?
I've just started using ROR so there's a good chance it's something pretty basic.
Just a couple of things to check:
Have you enabled caching in your development environment?
You didn't say which version of RoR you're using, however cache is not enabled by default on development environment.
To enable it, set config.action_controller.perform_caching = true in config/environments/development.rb file.
Have you started memcached server?
If not, start it with memcached -vv
If your memcached server is down when you try to write into cache, the call to Rails.cache.write will return false and the subsequent call to Rails.cache.read will therefore return nil.

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

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

Resources