Module: PrestoCache::API

Included in:
Memory, MongoDB
Defined in:
lib/presto/cache/api.rb

Constant Summary

KEY =
"key".freeze
VAL =
"val".freeze

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

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

Returns:

  • found related store

Raises:

  • NoMethodError if no store 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)

Note:

# list of actions executing callbacks:

- new
- insert
- set
- get
- update
- delete
- truncate
- drop
Note:

all args will be passed back to callback.

set an callback to be executed before/after action(s).

Examples:

store.before :new do |name|
  p 'creating %s store' % name
end

store.new :articles
"creating articles store"

store.before :set do |key, val|
  p 'inserting: %s => %s' % [key, val]
end
store['alert'] = 'page updated'
"inserting: alert => page updated"

Parameters:

  • actions (Array)

    list of actions. if no args given, callback to be executed before/after any action.



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)

Note:

# list of actions executing callbacks:

- new
- insert
- set
- get
- update
- delete
- truncate
- drop
Note:

all args will be passed back to callback.

set an callback to be executed before/after action(s).

Examples:

store.before :new do |name|
  p 'creating %s store' % name
end

store.new :articles
"creating articles store"

store.before :set do |key, val|
  p 'inserting: %s => %s' % [key, val]
end
store['alert'] = 'page updated'
"inserting: alert => page updated"

Parameters:

  • actions (Array)

    list of actions. if no args given, callback to be executed before/after any action.



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

Parameters:

  • name (Symbol)
  • create_new_if_missing (Boolean) (defaults to: false)

    if true and no store found by given name, will create new one using given name.

Returns:

  • found or new store or nil



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.

Parameters:

  • key (Symbol, String)


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.

Parameters:

  • recursive (Boolean) (defaults to: nil)

    if true, it will act recursively, truncating all children and children of children.

Returns:

  • (nil, true)


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?

Returns:

  • (Boolean)


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.

Examples:

db = Mongo::Connection.new("localhost", 20_000).db("presto-cache")
store = ::PrestoCache::MongoDB.new db
store['item1'] = 'one'
store['item2'] = 'two'
item1 = store.filter key: 'item1'
item2 = store.filter val: 'two'
items = store.filter key: /item/

p item1.first
{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b614'), "key"=>"item1", "val"=>"one"}

p item2.first
{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b615'), "key"=>"item2", "val"=>"two"}

items.each do |i|
  p i
end
{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b614'), "key"=>"item1", "val"=>"one"}
{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b615'), "key"=>"item2", "val"=>"two"}

p items.to_a
[{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b614'), "key"=>"item1", "val"=>"one"}, {"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b615'), "key"=>"item2", "val"=>"two"}]

store = ::PrestoCache::Memory.new
store['item1'] = 'one'
store['item2'] = 'two'
item1 = store.filter key: 'item1'
item2 = store.filter val: 'two'
items = store.filter key: /item/

p item1.first
{"key"=>"item1", "val"=>"one"}

p item2.first
{"key"=>"item2", "val"=>"two"}

items.each do |i|
  p i
end
{"key"=>"item1", "val"=>"one"}
{"key"=>"item2", "val"=>"two"}

Parameters:

  • filters (Hash) (defaults to: {})

Options Hash (filters):

  • :key (String, Regexp)
  • :val (String, Regexp)

Returns:

  • (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.

Parameters:

  • args (Array)

Returns:

  • first item from array returned by #filter or nil if no items found.



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.

Examples:

cache = ::PrestoCache::Memory.new
page = cache.get '/about-us.html' do
  Page.first(path: '/about-us.html')
end

Parameters:

  • key (Symbol, String)
  • proc (Proc)

Returns:

  • Object - if item found or proc given. nil - if no item found and no proc given.



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

Returns:

  • (API)

    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.

Examples:

global_cache = ::PrestoCache::Memory.new
page_cache = global_cache.new(:page)
# now you can access new store by assigned var or by:
#  - global_cache.page
#  - global_cache.db(:page)

Parameters:

  • name (Symbol, String)

    names are symbolized, so if you use a string as 'name', you'll can access it by **store.db(:name)**

Returns:

  • new store



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.

Parameters:

  • key (Symbol, String)
  • val

Returns:

  • val



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.

Parameters:

  • recursive (Boolean) (defaults to: nil)

    if true, it will act recursively, truncating all children and children of children.

Returns:

  • (nil, true)


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.

Examples:

store['item1'] = 'one'
store['item2'] = 'two'

p store.filter.to_a
[{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b614'), "key"=>"item1", "val"=>"one"}, {"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b615'), "key"=>"item2", "val"=>"two"}]

store.update 'item1' => 'oneUpdated', 'item2' => 'twoUpdated'
p store.filter.to_a
[{"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b614'), "key"=>"item1", "val"=>"oneUpdated"}, {"_id"=>BSON::ObjectId('4edee68a73726b0d24f2b615'), "key"=>"item2", "val"=>"twoUpdated"}]

Parameters:

  • hash (Hash)

    containing key of item to be updated and new value.

Returns:

  • true - if all updates passed. false - if at least one operation failed.



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