How to set Rails Cache log level separately? - ruby-on-rails

I use a pretty old rails version which is 2.3.2 because of legacy project.
I set global log_level to :debug in our rails app. But since we also use Rails.cache the log file are full of annoying lines such as
Cache read: ...
Cache miss: ...
I want to just suppress these but not affect other 'more useful' info such as SQL logging.
How to do that?

Well, after initializing your cache store (in the example below, I use memory store) in your specific environment.rb file, you can redirect cache_store's log to a separate file and also tweak the logger level:
config.cache_store = ActiveSupport::Cache::MemoryStore.new(:expires_in => 5.minutes)
config.cache_store.logger = Logger.new("#{Rails.root}/log/#{ENV['RAILS_ENV']}_cache.log")
config.cache_store.logger.level = Logger::INFO
In addition to that, the cache store has a method called silence! that will turn off the logger :-|
config.cache_store.silence!

Related

`method_missing': undefined method `cache' for #<Rails::Application::Configuration:

I am having the mentioned error on a rails project, after I added the following line of code in the config/environments/development.rb
config.cache.store = :dalli_store
I am working on a personal project using Rails 4.2, installed on OS X 10.7.5. Also memcached 1.4.5 is installed an running in my computer. I don't understand why this is happening. I want to use cache.store on my project but, I don't know how to solve this problem. Any help will be well received !
Please change:
config.cache.store = :dalli_store
to
config.cache_store = :dalli_store
Guide says Cache Stores
Rails provides different stores for the cached data created by action and fragment caches.
Configuration
You can set up your application's default cache store by calling config.cache_store = :dalli_store
Alternatively, you can call ActionController::Base.cache_store outside
of a configuration block.
You can access the cache by calling Rails.cache
The cache store has a bounded size specified by the :size options to the initializer (default is 32Mb).
If you need to increase that then like below
config.cache_store = :dalli_store, { size: 64.megabytes }
And finally you written config.cache.store = :dalli_store that is wrong because Rails guide says config.cache_store
If you need to know more about Rails 4.2 Caching then please visit Rails official document about Cach here
Hope it helps

Rails fragment caching not working locally

I've added the following at the top of my index.html.haml:
- cache do
content
And as far as I can see, the content is not cached. My server output shows that when I reload the page, it still fetches all the info from the database again.
I haven't tried on live as I don't want to push anything before it's 100% working. What am I doing wrong? Am I not understanding how it's supposed to work? I've set the config.action_controller.perform_caching = true.
cache_store configures which cache store to use for Rails caching so you need to specify that
You need to set cache store in general config.
config.cache_store = xyz,abc # PUT THIS
Options that you can set:
:memory_store, :file_store, :mem_cache_store

Rails: control file store cache size

The documentation for the file-based cache in rails says:
Note that the cache will grow until the disk is full unless you
periodically clear out old entries.
Unfortunately it doesnt give any information about how to clear old entries periodically. Does setting an appropriate value for :expires_in do the job or is there some other sort of black magic behind clearing the cache?
Also, the documenation gives an option to limit the memory-based cache in size:
config.cache_store = :memory_store, { size: 64.megabytes }
Does this also work for the file based cache? And even more importantly, what happens when the cache growths below that size limit? Does it remove old cached values or will it throw some kind of exception?
thanks in advance,
danijoo
Experimenting with FileStore cache I found that :expires_in options works, but :size one doesn't.
If you want to specify options then you need to also specify the path, try with the following example:
config.cache_store = :file_store, Rails.root.join('tmp', 'cache'), { expires_in: 1.minute }
Put the code in config/application.rb and remember to activate the cache in config/environments/development.rb and restart the app.
P.S. I use 1 minute to easily do a quick test.
Yes the limit applies in case of file-based cache too. And yes a value for :expires_in will do the job.
When this limit is reached, no further stuff will be cached. No exception is thrown.

Why does Rails not refresh classes on every request (despite configuration)?

I recently started having to restart my development server every time I change my code. My development.rb file still has this line:
config.cache_classes = false
I tried using the debugger verify that this value has stuck around. To do this I set my configuration to a global variable in environment.rb:
$my_initializer = Rails::Initializer.run do |config|
...
end
then I put a debugger line in one of my controllers so I could do this:
(rdb:2) $my_initializer.configuration.cache_classes
false
So that eliminated the possibility that the value of cache_classes was getting set to true somewhere else. I've tried using both Mongrel and WEBrick and it still happens.
What else might be causing Rails not to reload my code with every request?
I am running:
Mongrel 1.1.5
WEBrick 1.3.1
Rails 2.3.8
Ruby 1.8.7 p253
EDIT:
at #Daemin 's suggestion I checked that the mtime of my files are are actually getting updated when I save them in my text editor (Textmate)
merced:controllers lance$ ls -l people_controller.rb
-rwxr-xr-x 1 lance staff 2153 Act 10 18:01 people_controller.rb
Then I made a change and saved the file:
merced:controllers lance$ ls -l people_controller.rb
-rwxr-xr-x# 1 lance staff 2163 Oct 11 12:03 people_controller.rb
So it's not a problem with the mtimes.
So it turns out that config.threadsafe! overwrites the effect of config.cache_classes = false, even though it doesn't actually overwrite the value of cache_classes (see my question for proof). Digging around a bit more in the Rails source code might illuminate why this might be, but I don't actually need threadsafe behavior in my development environment. Instead, I replaced my call to config.threadsafe! in environment.rb to
config.threadsafe! unless RAILS_ENV == "development"
and everything works fine now.
If anyone else has this problem the solution was the order: config.threadsafe! has to come before config.cache_classes. Reorder it like this to fix it:
...
config.threadsafe!
config.cache_classes = false
...
answer from: Rails: cache_classes => false still caches
I suspect that the classes you are expecting to refresh have been 'required' somewhere in your configuration. Note that Rails' dependency loading happens after Ruby's requires have happened. If a particular module or class has already been required, it will not be handled by Rails' dependency loader, and thus it will not be reloaded. For a detailed explanation, check out this article: http://spacevatican.org/2008/9/28/required-or-not
Despite the fact that the threadsafe! solution works, I also wanted to point out for your benefit and the others that may come in after the following...
If you're editing engine code that is directly in your vendor/engines directory, those files will not be updated without a restart. There may be a configuration option to enable such functionality. However, this is very important to remember if you have used engines to separate large bits of functionality from your application.
My guess would be that it's not reloading the classes for each request because they haven't changed between requests. So the system would note down the last modified time when the classes are loaded, and not reload them until that changed.

Rails Caching Log Level

With the new caching options in Rails 2.1 i get nice entires in my log along the lines of
Cached fragment hit: views/homepage (0.16549)
However they are logged at the :debug level, which is the same level as the SQL output. I want to be able to disable the SQL output, and still see the cache info. How can I do this
well you could instantiate a specific logger for ActiveRecord and set it's log level to :info while leaving the default logger at debug ...
ActiveRecord::Base.logger = Logger.new("#{RAILS_ROOT}/log/#{RAILS_ENV}_database.log")
ActiveRecord::Base.logger.level = Logger::INFO # should set the log_level to info for you
from http://wiki.rubyonrails.org/rails/pages/HowtoConfigureLogging
or you could reopen AbstractAdapter and override the log(sql,name) method so it does nothing
http://api.rubyonrails.com/classes/ActiveRecord/ConnectionAdapters/AbstractAdapter.html#M001242

Resources