[英]Rails cache store: how to configure redis_cache_store with password
在我的環境文件中,我有:
config.cache_store = :redis_cache_store, { url: ENV.fetch('REDIS_URL_CACHING', 'redis://localhost:6379/0')}
在 rails 控制台中,如果我打印REDIS_URL_CACHING
,我會得到:
> ENV['REDIS_URL_CACHING']
=> "redis://:mypassword@localhost:6379/0
但是,如果我想檢查 rails 是否連接到 redis,我會得到:
> 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))
我的 redis.conf 文件如下:
bind 0.0.0.0
requirepass mypassword
我在這里想念什么?
如果我刪除密碼選項,它可以工作,但我的 MacBook 受到攻擊。
在redis gem guide中,您可以為 redis 設置密碼,如下所示:
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")
但是,我想按照官方低級緩存指南中提到的方式使用低級緩存
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
不知道如何配置 redis gem 與Rails.cache.fetch
一起使用
根據已完成的測試,似乎唯一的問題是配置 Rail 的緩存適配器以在配置期間正確地將密碼傳遞給 Redis 客戶端。
在 Rails 源碼中, 設置配置會將后面的參數傳遞給客戶端。
這意味着如果這有效:
Redis.new(url: your_url, password: your_pass)
通過像這樣設置配置讓 Rails 做同樣的事情:
config.cache_store = :redis_cache_store, { url: your_url, password: your_pass }
就我而言,我對 redis 容器進行了 dockerized,因此它無法與 localhost 連接。
通過 url 和我的 docker 主機地址和密碼(如@Kache 的答案)解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.