Ruby on Rails Caching

Caching in Rails is easy since the framework has some built-in functionality for this action. Developers do not have to look for 3rd party applications to efficiently cache their applications.

Although a 3rd party caching tool is still a possibility, understanding cache in rails will deem the 3rd party applications unnecessary. After years of updating, Rails have included great caching tools for ease of development.

Caching Types in Rails

  • Page Caching – As the name suggests, this type of caching in rails caches data from the users webpage. This will prevent the application from running data again when the user needs them in the future. However, this type of caching has limitations. 
    .
    Page caching is only limited to pages and data on sites that will not require authentication or when the data is located in the secured area of the webpage. This is understandable as developers have to implement security in their application.
    .
  • Action Caching – Since page caching cannot run on secured area of the webpage or application, action caching is used. Action caching almost has the same behavior with page caching but instead of directly recording data, information will have to go through Rails Stack. 
    .
    After the stack, the data will again go through Action Pack (one of the forms of Rails framework). This process will enable filtering of data wherein the secured data will not be revealed unless authentication is properly implemented.
    .
  • Fragment Caching – Fragment caching is implemented when you are looking for a caching tool for complicated websites or applications. There are websites wherein the expiration of the cached data has to be implemented in different times. Page and action caching cannot implemented different expiration time.  Through fragment caching, you can specify which data will expire first or the data that will be consistently cached.

Cache Sweepers

For additional security, Rails have added sweepers for its cache. A sweeper for cache is practically a tool that will completely remove all cached data in on simple command. Developers could set a time or date when the cached data will be completely removed.

Sweepers could also be used as an observer for data changes. When properly implemented, sweepers could monitor the changes in the application. If new data is sensed, the sweeper could be configured to automatically delete the previous information.

This is very useful especially when you have an online application that continuously processes data. Through sweepers, the expected data will be loaded faster as it has deleted the previous information and concentrated on the new data.

SQL Caching

This form of caching is specifically geared towards the database of the application. This form of caching is also built-in on Rails so compatibility with SQL is no problem at all.

SQL caching records all queries which could be easily loaded again when the user needs them. Like most caching tools, expiration could also be implemented at the developer’s preference.

Forms of Storing Cached Data

Rails also enable developers to specify how the cached data will be stored. This gives the developers a freedom on how fast (or slow) their data could be recorded and implemented.

  • Memory Store – Data will be stored in the memory provided by developers for Rails. Its advantage is that it can easily work with WEBrick which efficiently improves loading time for most webpages. However, it cannot work with CGI (Common Gateway Interface). Be careful in implementing this type of storage – it can easily drag down an application and the gadget as it will aggressively use the memory in recording data.
    .
  • File Store – Instead of using the memory, file store will enable the actual storage facility of the gadget. This makes it a lot easier to store files as it could work with almost any environment.
    .
  • DRb Store – As the name suggests, data is placed in DRb which is implemented in the server. It will require developers to build a separate DRb process for data.
    .
  • MemCached Store – Requires developers to build a library with the same name from Ruby where data will be stored.
    .
  • Custom Store – Available since Version 2.1, developers could now specify where the cached data will be placed if the above mentioned options are not ideal for the application.

Editorial Team at Geekinterview is a team of HR and Career Advice members led by Chandra Vennapoosa.

Editorial Team – who has written posts on Online Learning.


Pin It