简体   繁体   English

Rails 缓存存储:如何使用密码配置 redis_cache_store

[英]Rails cache store: how to configure redis_cache_store with password

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:在 rails 控制台中,如果我打印REDIS_URL_CACHING ,我会得到:

> ENV['REDIS_URL_CACHING']
=> "redis://:mypassword@localhost:6379/0

However, if I want to check if rails is connected to redis, I get:但是,如果我想检查 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))

My redis.conf file is like:我的 redis.conf 文件如下:

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 .如果我删除密码选项,它可以工作,但我的 MacBook 受到攻击

EDIT编辑

In redis gem guide , you can set password for redis like the following: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")

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不知道如何配置 redis gem 与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.根据已完成的测试,似乎唯一的问题是配置 Rail 的缓存适配器以在配置期间正确地将密码传递给 Redis 客户端。

In Rails source, setting the config will pass the latter parameters to the client .在 Rails 源码中, 设置配置会将后面的参数传递给客户端

This means if this works:这意味着如果这有效:

Redis.new(url: your_url, password: your_pass)

Have Rails do the same by setting configuration like so:通过像这样设置配置让 Rails 做同样的事情:

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.就我而言,我对 redis 容器进行了 dockerized,因此它无法与 localhost 连接。

Passing url with my docker host machine address and password (like in @Kache's answer) solved problem.通过 url 和我的 docker 主机地址和密码(如@Kache 的答案)解决了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM