Class: Opod::PStoreCache
- Inherits:
-
Object
- Object
- Opod::PStoreCache
- Defined in:
- lib/opod/pstore.rb
Instance Method Summary collapse
-
#[](key) ⇒ Object
(also: #get, #read)
Return the object stored under key from the cache.
-
#[]=(key, obj) ⇒ Object
(also: #put, #write)
Store obj under key in the cache.
-
#all ⇒ Object
(also: #values)
Return all objects stored in the cache.
-
#delete(key) ⇒ Object
Delete the object stored under key in the cache.
-
#gc! ⇒ Object
Remove all expired objects from the cache.
-
#initialize(keepalive = nil) ⇒ PStoreCache
constructor
A new instance of PStoreCache.
Constructor Details
#initialize(keepalive = nil) ⇒ PStoreCache
17 18 19 20 |
# File 'lib/opod/pstore.rb', line 17 def initialize(keepalive = nil) @keepalive = keepalive initialize_store end |
Instance Method Details
#[](key) ⇒ Object Also known as: get, read
Return the object stored under key from the cache. If the object isn’t present in the cache nil is returned.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/opod/pstore.rb', line 25 def [](key) current_try = 0 begin current_try += 1 @store.transaction(true) do @store.fetch(key, nil)[1] end rescue PStore::Error # Unable to return the requested object from the cache. # Create a new store and retry. initialize_store unless current_try > PStoreCache.max_tries retry else raise Exception.new("Unable to read from cache file #{@store.path}!") end end end |
#[]=(key, obj) ⇒ Object Also known as: put, write
Store obj under key in the cache.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/opod/pstore.rb', line 46 def []=(key, obj) current_try = 0 begin current_try += 1 @store.transaction(false) do if @keepalive @store[key] = [Time.now + @keepalive, obj] else @store[key] = [nil, obj] end end rescue PStore::Error # Unable to store object. # Create a new store and retry. initialize_store unless current_try > PStoreCache.max_tries retry else raise Exception.new("Unable to write to cache file #{@store.path}!") end end end |
#all ⇒ Object Also known as: values
Return all objects stored in the cache.
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/opod/pstore.rb', line 71 def all current_try = 0 begin current_try += 1 @store.transaction(true) do @store.roots.inject([]) do |result, current| result << @store[current][1] end end rescue PStore::Error # Unable to return stored objects from cache. # Create a new store and retry. initialize_store unless current_try > PStoreCache.max_tries retry else raise Exception.new("Unable to read from cache file #{@store.path}!") end end end |
#delete(key) ⇒ Object
Delete the object stored under key in the cache.
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/opod/pstore.rb', line 94 def delete(key) begin @store.transaction(false) do @store.delete(key) end rescue PStore::Error # Unable to delete object from the cache. # Create a new store. initialize_store end end |
#gc! ⇒ Object
Remove all expired objects from the cache.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/opod/pstore.rb', line 108 def gc! return unless @keepalive begin now = Time.now @store.transaction(false) do @store.roots.each do |r| @store.delete(r) if now > @store[r][0] end end rescue PStore::Error # Unable to delete object from the cache. # Create a new store. initialize_store end end |