class RedisConnectionPool
This class is used to define a pool of re-usable redis connections.
Attributes
config[RW]
Public Class Methods
new(namespace = nil, config = nil)
click to toggle source
# File lib/cache_store_redis/redis_connection_pool.rb, line 5 def initialize(namespace = nil, config = nil) @namespace = namespace @config = config @queue = Queue.new @connections = [] @mutex = Mutex.new @monitor_thread = Thread.new do loop do sleep(1) @mutex.synchronize do connections.select { |con| con.expired? }.each do |con| con.close end end end end end
Public Instance Methods
check_in(connection)
click to toggle source
This method is called to checkin a connection to the pool after use.
# File lib/cache_store_redis/redis_connection_pool.rb, line 53 def check_in(connection) if connection.expired? connection.close end @mutex.synchronize do connections.push(connection) queue.push(connection) end end
check_out()
click to toggle source
This method is called to checkout a connection from the pool before use.
# File lib/cache_store_redis/redis_connection_pool.rb, line 42 def check_out connection = nil @mutex.synchronize do connection = fetch_connection connections.delete(connection) connection.open end connection end
connections()
click to toggle source
This method is called to get an array of idle connections in this pool.
# File lib/cache_store_redis/redis_connection_pool.rb, line 72 def connections @connections end
fetch_connection()
click to toggle source
This method is called to fetch a connection from the queue or create a new connection if no idle connections are available.
# File lib/cache_store_redis/redis_connection_pool.rb, line 35 def fetch_connection queue.pop(true) rescue RedisConnection.new(config) end
namespace()
click to toggle source
This method is called to get the namespace for redis keys.
# File lib/cache_store_redis/redis_connection_pool.rb, line 24 def namespace @namespace end
queue()
click to toggle source
This method is called to get the idle connection queue for this pool.
# File lib/cache_store_redis/redis_connection_pool.rb, line 29 def queue @queue end
shutdown()
click to toggle source
# File lib/cache_store_redis/redis_connection_pool.rb, line 76 def shutdown connections.each do |con| con.client.close end @connections = [] @monitor_thread.kill @queue = Queue.new end
with_connection() { |client| ... }
click to toggle source
This method is called to use a connection from this pool.
# File lib/cache_store_redis/redis_connection_pool.rb, line 64 def with_connection connection = check_out return yield connection.client ensure check_in(connection) end