clearing rails cache dynamically - ruby-on-rails

I am working on rails application , in which i am using ruby 1.9.2 and rails 3.0.8. My application is running quite fine in development environment, which includes creating tables from the application and accessing them.
But when i start my application in production environment in which caching is enabled, every thing is working fine , i am not able to access the table which i am creating using my application. I am able to access these tables after restarting the server, which is a pain.
I am searching for a way where i can clear the cache whenever new table get created, can you please help me to clear the cache dynamically.
Thanks
Naveen Kumar Madipally

The one workaround would be to do this in your environments/production.rb (which is not at all recommended on production)
config.cache_classes = false

this will decrease your performance in production but what you can do is fo to production.rb file and check the blow lines
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
i guess it will solve your problem

There are abstractions for that in ActionDispatch::Reloader : it's what's used in development environment to reload classes.
So basically, you would need to run :
ActionDispatch::Reloader.cleanup!
ActionDispatch::Reloader.prepare!
I'm not sure it would be such a good idea, though, as you can't expect which code (yours or from gems) does things that are supposed to happen only once.
Couldn't you use STI rather than dynamicly creating tables ?

Related

Manually Reload Rails classes that have been 'cached' by enabling cache_classes = true

I'm trying to speed up my web fronted by caching classes in development,
My::Application.configure do
config.cache_classes = true
end
but I would like to manually reload classes using guard if a file in my model or lib changes.
So the question is this: without restarting my local server, how can i manually trigger a class cache refresh?
Update
You can use reload!
don't know why i didn't think of that sooner
Even I don't answer your raw question, this link should answer your goal as a whole.
In a nutshell:
Reload Rails code in development mode only when change is detected

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.
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.
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
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.

What is the best way to develop and test cache in a Ruby on rails application?

I am using Ruby on Rails 3 and I would like to start "to code" the cache system.
I am working on localhost in development mode. I heard that working in development mode it is possible to face some "confusing" errors. Why? What is the best way to that cache?
You just need a clear mind.
In development.rb, change this:
config.action_controller.perform_caching = true
Then work and test. It will write the cached pages in /public, and fragments in /tmp/cache
Here are great articles to understand caching in Rails:
http://broadcastingadam.com/2012/07/advanced_caching_revised

rails memcache dev vs production

I am using memcache for caching in my rails app and currently I have a dev and a production environment.
I would like to run the dev environment without caching so that I can debug more easily but I wanna enable the caching in production obviously. I am using github and capistrano for deployment.
Without doing a check at every statement where I can potentially dig into the cache, is there any way of handling this more gracefully or globally?
if env == 'dev'
#post = Post.all
else
//get #post from cache
end
You may want to consider adding a "flush_all" some where in your capistrano deployment script. This should help in removing "old" cache content for your capistrano development pushes.
You may also want to consider overloading/aliasing the get/set CACHE functions and just replacing the code where you are doing the caching. I haven't seen a good implementation of this.
You may also want to overload the Rails.logger and add in display of the memcache code where you want it
Rails.logger.memcache_display = true
..
..
Rails.logger.memcache_display = false
This doesn't answer your question, but this can help make debuging more easy.

Why don't my views update?

I'm new to Ruby on Rails and am creating a test application. So far, it's working, but when I make some minor changes to my views, the page doesn't change.
My problem may be related to this question, but I'm not sure what is meant by setting the date and time in the VM. My code is on a remotely hosted server, so I assume it would use the system time of that machine.
Is there a caching issue here? What can I do about it?
If you don't have control over the server environment yourself (no shell access, etc.), you can set the following at the top of config/environment.rb:
ENV['RAILS_ENV'] = 'development'
Development doesn't cache much, so while it's slower it's much nicer to develop in.
You'll still need to restart your app after making changes to anything outside the app/ folder though (configs, plugins, etc.).
You need to restart your Rails app (or Apache if you are using Passenger) if you are in production mode!

Resources