Class: Boogaloo::Cache::Temporary
- Defined in:
- lib/boogaloo/cache/temporary.rb
Overview
A temporary cache in which objects can expire and are subsequently deleted when they do. The frequency at which the cache is checked for objects to delete is determined by the check_frequency parameter.
See Boogaloo::Cache::Base for common cache methods.
Instance Method Summary collapse
-
#__times_size ⇒ Object
:nodoc: Only used by unit test.
-
#add(namespace, key, obj, expires) ⇒ Object
Add an object to the cache only if it doesn't already exist.
-
#get(namespace, key) ⇒ Object
Get an object from the cache.
-
#initialize(name, config) ⇒ Temporary
constructor
Initialize a new temporary cache instance.
-
#pull(namespace, key) ⇒ Object
Pull and object from the cache (get and delete).
- #reset ⇒ Object
-
#set(namespace, key, obj, expires) ⇒ Object
Add an object to the cache, overwriting if one already exists.
Methods inherited from Base
#inspect_keys, #stat_all, #stat_hits, #stat_misses, #stat_namespaces, #stat_objects, #stat_reset_at
Constructor Details
#initialize(name, config) ⇒ Temporary
Initialize a new temporary cache instance.
Parameters:
nameThe name of the cache
configA Hash containing configuration for the cache.
Configuration options:
check_frequencyThe frequency in seconds at which the cache will check for objects to delete.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/boogaloo/cache/temporary.rb', line 18 def initialize(name, config) super Thread.new { while true sleep @config['check_frequency'].to_f @global_store.keys.each do |key| times_key = "#{key}_#{@global_store[key].object_id}" if @times[times_key][:time] <= Time.now @global_store.delete(key) @times.delete(key) end end @namespace_store.keys.each do |nskey| @namespace_store[nskey].keys.each do |key| times_key = "#{nskey}_#{key}_#{@namespace_store[nskey][key].object_id}" if @times[times_key][:time] <= Time.now @namespace_store[nskey].delete(key) @times.delete(times_key) end end end end } end |
Instance Method Details
#__times_size ⇒ Object
:nodoc: Only used by unit test.
120 121 122 123 124 |
# File 'lib/boogaloo/cache/temporary.rb', line 120 def __times_size #:nodoc: Only used by unit test. @times.size end |
#add(namespace, key, obj, expires) ⇒ Object
Add an object to the cache only if it doesn't already exist.
Parameters:
namespaceThe namespace in which the object will exist. Use nil if global namespace is desired.
keyA uniqeue identifier for the object relative to the given namespace.
objThe object to add to the cache.
expiresThe time in seconds which the object should exist in the cache without being touched before it is deleted.
71 72 73 74 75 76 77 |
# File 'lib/boogaloo/cache/temporary.rb', line 71 def add(namespace, key, obj, expires) check_expires(expires) super(namespace, key, obj) set_expiration(namespace, key, obj, expires) end |
#get(namespace, key) ⇒ Object
Get an object from the cache.
Parameters:
namespaceThe namespace to get the object from. Use nil if global namespace should be used.
keyA uniqeue identifier for the object relative to the given namespace.
99 100 101 102 103 104 105 |
# File 'lib/boogaloo/cache/temporary.rb', line 99 def get(namespace, key) ret = super reset_expiration(namespace, key, ret) if not ret.nil? ret end |
#pull(namespace, key) ⇒ Object
Pull and object from the cache (get and delete).
Parameters:
namespaceThe namespace to get the object from. Use nil if global namespace should be used.
keyA uniqeue identifier for the object relative to the given namespace.
112 113 114 115 116 117 118 |
# File 'lib/boogaloo/cache/temporary.rb', line 112 def pull(namespace, key) ret = super @times.delete(format_times_key(namespace, key, ret)) if not ret.nil? ret end |
#reset ⇒ Object
126 127 128 129 130 131 |
# File 'lib/boogaloo/cache/temporary.rb', line 126 def reset @times = {} super end |
#set(namespace, key, obj, expires) ⇒ Object
Add an object to the cache, overwriting if one already exists.
Parameters:
namespaceThe namespace in which the object will exist. Use nil if global namespace should be used.
keyA uniqeue identifier for the object relative to the given namespace.
objThe object to add to the cache.
expiresThe time in seconds which the object should exist in the cache without being touched before it is deleted.
86 87 88 89 90 91 92 |
# File 'lib/boogaloo/cache/temporary.rb', line 86 def set(namespace, key, obj, expires) check_expires(expires) super(namespace, key, obj) set_expiration(namespace, key, obj, expires) end |