Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Rails 2/3 from caching of Lib/ Classes

Does anyone know how to instruct rails to NOT cache classes that are included within the lib folder?

like image 687
matsko Avatar asked Aug 06 '10 00:08

matsko


2 Answers

By "caching classes" I suppose you mean that source files inside the app directory are automatically reloaded in development environment before a new request is processed?

This is not related to caching, the normal behavior of Ruby is, to read and parse a source file once and never again as long as the process runs. Rails (ActiveSupport::Dependencies actually) provides a mechanism to reload the whole code before a request is processed. In development environment, this is useful since you don't want to restart the local webserver for every change you do to the code. In production environment this would badly hurt performance and is turned off therefore.

By default, the app classes are marked as reloadable. You can mark arbitrary classes to be reloaded before a request is processed in development environment by using the unloadable class method:

class MyClass
  unloadable # mark this class as reloadable before a request is processed

  # …
end

Beware that not every class may play well with unloading. As long as you define your class in one source file that gets found and loaded by Rails' autoloading mechanism, you're probably good. But can run into troubles if you re-open your class elsewhere to monkeypatch it since autoloading won't catch this.

like image 170
Zargony Avatar answered Nov 06 '22 13:11

Zargony


This can also be done by not using the lib/ directory, but instead by storing any classes or helper files within the app/helpers directory. This way they will be reloaded during development time and cached during production time.

like image 45
matsko Avatar answered Nov 06 '22 12:11

matsko