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.
Related
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)
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.
I want to remove cache from my code. My code is in Ruby on rails. The issue is that i have updated my design but it is displaying the older one.
I tried this Rails.cache.clear command but its not working .
It is giving me this error 'Rails.cache.clear' command not found.
Rails.cache.clear needs to be ran from the Rails console.
You need to go to ./config/environments/production.rb or development.rb depending where do you launch your code, and change settings to reflect this :
# Code is not reloaded between requests
config.cache_classes = false
and if necessary, comment out your cache store line :
# Use a different cache store in production
config.cache_store = :dalli_store
Ideally the first snippet should be enaught.
For an AR query like this:
#users = User.find(some_conditions_here)
then #users is an AR array and I want to cache this.
If I do, in one controller call, a
Rails.cache.write('foo',#users)
it doesn't complain or error out and I can even see a 'foo' under /tmp/cache with a non-zero size but a subsequent controller call to
Rails.cache.read('foo')
returns a nil. When I do both the write and read from a Rails console, it works as expected. What is it about doing it via a controller that causes this problem?
This used to work before in Rails 2... what am I missing?
Check that config.action_controller.perform_caching is set to true.
To quote the Rails caching guide,
caching is disabled by default for development and test, and enabled for production
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