简体   繁体   English

如何为Redis-As-Rails-Cache(使用Redis作为Rails缓存)Rails 4.1实现连接池?

[英]How do I implement connection pooling for Redis-As-Rails-Cache (using Redis as rails cache) Rails 4.1?

How can I add connection pooling to my Redis setup? 如何为我的Redis设置添加连接池?

for some reason I can't seem to find any information on this. 由于某种原因,我似乎找不到任何信息。 I'm using redis (3.2.0) set up like so: (you can assume resque_uri and cache_uri has been parsed ahead of this c) 我正在使用像这样设置的redis(3.2.0):(您可以假设resque_uri和cache_uri已在此c之前解析)

for resque: 对于resque:

$REDIS_BACKGROUND_JOB =  Redis.new(:host => resque_uri.host, 
                                  :port => resque_uri.port, 
                                  :password => resque_uri.password)

later in the initialization sequence, we do 在初始化序列的后面,我们做

Resque.redis = $REDIS_BACKGROUND_JOB

We have a 2nd Redis instance with a different memory, connection limit, and eviction policy, for the Rails cache 对于Rails缓存,我们有一个第二Redis实例,具有不同的内存,连接限制和逐出策略

$REDIS_CACHE = Redis.new(:host => cache_uri.host, 
                        :port => cache_uri.port, 
                        :password => cache_uri.password)

UPDATE 2017-02-16@20-17EST: I am running two redis instances, one for resque (1.25.2) and another for the caching layer using redis-rails (5.0.1). 更新2017-02-16 @ 20-17EST:我正在运行两个redis实例,一个用于resque(1.25.2),另一个用于使用redis-rails(5.0.1)的缓存层。 I have updated the setup examples above. 我已经更新了上面的设置示例。 I suspect something is misconfigured in the initialization or there is a fundamental setup mismatch. 我怀疑初始化中配置错误或基本设置不匹配。

UPDATE 2017-02-16@21-37EST: 更新2017-02-16 @ 21-37EST:

environments/development.rb has environments/development.rb具有

config.cache_store = :redis_store, 'redis://localhost:6379/1/cache_store'

OK here's what I got working. 好的,这就是我的工作。

added to my Gemfile 添加到我的Gemfile

gem 'connection_pool'

For the RAILS CACHE: 对于RAILS CACHE:

Initializing $REDIS_CACHE = Redis.new for the Rails cache is incorrect, as Rails does this inside of the config.cache_store setting in your environment files ( config/environments/development.rb , config/environments/production.rb , etc) 为Rails缓存初始化$REDIS_CACHE = Redis.new不正确,因为Rails在环境文件( config/environments/development.rbconfig/environments/production.rb等)的config.cache_store设置内执行此操作

And then my initialization in my environment files is as follows: 然后,我在环境文件中的初始化如下:

config.cache_store = :redis_store, ENV["REDISCLOUD_FOR_RAILS_CACHE_URL"], {pool_size: 10, pool_timeout: 10}

(you will note that ENV["REDISCLOUD_FOR_RAILS_CACHE_URL"] is set as the full URI, including protocol, username, password, host, and port. An alternative is to pass these each one as its own parameter here in a hash.) (您会注意到,将ENV["REDISCLOUD_FOR_RAILS_CACHE_URL"]设置为完整URI,包括协议,用户名,密码,主机和端口。另一种方法是在哈希中将这些参数作为自己的参数传递。)

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

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