Module: PrestoCache::API
Constant Summary
- KEY =
"key".freeze
- VAL =
"val".freeze
Instance Attribute Summary (collapse)
-
- (Object) store
readonly
Returns the value of attribute store.
Instance Method Summary (collapse)
-
- (Object) [](key)
find a item by key.
-
- (Object) []=(key, val)
insert new item or update found one.
-
- (Object) after(*actions, &proc)
set an callback to be executed before/after action(s).
-
- (Object) before(*actions, &proc)
set an callback to be executed before/after action(s).
-
- (Object) db(name, create_new_if_missing = false)
return earlier created store or create new one.
-
- (Object) dbs
list of related stores.
-
- (Object) delete(key)
delete an item by key.
-
- (nil, true) drop(recursive = nil)
removing an entire collection.
-
- (Object) each_key
same as Hash#each_key.
-
- (Object) each_pair
(also: #each)
same as Hash#each_pair.
-
- (Object) each_value
same as Hash#each_value.
- - (Boolean) empty?
-
- (Array) filter(filters = {})
filter collection by key and/or value.
-
- (Object) first(*args)
a simple wrapper around #filter.
-
- (Object) get(key, &proc)
find item by given key.
-
- (API) initialize(*args)
A new instance of API.
-
- (Object) keys
same as Hash#keys.
-
- (Object) method_missing(name)
searching through related stores and return first found.
-
- (Object) new(name)
creates a new store, related to current one.
-
- (Object) set(key, val)
insert new item or if an item with given key already exists, update it.
-
- (Object) size
(also: #count)
items count.
-
- (nil, true) truncate(recursive = nil)
remove all items from an collection.
-
- (Object) update(hash)
updates an arbitrary amount of items by given keys.
-
- (Object) values
same as Hash#values.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
- (Object) method_missing(name)
searching through related stores and return first found
57 58 59 |
# File 'lib/presto/cache/api.rb', line 57 def method_missing name db(name) || raise(NoMethodError, name.to_s) end |
Instance Attribute Details
- (Object) store (readonly)
Returns the value of attribute store
7 8 9 |
# File 'lib/presto/cache/api.rb', line 7 def store @store end |
Instance Method Details
- (Object) [](key)
find a item by key
295 296 297 |
# File 'lib/presto/cache/api.rb', line 295 def [] key get key end |
- (Object) []=(key, val)
insert new item or update found one
290 291 292 |
# File 'lib/presto/cache/api.rb', line 290 def []= key, val set key, val end |
- (Object) after(*actions, &proc)
# list of actions executing callbacks:
- new
- insert
- set
- get
- update
- delete
- truncate
- drop
all args will be passed back to callback.
set an callback to be executed before/after action(s).
279 280 281 282 |
# File 'lib/presto/cache/api.rb', line 279 def after *actions, &proc actions = [:*] if actions.size == 0 actions.each { |a| @callbacks[:z][a] = proc } end |
- (Object) before(*actions, &proc)
# list of actions executing callbacks:
- new
- insert
- set
- get
- update
- delete
- truncate
- drop
all args will be passed back to callback.
set an callback to be executed before/after action(s).
273 274 275 276 |
# File 'lib/presto/cache/api.rb', line 273 def before *actions, &proc actions = [:*] if actions.size == 0 actions.each { |a| @callbacks[:a][a] = proc } end |
- (Object) db(name, create_new_if_missing = false)
return earlier created store or create new one
45 46 47 48 49 50 51 52 |
# File 'lib/presto/cache/api.rb', line 45 def db name, create_new_if_missing = false if db = @children[name] wipe = db.store ? db.store.dropped? : true wipe && @children.delete(name) && db = nil end db ||= new(name) if create_new_if_missing db end |
- (Object) dbs
list of related stores
285 286 287 |
# File 'lib/presto/cache/api.rb', line 285 def dbs @children.keys end |
- (Object) delete(key)
delete an item by key.
204 205 206 207 208 209 210 211 212 |
# File 'lib/presto/cache/api.rb', line 204 def delete key persisted = false execute_callbacks :a, __method__, key @mutex.synchronize do persisted = @store.delete(key.to_s) end execute_callbacks :z, __method__, key persisted end |
- (nil, true) drop(recursive = nil)
removing an entire collection.
232 233 234 235 236 237 238 239 240 |
# File 'lib/presto/cache/api.rb', line 232 def drop recursive = nil status = nil execute_callbacks :a, __method__, recursive recursive && @children.each_pair { |n, s| @mutex.synchronize { status = s.drop(recursive) && @children.delete(n) } } @mutex.synchronize { @store.drop } @store = nil execute_callbacks :z, __method__, recursive status end |
- (Object) each_key
same as Hash#each_key
315 316 317 |
# File 'lib/presto/cache/api.rb', line 315 def each_key filter.map { |i| yield i['key'] } end |
- (Object) each_pair Also known as: each
same as Hash#each_pair
320 321 322 |
# File 'lib/presto/cache/api.rb', line 320 def each_pair filter.map { |i| yield i['key'], i['val'] } end |
- (Object) each_value
same as Hash#each_value
310 311 312 |
# File 'lib/presto/cache/api.rb', line 310 def each_value filter.map { |i| yield i['val'] } end |
- (Boolean) empty?
333 334 335 |
# File 'lib/presto/cache/api.rb', line 333 def empty? @store.size == 0 end |
- (Array) filter(filters = {})
filter collection by key and/or value. returns an array of found items or empty array.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/presto/cache/api.rb', line 153 def filter filters = {} filter = {} if key = filters[:key] || filters['key'] || filters[:k] || filters['k'] filter[KEY] = key end if val = filters[:val] || filters['val'] || filters[:v] || filters['v'] filter[VAL] = val end @store.filter filter end |
- (Object) first(*args)
a simple wrapper around #filter.
169 170 171 |
# File 'lib/presto/cache/api.rb', line 169 def first *args filter(*args).first end |
- (Object) get(key, &proc)
find item by given key. if no item found and proc given, it will create new item using as val the value returned by proc.
93 94 95 96 97 98 99 100 101 |
# File 'lib/presto/cache/api.rb', line 93 def get key, &proc execute_callbacks :a, __method__, key if val = @store[key.to_s] return load(val) end execute_callbacks :z, __method__, key return set(key, proc.call) if proc nil end |
- (API) initialize(*args)
A new instance of API
9 10 11 12 13 14 15 16 17 |
# File 'lib/presto/cache/api.rb', line 9 def initialize *args @args = args @mutex = Mutex.new @mutex.synchronize do @store = initialize_store *@args end @children = Hash.new @callbacks = {a: Hash.new, z: Hash.new} end |
- (Object) keys
same as Hash#keys
300 301 302 |
# File 'lib/presto/cache/api.rb', line 300 def keys @store.keys end |
- (Object) new(name)
creates a new store, related to current one.
31 32 33 34 35 36 37 |
# File 'lib/presto/cache/api.rb', line 31 def new name execute_callbacks :a, __method__, name store = self.class.new *@args, name @children[name.to_sym] = store execute_callbacks :z, __method__, name store end |
- (Object) set(key, val)
insert new item or if an item with given key already exists, update it.
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/presto/cache/api.rb', line 66 def set key, val execute_callbacks :a, __method__, key, val persisted = false @mutex.synchronize do if @store[key.to_s] = dump(val) persisted = true end end execute_callbacks :z, __method__, key, val val if persisted end |
- (Object) size Also known as: count
items count
327 328 329 |
# File 'lib/presto/cache/api.rb', line 327 def size @store.size end |
- (nil, true) truncate(recursive = nil)
remove all items from an collection.
219 220 221 222 223 224 225 226 |
# File 'lib/presto/cache/api.rb', line 219 def truncate recursive = nil status = nil execute_callbacks :a, __method__, recursive recursive && @children.each_value { |s| @mutex.synchronize { status = s.truncate(recursive) } } @mutex.synchronize { @store.truncate } execute_callbacks :z, __method__, recursive status end |
- (Object) update(hash)
updates an arbitrary amount of items by given keys.
191 192 193 194 195 196 197 198 199 |
# File 'lib/presto/cache/api.rb', line 191 def update hash persisted = true execute_callbacks :a, __method__, hash @mutex.synchronize do hash.each_pair { |k, v| (@store[k.to_s] = v) || persisted = false } end execute_callbacks :z, __method__, hash persisted end |
- (Object) values
same as Hash#values
305 306 307 |
# File 'lib/presto/cache/api.rb', line 305 def values keys.each.map { |k| get(k) } end |