Hi all,
In my project, I’m using Redis as the cache_store
.
However, I noticed a behavior that concerns me, and I would like to hear your opinions.
I’m using environment variables to specify the configuration values such as pool_size
for the cache_store
. However, I encountered the following error unless the values are converted to numeric types
# config/environments/development.rb
config.cache_store = :redis_cache_store, {
url: ENV.fetch('CACHE_REDIS_URL'),
pool_size: ENV.fetch('CACHE_REDIS_POOL_SIZE', 5),
:
}
[1] pry(main)> Rails.cache.write("/foo", "bar")
/usr/local/bundle/gems/connection_pool-2.3.0/lib/connection_pool/timed_stack.rb:67:in `+': String can't be coerced into Float (TypeError)
https://github.com/mperham/connection_pool/blob/main/lib/connection_pool/timed_stack.rb:67
It seems that the values retrieved from the environment variables remain as strings, which causes the above error. Of course, the values retrieved from environment variables are of string type. This issue can be resolved by using to_i
.
# config/environments/development.rb
config.cache_store = :redis_cache_store, {
url: ENV.fetch('CACHE_REDIS_URL'),
pool_size: ENV.fetch('CACHE_REDIS_POOL_SIZE', 5).to_i,
:
}
However, I don’t think it’s appropriate that the error occurs only when writing to the cache if a string value is specified. Perhaps it would be better if an error occurred when a string value is provided.
How do you feel about it? We look forward to hearing your thoughts.
Thanks for everyone’s efforts on Rails!