Class: Pinky::Cache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/pinky/cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item_methods, capacity) ⇒ Cache

Returns a new instance of Cache.



14
15
16
17
18
# File 'lib/pinky/cache.rb', line 14

def initialize(item_methods, capacity)
  @capacity = capacity
  @item_methods = [item_methods].flatten.sort
  @name = self.class.name_for(@item_methods)
end

Instance Attribute Details

#capacityObject (readonly)

Returns the value of attribute capacity.



5
6
7
# File 'lib/pinky/cache.rb', line 5

def capacity
  @capacity
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/pinky/cache.rb', line 5

def name
  @name
end

Class Method Details

.name_for(*methods) ⇒ Object



9
10
11
# File 'lib/pinky/cache.rb', line 9

def name_for(*methods)
  "cache_by_#{methods.flatten.sort.join("_and_")}"
end

Instance Method Details

#clear_cacheObject



38
39
40
# File 'lib/pinky/cache.rb', line 38

def clear_cache
  @cache = nil
end

#query(query_hash = {}) ⇒ Object



33
34
35
36
# File 'lib/pinky/cache.rb', line 33

def query(query_hash = {})
  key = query_hash.sort.map { |k,v| v }.join '.'
  cache.get(key)
end

#queryable_for?(query_hash) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pinky/cache.rb', line 20

def queryable_for?(query_hash)
  query_hash.keys.sort == @item_methods
end

#update_with(item, action = :create) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/pinky/cache.rb', line 24

def update_with(item, action = :create)
  key = key_for item
  if action == :destroy
    cache.delete key
  else
    cache.put(key, item)
  end
end