Skip to content

Improving performance with caching

monfresh edited this page Dec 2, 2014 · 9 revisions

Depending on how many visitors your app gets, you might want to improve the performance of the app by configuring two types of caching: one is caching the API requests via Faraday::HttpCache in config/initializers/ohanapi.rb, and the other is caching the entire results page and/or location details page in cacheable.rb and locations_controller.rb

API request caching is turned on by default, and will prevent the app from making the same API request for a period of time specified by the API. By default, that is set to 1 minute. That means that if a location's info has changed on the API side, if Ohana Web Search had already requested that same location, the latest data changes won't appear in Ohana Web Search until 1 minute has passed since the first request was made.

As for page caching, it is disabled by default. If you determine that you need to cache pages, you can turn it on by setting the ENABLE_CACHING environment variable on your production server to true. If you're using Heroku, you can set it like this:

$ heroku config:set ENABLE_CACHING=true -a your_heroku_app_name

When page caching is enabled, it will store the page in Memcached via the MemCachier add-on on Heroku (by default) so that the next time any browser requests that same page, it will be served from cache instead of the server. In addition to the app storing the page in Memcached, most modern browsers will also store the page in their cache, and will send an If-Modified-Since date and an If-None-Match Etag in the Request Headers to ask the server if a newer version exists.

The server will determine whether or not a newer version of the page is available by comparing both the updated_at fields in the API response, and the current Git commit's SHA-1. This means that if you push changes to the page, the browser cache will be automatically invalidated.

Note that this is only one caching solution. For other caching strategies, visit the following resources:

https://devcenter.heroku.com/articles/http-caching-ruby-rails https://devcenter.heroku.com/articles/caching-strategies http://railscasts.com/episodes/321-http-caching http://brandonhilkert.com/blog/understanding-the-rails-cache-id-environment-variable/

Testing caching in development

You'll need to install Memcached if you don't already have it. On OS X, the easiest way to install it is via Homebrew:

brew install memcached

For other operating systems, visit the Memcached website.

Also make sure to set the following key in the development section of your config/application.yml file:

ENABLE_CACHING: 'true'