Module: TmpCache::Prototype::Methods
- Defined in:
- lib/tmp_cache/cache.rb
Instance Method Summary collapse
- #cache ⇒ Object
- #each(&block) ⇒ Object
- #gc ⇒ Object
- #get(key) ⇒ Object
- #keys ⇒ Object
- #reset ⇒ Object
- #set(key, value, expire = nil) ⇒ Object
- #values ⇒ Object
Instance Method Details
#cache ⇒ Object
16 17 18 |
# File 'lib/tmp_cache/cache.rb', line 16 def cache @cache ||= Hash.new{|h,k| h = {:expire => 0, :value => nil}} end |
#each(&block) ⇒ Object
58 59 60 61 62 63 |
# File 'lib/tmp_cache/cache.rb', line 58 def each(&block) gc cache.each do |k,v| yield k, v[:value] end end |
#gc ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/tmp_cache/cache.rb', line 36 def gc cache.each do |k,c| if c[:expire] != nil and Time.now.to_i > c[:expire].to_i cache.delete k end end end |
#get(key) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/tmp_cache/cache.rb', line 28 def get(key) if cache[key][:expire] < Time.now.to_i return cache[key][:value] = nil else return cache[key][:value] end end |
#keys ⇒ Object
48 49 50 51 |
# File 'lib/tmp_cache/cache.rb', line 48 def keys gc cache.keys end |
#reset ⇒ Object
44 45 46 |
# File 'lib/tmp_cache/cache.rb', line 44 def reset @cache = nil end |
#set(key, value, expire = nil) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/tmp_cache/cache.rb', line 20 def set(key, value, expire=nil) cache[key] = { :value => value, :expire => expire ? Time.now.to_i+expire.to_i : nil } value end |
#values ⇒ Object
53 54 55 56 |
# File 'lib/tmp_cache/cache.rb', line 53 def values gc cache.values.map{|v| v[:value] } end |