Saturday, November 26, 2011

Tips for MongoDB cache store and assets on Rails 3.1

(continued from phosphorescence: How to use MongoDB as model, session store and cache store on Rails3)

MongoDB cache store does not accept binary cache

Rails3.1's cache feature caches all – page caches, action caches, fragment caches and caches for files called assets(JavaScripts, stylesheets and images). All these are cached in default. But MondoDB cache store we have chosen in previous article is only supporting to cache text files. In other words, MongoDB cache store accepts JavaScripts and stylesheets, but does not accept images.

So we should configure to cache page caches, action caches and fragment caches, and not to cache assets at all. Referring these two sites(1 2), we can do it by editing config/environments/production.rb like below:
require 'rack/cache'
........
  # Prevent caching assets
  config.middleware.insert_before Rack::Cache, ::ActionDispatch::Static, 'public', config.static_cache_control
........

Before launch the web server

If you use Apache or nginx as your web server, you should modify the value of config.action_dispatch.x_sendfile_header on config/environments/production.rb. Then, if you use another web servers like WEBRick, you should modify to config.serve_static_assets = true on config/environments/production.rb.

If you plan to launch the web server on Windows, set config.assets.compress = false on config/environments/production.rb.

At last, precompile assets like below:
> rake assets:precompile
And then, if you launch the server on development mode with precompiled assets, you should modify to config.serve_static_assets = false on config/environments/development.rb.

No comments:

Post a Comment