Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to programmatically clear Rails 3 layouts and views cache?

I have a Rails 3 based CMS that allows users to create and modify layouts and views. These layouts and views are the same ones built into the framework, only backed by a model for some additional capabilities. The problem I would like to address is that these template files are cached as soon as they are accessed on the public end, so it is not possible to see changes in the layouts or views unless the server is restarted. This does not occur in development mode where caching is disabled, but obviously turning off template caching in production wouldn't be great for performance. Clearing memcache doesn't seem to do the trick. Is it possible to programatically clear out the layouts and views cache in production, perhaps with something like reload! like we have in the console? Or am I stuck having to restart Passenger every time someone wants to tweak one of these layouts or views (perhaps using the approach in this thread: Rails Cache Clearing)?

Please note that I am not referring to clearing the page and action caches, which the public pages rely on and works just fine.

like image 277
cmarin Avatar asked May 02 '11 23:05

cmarin


2 Answers

José Valim has a great chapter in "Crafting Rails Applications" that goes over this topic. Here is an approach that uses Mongoid to store view templates. If you build your own view Resolver, then you just need to call #clear_cache on the resolver instance when someone saves a new template in the database.

like image 198
monocle Avatar answered Sep 25 '22 20:09

monocle


this configuration may help (at least it worked* for me):

config.action_view.cache_template_loading = false
  • works in rails 3

There's just a slight difference in rails 2:

config.action_view.cache_template_reloading = false
like image 24
Evgeniya Manolova Avatar answered Sep 23 '22 20:09

Evgeniya Manolova