Class: LockManager::RedisConnection
- Inherits:
-
Connection
- Object
- Connection
- LockManager::RedisConnection
- Defined in:
- lib/lock_manager/redis_connection.rb
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Attributes inherited from Connection
Instance Method Summary collapse
-
#connect_redis(redis_server, redis_port = 6379) ⇒ Object
Create a connection to redis.
- #handle ⇒ Object
-
#initialize(options = {}) ⇒ RedisConnection
constructor
A new instance of RedisConnection.
- #key_from_host(host) ⇒ Object
- #read(host) ⇒ Object
- #remove(host) ⇒ Object
- #write(host, value) ⇒ Object
- #write_if_not_exists(host, value) ⇒ Object
Methods inherited from Connection
Constructor Details
#initialize(options = {}) ⇒ RedisConnection
Returns a new instance of RedisConnection.
7 8 9 10 11 12 |
# File 'lib/lock_manager/redis_connection.rb', line 7 def initialize( = {}) super raise ArgumentError, ':server option is mandatory for redis connections' unless [:server] @server = [:server] @port = [:port] end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
5 6 7 |
# File 'lib/lock_manager/redis_connection.rb', line 5 def port @port end |
#server ⇒ Object (readonly)
Returns the value of attribute server.
5 6 7 |
# File 'lib/lock_manager/redis_connection.rb', line 5 def server @server end |
Instance Method Details
#connect_redis(redis_server, redis_port = 6379) ⇒ Object
Create a connection to redis.
39 40 41 42 43 |
# File 'lib/lock_manager/redis_connection.rb', line 39 def connect_redis(redis_server, redis_port = 6379) redis = Redis.new(host: redis_server, port: redis_port) redis.ping redis end |
#handle ⇒ Object
34 35 36 |
# File 'lib/lock_manager/redis_connection.rb', line 34 def handle @handle ||= connect_redis(server, port) end |
#key_from_host(host) ⇒ Object
30 31 32 |
# File 'lib/lock_manager/redis_connection.rb', line 30 def key_from_host(host) "node_lock_#{host}" end |
#read(host) ⇒ Object
14 15 16 |
# File 'lib/lock_manager/redis_connection.rb', line 14 def read(host) handle.get key_from_host(host) end |
#remove(host) ⇒ Object
26 27 28 |
# File 'lib/lock_manager/redis_connection.rb', line 26 def remove(host) handle.del key_from_host(host) end |
#write(host, value) ⇒ Object
22 23 24 |
# File 'lib/lock_manager/redis_connection.rb', line 22 def write(host, value) handle.set(key_from_host(host), value) end |
#write_if_not_exists(host, value) ⇒ Object
18 19 20 |
# File 'lib/lock_manager/redis_connection.rb', line 18 def write_if_not_exists(host, value) handle.setnx(key_from_host(host), value) end |