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).
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:
File store: The contents of the cache are stored in the file system, just as in page caching.
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.
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.
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:
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.