[Ruby on Rails (RoR)] Use Redis Cache Store in Rails
Redis Cache Store
The Redis cache store takes advantage of Redis support for automatic eviction when it reaches max memory, allowing it to behave much like a Memcached cache server.
Deployment note: Redis doesn’t expire keys by default, so take care to use a dedicated Redis cache server. Don’t fill up your persistent-Redis server with volatile cache data! Read the Redis cache server setup guide in detail.
For a cache-only Redis server, set maxmemory-policy to one of the variants of allkeys. Redis 4+ supports least-frequently-used eviction (allkeys-lfu), an excellent default choice. Redis 3 and earlier should use least-recently-used eviction (allkeys-lru).
Set cache read and write timeouts relatively low. Regenerating a cached value is often faster than waiting more than a second to retrieve it. Both read and write timeouts default to 1 second, but may be set lower if your network is consistently low-latency.
By default, the cache store will not attempt to reconnect to Redis if the connection fails during a request. If you experience frequent disconnects you may wish to enable reconnect attempts.
Cache reads and writes never raise exceptions; they just return nil instead, behaving as if there was nothing in the cache. To gauge whether your cache is hitting exceptions, you may provide an error_handler to report to an exception gathering service. It must accept three keyword arguments: method, the cache store method that was originally called; returning, the value that was returned to the user, typically nil; and exception, the exception that was rescued.
Use :redis_cache_store >= Rails 5.2
Rails 5.2.0 includes a Redis cache store out of the boxBuilt-in Redis cache store by jeremy · Pull Request #31134 · rails/rails · GitHub - https://github.com/rails/rails/pull/31134, so you don’t really need this gem anymore if you just need to store the fragment cache in Redis. Maintenance on the redis-activesupport gem will continue for security and compatibility issues, but we are no longer accepting new features. We are still actively maintaining all other gems in the redis-store family, such as [redis-actionpack for session management, and redis-rack-cache for HTTP cache storage.
To get started, add the redis gem to your Gemfile:
1 | # Gemfile |
You can enable support for the faster hiredis connection library by additionally adding its ruby wrapper to your Gemfile:
1 | # Gemfile |
Redis cache store will automatically require & use hiredis if available. No further configuration is needed.
Finally, add the configuration in the config/environment.rb or relevant config/environments/*.rb file:
1 | config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] } |
A more complex, production Redis cache store may look something like this:
1 | # config/environment.rb or config/environments/*.rb |
More configuration examples:
1 | # Defaults to `redis://localhost:6379/0` |
Use :redis_store < Rails 5.2
1 | # Gemfile |
1 | config.cache_store = :redis_store, { url: ENV['REDIS_URL'] } |
References
[9] redis-activesupport by redis-store - http://redis-store.org/redis-activesupport/
[10] How to Use The Redis Database in Ruby - RubyGuides - https://www.rubyguides.com/2019/04/ruby-redis/