简体   繁体   中英

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.

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

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.

In production mode, it's normal to require a restart to implement rails code changes, which is what you are doing by editing the layouts and views. It sounds like you're really operating in a development environment if you are editing the application code while it's running. In production mode I don't know of a way to refresh Passenger without touching restart.txt or restarting the web server.

EDIT: You should be able to touch tmp/restart.txt programmatically from within your app. This should tell Passenger to reload on the next request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM