Class: RedisConnectionPool
- Inherits:
-
Object
- Object
- RedisConnectionPool
- Defined in:
- lib/cache_store_redis/redis_connection_pool.rb
Overview
This class is used to define a pool of re-usable redis connections.
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#connections ⇒ Object
readonly
This method is called to get an array of idle connections in this pool.
-
#namespace ⇒ Object
readonly
This method is called to get the namespace for redis keys.
-
#queue ⇒ Object
readonly
This method is called to get the idle connection queue for this pool.
Instance Method Summary collapse
-
#check_in(connection) ⇒ Object
This method is called to checkin a connection to the pool after use.
-
#check_out ⇒ Object
This method is called to checkout a connection from the pool before use.
-
#fetch_connection ⇒ Object
This method is called to fetch a connection from the queue or create a new connection if no idle connections are available.
-
#initialize(namespace = nil, config = nil) ⇒ RedisConnectionPool
constructor
A new instance of RedisConnectionPool.
- #shutdown ⇒ Object
-
#with_connection ⇒ Object
This method is called to use a connection from this pool.
Constructor Details
#initialize(namespace = nil, config = nil) ⇒ RedisConnectionPool
Returns a new instance of RedisConnectionPool.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
3 4 5 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 3 def config @config end |
#connections ⇒ Object (readonly)
This method is called to get an array of idle connections in this pool.
64 65 66 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 64 def connections @connections end |
#namespace ⇒ Object (readonly)
This method is called to get the namespace for redis keys.
22 23 24 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 22 def namespace @namespace end |
#queue ⇒ Object (readonly)
This method is called to get the idle connection queue for this pool.
25 26 27 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 25 def queue @queue end |
Instance Method Details
#check_in(connection) ⇒ Object
This method is called to checkin a connection to the pool after use.
47 48 49 50 51 52 53 |
# 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 ⇒ Object
This method is called to checkout a connection from the pool before use.
36 37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
This method is called to fetch a connection from the queue or create a new connection if no idle connections are available.
29 30 31 32 33 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 29 def fetch_connection queue.pop(true) rescue StandardError RedisConnection.new(config) end |
#shutdown ⇒ Object
66 67 68 69 70 71 72 73 |
# 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 ⇒ Object
This method is called to use a connection from this pool.
56 57 58 59 60 61 |
# 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 |