In my environment file, I have:
config.cache_store = :redis_cache_store, { url: ENV.fetch('REDIS_URL_CACHING', 'redis://localhost:6379/0')}
In rails console, if I print REDIS_URL_CACHING
, I get:
> ENV['REDIS_URL_CACHING']
=> "redis://:mypassword@localhost:6379/0
However, if I want to check if rails is connected to redis, I get:
> Rails.cache.redis.keys
=> false
irb(main):004:0> Rails.cache.redis.keys
Traceback (most recent call last):
1: from (irb):4
Redis::CannotConnectError (Error connecting to Redis on localhost:6379 (Errno::EADDRNOTAVAIL))
My redis.conf file is like:
bind 0.0.0.0
requirepass mypassword
What am I missing here?
If I remove password option, it’s working, but my macBook gets attacked.
EDIT
In redis gem guide, you can set password for redis like the following:
redis = Redis.new(url: "redis://:[email protected]:6380/15")
# Or
redis = Redis.new(password: "mysecret")
# And then
redis.set("foo", "bar")
redis.get("foo")
However, I’d like to use low-level caching in the way mentioned in the official low level caching guide
class Product < ApplicationRecord
def competing_price
Rails.cache.fetch("#{cache_key_with_version}/competing_price", expires_in: 12.hours) do
Competitor::API.find_price(id)
end
end
end
Can’t figure out how I can config redis gem to use with Rails.cache.fetch
2
Answers
In my case, I dockerized redis container, so it cannot be connected with localhost.
Passing url with my docker host machine address and password (like in @Kache's answer) solved problem.
Based on the testing that’s been done, it looks like the only issue is configuring Rail’s cache adapter to properly deliver the password to the Redis client during configuration.
In Rails source, setting the config will pass the latter parameters to the underlying Redis client.
So if this works:
Have Rails pass through those settings like so: