Class: Cache::This
- Inherits:
-
Object
- Object
- Cache::This
- Defined in:
- lib/cache/this.rb
Instance Method Summary collapse
- #clear ⇒ Object
- #count ⇒ Object (also: #size)
- #delete(name) ⇒ Object (also: #evict)
- #fetch(name, &block) ⇒ Object
- #get(name) ⇒ Object
- #get_or_set(name, timeout = nil, value = nil, &block) ⇒ Object
-
#initialize(*args) ⇒ This
constructor
A new instance of This.
- #key?(name) ⇒ Boolean (also: #has_key?)
- #to_a ⇒ Object
Constructor Details
#initialize(*args) ⇒ This
Returns a new instance of This.
6 7 8 9 10 11 12 13 14 |
# File 'lib/cache/this.rb', line 6 def initialize(*args) expiration, _ = args expiration ||= lambda { 1.hour.from_now } @expiration = expiration @cache_values ||= {} @cache_timeouts ||= {} #test = 13wr @timeout_suffix ||= '' end |
Instance Method Details
#clear ⇒ Object
42 43 44 45 46 |
# File 'lib/cache/this.rb', line 42 def clear @cache_values = {} @cache_timeouts = {} nil end |
#count ⇒ Object Also known as: size
63 64 65 |
# File 'lib/cache/this.rb', line 63 def count @cache_values.keys.size end |
#delete(name) ⇒ Object Also known as: evict
36 37 38 39 |
# File 'lib/cache/this.rb', line 36 def delete(name) @cache_timeouts.delete(timeout_key_name(name)) @cache_values.delete(name.to_sym) end |
#fetch(name, &block) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/cache/this.rb', line 28 def fetch(name, &block) result = get(name) if result.nil? and block_given? result = yield block end result end |
#get(name) ⇒ Object
24 25 26 |
# File 'lib/cache/this.rb', line 24 def get(name) get_value(name, nil, nil) end |
#get_or_set(name, timeout = nil, value = nil, &block) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/cache/this.rb', line 16 def get_or_set(name, timeout = nil, value = nil, &block) cached_value = get_value(name, timeout, value, &block) cached_value ||= value set_value(name, cached_value, &block) if cached_value.nil? or expired?(name) add_expiration(name, timeout) if expired?(name) value end |
#key?(name) ⇒ Boolean Also known as: has_key?
58 59 60 |
# File 'lib/cache/this.rb', line 58 def key?(name) @cache_values.has_key?(name.to_sym) and @cache_timeouts.has_key?(timeout_key_name(name).to_sym) end |
#to_a ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/cache/this.rb', line 48 def to_a result = [] @cache_values.keys.each_with_index do |name, index| element = [name.to_sym, get(name), timeout_for(name)] result[index] = element end #TODO: optionally sort this by timeout result end |