Types of Cache-Store

Explore various caching stores to manage cache data in Rails

Tushar Adhao
2 min readAug 24, 2022

The cache store is the place where cached data gets stored and utilized effectively. So it could be in memory, external files, etc. depending on the store being used. It has a vital role in improving the performance of the application. Let’s discuss various stores for caching in this blog.

ActiveSupport::Cache::FileStore

When no cache store is mentioned in the config of Rails then FileStore is used by default. As per the name, it saves the cached data in a file with the location of tmp/cache the directory which can be changed as follows,

config.cache_store = :file_store, 'path/to/store/cache'

The con to it is the cache files need to be removed manually or with regular use of Rails.cache.cleanup command.

Note: In production enviroment, One of the resons why the server stops working is when the cache memory consumes all the remaining memory from the allocated size for the server

ActiveSupport::Cache::MemoryStore

MemoryStore is best to use in the development environment as the memory goes clear after restarting the server and the least used cached data gets deleted automatically when the memory goes full. After Rails version 5, the new project gets initially configured for the development environment with the memory store. MemoryStore is configured to store 32MB data by default but it can be changed as follows,

config.cache_store = :memory_store, { size: 16.megabytes }

ActiveSupport::Cache::MemCacheStore

The Memcache Store is similar to the MemoryStore but the data is kept in a separate process than a ruby process. External gem is used to use the Memcache store which is dalli gem with another Memcache server. Memcache is configured to store 64MB data by default but can be configured accordingly. The Memcache store assumes the cache server is running on localhost by default but can be configured as below,

config.cache_store = :mem_cache_store, "cache-1.example.com"

ActiveSupport::Cache::RedisCacheStore

The RedisCache Store is similar to the Memcache store but the difference is in the Redis server being used instead of the Memcache server. It has been introduced with Rails version 5.2. The external gem used here is the Redis gem and Hiredis. The server configuration can be done as below,

config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] }

ActiveSupport::Cache::NullStore

The NullStore is ideal to use for development and test environment. By default, in a development environment when caching is off, Rails uses a Null Store cache. The lifespan is very short and persists per request. It can be configured as below,

config.cache_store = :null_store

Note: By default caching is enabled in production enviroment. So as to use it in development enviroment, rails dev:cache command can be used to toggle the on/off state for caching

Let’s explore some more interesting topics in the upcoming blogs. Stay tuned!

--

--

Tushar Adhao

Software artist spreading nuggets of coding gold and sometimes philosophy too.