rails memcache dev vs production - ruby-on-rails

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.

Related

clearing rails cache dynamically

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 ?

Can I change config.cache_classes programatically in Rails 3?

I have some iPhone client tests that run against my development rails server. The whole suite runs an order of magnitude faster if I turn on class caching in the Rails config. On the other hand, that slows down development when I'm not actually running the tests.
I want the test suite to hit an action at the beginning to turn on class caching and another action at the end to turn class caching off again.
Is this even possible? If so, how?
Not without some serious hacking. Rails goes to quite a lot of trouble to make sure your files are reloaded on every request (when cache_classes=false). The value of the cache_classes configuration variable is used by initializers in several places not the least of which being:
using require to load ruby files when cache_classes is true (meaning they are no longer reloadable)
setting up dispatcher callbacks to reaload the application on every request when cache_classes is false
You do have access to the value of the cache_classes variable, and you can even change it if you like:
Rails.configuration.cache_classes = true
But, this will have no effect on the running rails instance as the initializers where that value is used only run once when the rails app starts up.
What this means is this, unless you're prepared to invest some serious time and hacking effort you can't really avoid a restart of your server. So, what you need to look at is controlling this restart process via your test suite.
For example you can try to restart rails from within rails. This would allow you to define an action that your test suite can hit right before it begins executing (to restart the server in the right mode), and another action which the server can hit after all tests have finished, to restart everything with cache_classes set to what it used to be. You would control the value of cache classes via an environment variable like this post suggests.
It would still require a bit of work to set all of this up and get it to hang together, but this is probably your best bet if you want an 'auto-magical' solution.
I don't think doing what you suggest will work.
But I suggest you may be looking for the wrong solution.
If what you want is to access your development database from your iphone testing,
then why not add a new environment.
Add a new file config/environments/iphone_dev.rb
require File.dirname(__FILE__)+"/development.rb"
config.cache_classes = true
And in your database.yml (or mongoid.yml or whatever)
iphone_dev:
host: localhost
database: my_app_development
There is no reason the database cant be the same
Now just run rails server -eiphone_dev -p3001
You should have a server, almost the same as your dev server,
but running on a different port, with caching enabled.

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

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

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