茶歇驿站 成长之路由我相伴。

memcache和redis的连接池connection_pool使用

2014-11-05

memcache 的Ruby客户端比较常用的是(DalliCache)[https://github.com/mperham/dalli]。 连接池项目(connection_pool)[https://github.com/mperham/connection_pool]。

实战分析:

在application.rb 中添加

redis

case ENV[“RACK_ENV”] when “production” REDIS = ConnectionPool::Wrapper.new(size: 10, timeout: 5) { Redis.new(:driver => :synchrony, :host => “10.1.1.1”, :port => 6379) } when “development” REDIS = ConnectionPool::Wrapper.new(size: 10, timeout: 5) { Redis.new(:driver => :synchrony, :host => “192.168.1.2”, :port => 6379) } end

memcache

case ENV[“RACK_ENV”] when “production” Dalli.logger = logger DALLI_CACHE = ConnectionPool::Wrapper.new(size: 10, timeout: 5) { Dalli::Client.new(“10.1.1.2:11211”, {:threadsafe => true, :failover => true, :expires_in => 1.day, :compress => true}) } when “development” DALLI_CACHE = ConnectionPool::Wrapper.new(size: 10, timeout: 5) { Dalli::Client.new(“192.168.1.2:11211”, {:threadsafe => true, :failover => true, :expires_in => 1.day, :compress => true}) } end

需要使用到redis或者memcache的时候,直接使用即可,并发支持最大量为配置的size的个数,每个连接的超市时间是5秒。

但是DalliCache不支持text协议的memcache集群。

如果需要集群,则只能根据文档来解决。

issues

dalli作者的官方答复:

twemproxy does not support the memcached binary protocol.

Use an SSH tunnel instead.


Comments

Content