Changes
- If you're using a Redis cache in Rails (e.g. `:redis_cache_store`), it's possible to configure additional parameters for your Redis connection.
-See this example for an explanation:-- +## Example config for Rails 7.2
- ```ruby
-# e.g. production.rb-- config.cache_store = :redis_cache_store, {
-connect_timeout: 30,- +pool: { timeout: 0.5 },
- read_timeout: 0.2, # default 1 second
- write_timeout: 0.2, # default 1 second
- reconnect_attempts: 1, # 0- +# Attempt two reconnects with some wait time in between
- +reconnect_attempts: [1, 5], # default `1` attempt in Redis 5+
- url: REDIS_URL,
- error_handler: ->(method:, returning:, exception:) {
- Sentry.capture_exception(exception)
- },
- }
- ```
- ## Timeouts
-You probably want to adapt these settings depending on your application. The default value for `read_timeout` and `write_timeout` is 1 second, which is quite high for a standard application, where regenerating the cached values probably is a lot faster than the 1 second timeout. Be careful when changing these values, because setting a too low value for timeouts can make your cache timeout too often.- +You probably want to adapt these settings depending on your application. The default value for `read_timeout` and `write_timeout` is 1 second, which is quite high for a standard application, where regenerating the cached values probably is a lot faster than the 1 second timeout. Be careful when changing these values, because setting a too low value for timeouts can make your cache timeout too often. Note that Rails explicitly sets `connect_timeout`, `read_timeout` and `write_timeout`, which means that only configuring `timeout` will _not_ propagate as expected.
- ## Reconnection attempts
-The Redis client will raise a `Redis::ConnectionError` if it can't connect. To avoid getting this error from time to time, it's possible to increase the `:reconnect_attempts` setting to something bigger than 0. Don't set it too high, so you still notice if your connection setup has problems, but setting it to 0 in a production like environment seems too strict.- +The Redis client will raise a `Redis::ConnectionError` if it can't connect. An integer for `reconnect_attempts` will be interpreted as seconds to wait. An array of numbers will be taken as the seconds to wait before each reconnect attempt.
- ## Error handler
- To notice exceptions raised by the client you can configure e.g. Sentry to pick up these exceptions.
- ## Details
- See the [official guides](https://guides.rubyonrails.org/caching_with_rails.html#activesupport-cache-rediscachestore) for more information.
- You can use `Rails.cache.stats` to get basic information about the current redis connection.
Posted by Dominik Schöler to makandra dev (2024-12-04 11:11)