class RedisConnectionPool

This class is used to define a pool of re-usable redis connections.

Attributes

config[RW]
connections[R]

This method is called to get an array of idle connections in this pool.

namespace[R]

This method is called to get the namespace for redis keys.

queue[R]

This method is called to get the idle connection queue for this pool.

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(&:expired?).each(&:close)
      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 47
def check_in(connection)
  connection.close if connection.expired?
  @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 36
def check_out
  connection = nil
  @mutex.synchronize do
    connection = fetch_connection
    connections.delete(connection)
    connection.open
  end
  connection
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 29
def fetch_connection
  queue.pop(true)
rescue StandardError
  RedisConnection.new(config)
end
shutdown() click to toggle source
# File lib/cache_store_redis/redis_connection_pool.rb, line 66
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 56
def with_connection
  connection = check_out
  yield connection.client
ensure
  check_in(connection) if connection
end