Class: ZeevexCluster::Coordinator::Redis
- Inherits:
-
BaseKeyValStore
- Object
- BaseKeyValStore
- ZeevexCluster::Coordinator::Redis
- Defined in:
- lib/zeevex_cluster/coordinator/redis.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(key, value, options = {}) ⇒ Object
-
#cas(key, options = {}) ⇒ Object
Block is passed the current value, and returns the updated value.
- #delete(key, options = {}) ⇒ Object
- #get(key, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Redis
constructor
A new instance of Redis.
- #set(key, value, options = {}) ⇒ Object
Methods inherited from BaseKeyValStore
Methods included from Util::Logging
Constructor Details
#initialize(options = {}) ⇒ Redis
Returns a new instance of Redis.
13 14 15 16 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 13 def initialize( = {}) super @client ||= ::Redis.new :host => @server, :port => @port end |
Class Method Details
.setup ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 5 def self.setup unless @setup require 'redis' BaseKeyValStore.setup @setup = true end end |
Instance Method Details
#add(key, value, options = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 18 def add(key, value, = {}) if @client.setnx(to_key(key), serialize_value(value, is_raw?())) @client.expire to_key(key), .fetch(:expiration, @expiration) true else false end rescue ::Redis::CannotConnectError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#cas(key, options = {}) ⇒ Object
Block is passed the current value, and returns the updated value.
Block can raise DontChange to simply exit the block without updating.
returns nil for no value returns false for failure (somebody else set) returns true for success
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 52 def cas(key, = {}) key = to_key(key) @client.unwatch @client.watch key orig_val = @client.get key return nil if orig_val.nil? expiration = .fetch(:expiration, @expiration) newval = serialize_value(yield(deserialize_value(orig_val, is_raw?())), is_raw?()) res = @client.multi do if expiration @client.setex key, expiration, newval else @client.set key, newval end end @client.unwatch case res when nil then false when Array then true else raise "Unhandled return value from multi - #{res.inspect}" end rescue ZeevexCluster::Coordinator::DontChange => e false rescue ::Redis::CannotConnectError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#delete(key, options = {}) ⇒ Object
37 38 39 40 41 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 37 def delete(key, = {}) @client.del(to_key(key)) == 1 rescue ::Redis::CannotConnectError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#get(key, options = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 81 def get(key, = {}) deserialize_value(@client.get(to_key(key)), is_raw?()) rescue ::Redis::CannotConnectError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#set(key, value, options = {}) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/zeevex_cluster/coordinator/redis.rb', line 29 def set(key, value, = {}) status( @client.setex(to_key(key), .fetch(:expiration, @expiration), serialize_value(value, is_raw?())) ) == STATUS_OK rescue ::Redis::CannotConnectError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |