removing cache from command line in ruby on rails - ruby-on-rails

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.

Related

Rails.cache.clear returns nil

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)

Joining separate log to main Rails development log

This is the reverse of the question I have seen several times elsewhere, in which someone wants to see how to create an another, separate Rails log from the main development log. For some reason, my Rails app is logging my DelayedJob gem's activity to a separate log (delayed_job.log), but I want it to log to the main development.log file. I am using the Workless gem and NewRelic as well, should this be potentially relevant (although I experimented on this by removing NewRelic, and the issue still remained).
I'm not clear on how this happened. However, I was having some trouble earlier with seeing SQL insertions and deletions in my log, and another user kindly suggested that I use the following in an initializer file:
if defined?(Rails) && !Rails.env.nil?
logger = Logger.new(STDOUT)
ActiveRecord::Base.logger = logger
ActiveResource::Base.logger = logger
end
Once I did this, I saw the SQL statements, but no longer saw the DelayedJob information in the main development log.
So my question is: How can I make sure that DelayedJob activity logs to the main development log? I don't mind if it also logs to a separate log, but the important thing is that I see its activity in my Mac's console.
Please let me know if you'd like more code from my app - I'd be happy to provide it. Much thanks from a Rails newbie.
Try adding the following line to config/initializers/delayed_job_config.rb
Delayed::Worker.logger = Logger.new(STDOUT)
I finally got this to work. All thanks to Seamus Abshere's answer to the question here. I put what he posted below in an initializer file. This got delayed_job to log to my development.rb file (huzzah!).
However, delayed_job still isn't logging into my console (for reasons I still don't understand). I solved that by opening a new console tab and entering tail -f log/development.log.
Different from what Seamus wrote, though, auto-flushing=true is deprecated in Rails 4 and my Heroku app crashed. I resolved this by removing it from my initializer file and placing it in my environments/development.rb file as config.autoflush_log = true. However, I found that neither of the two types of flushing were necessary to make this work.
Here is his code (without the auto-flushing):
file_handle = File.open("log/#{Rails.env}_delayed_jobs.log", (File::WRONLY | File::APPEND | File::CREAT))
# Be paranoid about syncing
file_handle.sync = true
# Hack the existing Rails.logger object to use our new file handle
Rails.logger.instance_variable_set :#log, file_handle
# Calls to Rails.logger go to the same object as Delayed::Worker.logger
Delayed::Worker.logger = Rails.logger
If the above code doesn't work, try replacing Rails.logger with RAILS_DEFAULT_LOGGER.

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 log file size too large

I stumbled to learn that my rails3.1 log file is super large, around 21mb.
Is this, in terms of size normal? What the log file would like in the production environment?
Besides, can I get rid of the log?thanks
The log folder of your Rails application holds three log files corresponding to each of the standard environments. Log files can grow very large over time. A rake task is provided to allow the easy clearing of the log files.
rake log:clear
# Truncates all *.log files in log/ to zero bytes
# Specify which logs with LOGS=test,development,production
you can just delete the file!
Rails will create a new log if one doesn't exist.
Obviously save / backup the file if it's important, but usually it's not.
You can also zip the backuped up file (and then delete the source) if you want to keep it on the same drive but still save space.
To automatically rotate log files (the best long-term solution) use log rotate as described here:
Ruby on Rails production log rotation
then you can set it and forget it!
To actually change what gets logged see:
http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/
According to the documentation, if you want to limit the size of the log folder, put this in your 'development.rb'-file:
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024)
With this, your log files will never grow bigger than 50Mb. You can change the size to your own preference. The ‘1’ in the second parameter means that 1 historic log file will be kept, so you’ll have up to 100Mb of logs – the current log and the previous chunk of 50Mb.
I automatically clear the logs in development on each server start with config/initializers/clear_development_log.rb:
if Rails.env.development?
`rake log:clear`
end
You may want to use logrotate. Have a look at the answer to this question: Ruby on Rails production log rotation.
Yes, You can using syntax like this:
config.logger = ActiveSupport::Logger.new(config.log_file, num_of_file_to_keep, num_of_MB*1024*1024)
Example:
config.logger = ActiveSupport::Logger.new(config.log_file, 2, 20*1024*1024)
It not only using for Rails log, you can using log file of any services run with rails, such as: rpush log, ...
config.logger = ActiveSupport::Logger.new(nil) does the trick and completely disables logging to a file (console output is preserved).
A fair compromise, in an initializer:
Rake::Task['log:clear'].invoke if Rails.env.development? || Rails.env.test?
If you don't like waiting for rake log:clear to load its environment and only want to clear one log on the fly, you can do the following:
cat /dev/null > log/mylog.log # Or whatever your log's name is
(This allows the log to stay online while the app is running, whereas rm log/mylog.log would require restarting the app.)

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