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