Rails 4 : What is cached when using config.cache_classes = true - ruby-on-rails

I was just wondering and didn't find explicit response on what in the model class (ActiveRecord) is cached when setting config.cache_classes to true ?
Could someone tell me or point me to the doc I didn't found ?
Thanks

It determines whether or not your application classes are reloaded on each request. If it's true, you have to restart your server for code changes to take effect (i.e. you set it to true in production, false in development.)
Documentation is here.

What is cached when using config.cache_classes = true
It responsible for two thing in rails 4
1. It prevent class reloading between requests.
2. It ensure Rack::Lock in not included in middleware stack, so
that your thread don't get locked.

Related

Question about Rails 6 config.cache_classes behavior

I am pretty new to Rails.
Rails automatically reloads classes and modules if application files in the autoload paths change.
(I don't change any files at this moment)
In my test,
a simple /foo request will always take around 200ms to response when config.cache_classes = false.
the simple /foo request can down to 60ms in the second hit when config.cache_classes = true.
will config.cache_classes = false. cache the class and modules in the ruby process memory,
if yes, why it was still 200ms instead of 60ms after the first hit
when config.cache_classes = false.
if no , why we need auto reload since it will always look up in the
file system for the change.
I got confused by this behavior, Any help is appreciated.

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: Stop Caching for Testing

We are load testing an application. I just want to check how it behaves if it hits database every time a request is made. I want to stop all type of caching temporarily. Is there a to do this?
Thanks,
Imran
In development mode by default no caching performed. You can adjust caching in config/environments/development.rb and config/environments/production.rb
E.g., there're following values in the production config by default
config.cache_classes = true
config.action_controller.perform_caching = true
config.action_view.cache_template_loading = true

In Rails, what is the standard way to turn on code-caching for Model, View, Controller, all of them, or individually?

Is it true that the standard way to say "cache all Model, View, Controller code" when running the Rails server, by using the following line in config/environments/development.rb
config.cache_classes = true
and for don't cache any of them:
config.cache_classes = false
and to "selectively" cache any one of them, use the above false line, and in config/environment.rb:
config.load_once_paths += %W( #{RAILS_ROOT}/app/models )
which will only cache the Model code. And to cache Controller code or View code, just add either
#{RAILS_ROOT}/app/controllers
or
#{RAILS_ROOT}/app/views
to inside the %W{ }. For example, if we are only developing the Views (HTML and CSS), then there is no need to reload
Model and Controller code when running the server, so set load_once_paths for Models and Controllers,
and just let the View code load every time? (is there docs that talk about this?)
Well, there is no documentation that explains this in detail, but you can read about rails configuration here : http://guides.rubyonrails.org/configuring.html
As for your question, You are absolutely correct :).
use config.load_once_paths to cache selectively ( Obviously with config.cache_classes = false )
And use config.cache_classes = true to cache everything

Purpose of "consider_all_requests_local" in config/environments/development.rb?

What is the purpose of this Rails config setting...
config.action_controller.consider_all_requests_local = true
It's set to true by default in config/environments/development.rb.
Thanks,
Ethan
Non-local requests result in user-friendly error pages. Local requests, assumed to come from developers, see a more useful error message that includes line numbers and a backtrace. consider_all_requests_local allows your app to display these developer-friendly messages even when the machine making the request is remote.
At development level we set:
consider_all_requests_local set = true
because developer needs to take a look at full error showing layout/view as you can see in the image below.
But at production level, we don't need to show our internal coding bug so we set false:
config.consider_all_requests_local = false

Resources