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.
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.
-
#connections ⇒ Object
This method is called to get an array of idle connections in this pool.
-
#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.
-
#namespace ⇒ Object
This method is called to get the namespace for redis keys.
-
#queue ⇒ Object
This method is called to get the idle connection queue for this pool.
- #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 20 21 |
# 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 |
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 |
Instance Method Details
#check_in(connection) ⇒ Object
This method is called to checkin a connection to the pool after use.
53 54 55 56 57 58 59 60 61 |
# 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 ⇒ Object
This method is called to checkout a connection from the pool before use.
42 43 44 45 46 47 48 49 50 |
# 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 ⇒ Object
This method is called to get an array of idle connections in this pool.
72 73 74 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 72 def connections @connections 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.
35 36 37 38 39 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 35 def fetch_connection queue.pop(true) rescue RedisConnection.new(config) end |
#namespace ⇒ Object
This method is called to get the namespace for redis keys.
24 25 26 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 24 def namespace @namespace end |
#queue ⇒ Object
This method is called to get the idle connection queue for this pool.
29 30 31 |
# File 'lib/cache_store_redis/redis_connection_pool.rb', line 29 def queue @queue end |
#shutdown ⇒ Object
76 77 78 79 80 81 82 83 |
# 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 ⇒ Object
This method is called to use a connection from this pool.
64 65 66 67 68 69 |
# 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 |