Home arrow Ruby-on-Rails arrow Page 2 - Caching and Performance Optimization

Caching and Performance Optimization

In this fourth part of a six-part series on optimizing the performance of a Ruby on Rails application, you'll learn about action caching, fragment stores, and more. This article is excerpted from chapter 13 of the book Practical Rails Projects, written by Eldon Alameda (Apress; ISBN: 1590597818).

Author Info:
By: Apress Publishing
Rating: 5 stars5 stars5 stars5 stars5 stars / 4
December 13, 2010
  1. · Caching and Performance Optimization
  2. · Fragment Stores

print this article

Caching and Performance Optimization - Fragment Stores
(Page 2 of 2 )

Page caching results are always stored in the file system. Fragment caching has a few more options:

  1. File store: The contents of the cache are stored in the file system, just as in page caching. 
  2. Memory store: All the cached fragments are stored in memory. This is the fastest option of all, but it doesn’t scale beyond one application server process, since each process keeps its own cache. 
  3. DRb store: Cached fragments are kept in the memory of a shared DRb (Distributed Ruby) process. This option scales, but it requires you to build and maintain the process yourself. 
  4. Memcache store: Fragments are stored in a memcached process. Is very fast and scales well, since memcached can be accessed through the network.

The fragment store that is being used can be chosen either on the application (inconfig/environment.rb) or environment level (for example, inconfig/environments/production.rb) by setting thefragment_cache_storeparameter:

ActionController::Base.fragment_cache_store = :mem_cache_store, "localhost"

Note  Technically, sincefragment_cache_storeis a class variable of theActionController::Baseclass, you can overwrite it on a controller-specific level as well.


According to its website (www.danga.com/memcached/), memcached is "a high-performance, distributed
memory object caching system." It was developed by Danga Interactive to help speed the performance of
LiveJournal.com, a site serving more than 20 million dynamic page views per day.

Memcached can be used to temporarily store items such as the results of complex database queries and objects that are accessed frequently. Contents of memcached are always stored in RAM, so fetching them is blazingly fast.

Memcached is very lightweight and easy to deploy. It is very memory-hungry and CPU-light, so it is a good companion for an application server, which is often CPU-hungry and lighter on memory. Good advice is to deploy memcached on any server that has free memory, because the load of memcached servers can be distributed.

In a standard Rails setup, you can use memcached to store sessions and cache data. With the libraries introduced later in this chapter, in the "Caching ActiveRecord Objects" section, you can basically cache whatever data you want.

Please check back for the next part of this article.

DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus

- Ruby-on-Rails Faces Second Security Flaw in ...
- Ruby 2.0 Prepped for February 2013 Release
- Why LinkedIn Switched from Ruby on Rails
- Adding Style with Action Pack
- Handling HTML in Templates with Action Pack
- Filters, Controllers and Helpers in Action P...
- Action Pack and Controller Filters
- Action Pack Categories and Events
- Logging Out, Events and Templates with Actio...
- Action Pack Sessions and Architecture
- More on Action Pack Partial Templates
- Action Pack Partial Templates
- Displaying Error Messages with the Action Pa...
- Action Pack Request Parameters
- Creating an Action Pack Registration Form

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials