Class: ZeevexCluster::Coordinator::Memcached
- Inherits:
-
BaseKeyValStore
- Object
- BaseKeyValStore
- ZeevexCluster::Coordinator::Memcached
- Defined in:
- lib/zeevex_cluster/coordinator/memcached.rb
Class Method Summary collapse
Instance Method Summary collapse
- #add(key, value, options = {}) ⇒ Object
- #append(key, val, options = {}) ⇒ Object
-
#cas(key, options = {}, &block) ⇒ Object
Block is passed the current value, and returns the updated value.
- #delete(key, options = {}) ⇒ Object
- #get(key, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ Memcached
constructor
A new instance of Memcached.
- #prepend(key, val, options = {}) ⇒ Object
- #push_to_queue(key, object, options = {}) ⇒ Object
- #set(key, value, options = {}) ⇒ Object
Methods inherited from BaseKeyValStore
Methods included from Util::Logging
Constructor Details
#initialize(options = {}) ⇒ Memcached
Returns a new instance of Memcached.
14 15 16 17 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 14 def initialize( = {}) super @client ||= MemCache.new "#@server:#@port" end |
Class Method Details
.setup ⇒ Object
5 6 7 8 9 10 11 12 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 5 def self.setup unless @setup require 'memcache' BaseKeyValStore.setup @setup = true end end |
Instance Method Details
#add(key, value, options = {}) ⇒ Object
19 20 21 22 23 24 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 19 def add(key, value, = {}) status( @client.add(to_key(key), serialize_value(value, [:raw]), .fetch(:expiration, @expiration), raw?) ) == STORED rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#append(key, val, options = {}) ⇒ Object
73 74 75 76 77 78 79 80 81 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 73 def append(key, val, = {}) val = serialize_value(val, [:raw]) key = to_key(key) status( @client.append(key, val) ) == STORED || status( @client.add(key, val, .fetch(:expiration, @expiration), true) ) == STORED || status( @client.append(key, val) ) == STORED rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#cas(key, options = {}, &block) ⇒ 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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 46 def cas(key, = {}, &block) res = @client.cas(to_key(key), .fetch(:expiration, @expiration), raw?) do |inval| serialize_value(yield(deserialize_value(inval, [:raw])), [:raw]) end case status(res) when nil then nil when EXISTS then false when STORED then true else raise "Unhandled status code: #{res}" end rescue ZeevexCluster::Coordinator::DontChange false rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#delete(key, options = {}) ⇒ Object
33 34 35 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 33 def delete(key, = {}) status( @client.delete(to_key(key)) ) == DELETED end |
#get(key, options = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 62 def get(key, = {}) val = @client.get(to_key(key), raw?) if val && ![:raw] deserialize_value(val) else val end rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#prepend(key, val, options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 83 def prepend(key, val, = {}) val = serialize_value(val, [:raw]) key = to_key(key) status( @client.prepend(key, val) ) == STORED || status( @client.add(key, val, .fetch(:expiration, @expiration), true) ) == STORED || status( @client.prepend(key, val) ) == STORED rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |
#push_to_queue(key, object, options = {}) ⇒ Object
93 94 95 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 93 def push_to_queue(key, object, = {}) end |
#set(key, value, options = {}) ⇒ Object
26 27 28 29 30 31 |
# File 'lib/zeevex_cluster/coordinator/memcached.rb', line 26 def set(key, value, = {}) status( @client.set(to_key(key), serialize_value(value, [:raw]), .fetch(:expiration, @expiration), raw?) ) == STORED rescue MemCache::MemCacheError raise ZeevexCluster::Coordinator::ConnectionError.new 'Connection error', $! end |