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 .
In redis gem guide , you can set password for redis like the following:
redis = Redis.new(url: "redis://:p4ssw0rd@10.0.1.1: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
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 client .
This means if this works:
Redis.new(url: your_url, password: your_pass)
Have Rails do the same by setting configuration like so:
config.cache_store = :redis_cache_store, { url: your_url, password: your_pass }
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.