Class: MemcachedServer::Memcache
- Inherits:
-
Object
- Object
- MemcachedServer::Memcache
- Defined in:
- lib/memcached-server/memcache.rb
Overview
The class used to process Memcache commands and store the Memcache server data
Instance Attribute Summary collapse
-
#storage ⇒ Hash
readonly
The Hash map used to store the Memcache server data.
Instance Method Summary collapse
-
#add(key, flags, exptime, bytes, data_block) ⇒ String
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage only if it isn’t already stored Depends on #store_item method.
-
#append(key, bytes, new_data) ⇒ String
Appends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>.
-
#cas(key, flags, exptime, bytes, cas_id, data_block) ⇒ String
Check and set operation that stores a MemcachedServer::Item only if no one else has updated since it was last fetched Depends on #store_item method.
-
#get(keys) ⇒ [MemcachedServer::Item]
Retrieves the items corresponding to the given keys from @storage.
-
#initialize ⇒ Memcache
constructor
A new instance of Memcache.
-
#prepend(key, bytes, new_data) ⇒ String
Prepends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>.
-
#purge_keys ⇒ Object
Deletes @storage items if they are expired.
-
#replace(key, flags, exptime, bytes, data_block) ⇒ String
Replaces a MemcachedServer::Item stored in @storage, with a new one with the attributes recieved by param Depends on #store_item method.
-
#set(key, flags, exptime, bytes, data_block) ⇒ String
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Depends on #store_item method.
-
#store_item(key, flags, exptime, bytes, data_block) ⇒ Object
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Before storing the MemcachedServer::Item it updates it’s cas_id.
Constructor Details
#initialize ⇒ Memcache
Returns a new instance of Memcache.
14 15 16 17 18 |
# File 'lib/memcached-server/memcache.rb', line 14 def initialize() @storage = Hash.new() end |
Instance Attribute Details
#storage ⇒ Hash (readonly)
The Hash map used to store the Memcache server data
12 13 14 |
# File 'lib/memcached-server/memcache.rb', line 12 def storage @storage end |
Instance Method Details
#add(key, flags, exptime, bytes, data_block) ⇒ String
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage only if it isn’t already stored Depends on #store_item method
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/memcached-server/memcache.rb', line 65 def add(key, flags, exptime, bytes, data_block) purge_keys() return Reply::NOT_STORED if @storage.key?(key) store_item(key, flags, exptime, bytes, data_block) return Reply::STORED end |
#append(key, bytes, new_data) ⇒ String
Appends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/memcached-server/memcache.rb', line 100 def append(key, bytes, new_data) purge_keys() return Reply::NOT_STORED unless @storage.key?(key) item = @storage[key] item.lock.synchronize do item.data_block.concat(new_data) item.bytes += bytes end item.update_cas_id() return Reply::STORED end |
#cas(key, flags, exptime, bytes, cas_id, data_block) ⇒ String
Check and set operation that stores a MemcachedServer::Item only if no one else has updated since it was last fetched Depends on #store_item method
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/memcached-server/memcache.rb', line 154 def cas(key, flags, exptime, bytes, cas_id, data_block) purge_keys() return Reply::NOT_FOUND unless @storage.key?(key) item = @storage[key] item.lock.synchronize do return Reply::EXISTS if cas_id != item.cas_id end store_item(key, flags, exptime, bytes, data_block) return Reply::STORED end |
#get(keys) ⇒ [MemcachedServer::Item]
Retrieves the items corresponding to the given keys from @storage
31 32 33 34 35 36 37 38 |
# File 'lib/memcached-server/memcache.rb', line 31 def get(keys) purge_keys() items = @storage.values_at(*keys) return items end |
#prepend(key, bytes, new_data) ⇒ String
Prepends <new_data> to a MemcachedServer::Item data_block that is stored in @storage with an associated <key>
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/memcached-server/memcache.rb', line 125 def prepend(key, bytes, new_data) purge_keys() return Reply::NOT_STORED unless @storage.key?(key) item = @storage[key] item.lock.synchronize do item.data_block.prepend(new_data) item.bytes += bytes end item.update_cas_id() return Reply::STORED end |
#purge_keys ⇒ Object
Deletes @storage items if they are expired
21 22 23 24 25 |
# File 'lib/memcached-server/memcache.rb', line 21 def purge_keys() @storage.delete_if { | key, item | item.expired? } end |
#replace(key, flags, exptime, bytes, data_block) ⇒ String
Replaces a MemcachedServer::Item stored in @storage, with a new one with the attributes recieved by param Depends on #store_item method
85 86 87 88 89 90 91 92 |
# File 'lib/memcached-server/memcache.rb', line 85 def replace(key, flags, exptime, bytes, data_block) return Reply::NOT_STORED unless @storage.key?(key) store_item(key, flags, exptime, bytes, data_block) return Reply::STORED end |
#set(key, flags, exptime, bytes, data_block) ⇒ String
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Depends on #store_item method
49 50 51 52 53 54 |
# File 'lib/memcached-server/memcache.rb', line 49 def set(key, flags, exptime, bytes, data_block) store_item(key, flags, exptime, bytes, data_block) return Reply::STORED end |
#store_item(key, flags, exptime, bytes, data_block) ⇒ Object
Stores a MemcachedServer::Item, with the attributes recieved by param, in @storage Before storing the MemcachedServer::Item it updates it’s cas_id
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/memcached-server/memcache.rb', line 180 def store_item(key, flags, exptime, bytes, data_block) item = Item.new(key, flags, exptime, bytes, data_block) item.update_cas_id() item.lock.synchronize do @storage.store(key, item) unless item.expired?() end end |