Class: ESI::RubyCache
Overview
A ruby thread safe cache, stores cached fragments in the current ruby process memory
A hash table indexed by cache_key of Fragments. the cache is made thread safe if the external invalidator is active otherwise the Mutex is a no op
Default cache.
Instance Method Summary collapse
- #cached?(uri, params) ⇒ Boolean
- #delete(key) ⇒ Object
- #delete_unlocked(key) ⇒ Object
- #get(uri, params) ⇒ Object
-
#initialize(options = {}) ⇒ RubyCache
constructor
A new instance of RubyCache.
- #keys(&block) ⇒ Object
- #put(uri, params, max_age, body) ⇒ Object
-
#sweep! ⇒ Object
run through the cache and dump anything that has expired.
- #sweep_unlocked! ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ RubyCache
Returns a new instance of RubyCache.
163 164 165 166 167 |
# File 'lib/esi/cache.rb', line 163 def initialize( = {} ) super() @semaphore = Mutex.new @cache = {} end |
Instance Method Details
#cached?(uri, params) ⇒ Boolean
169 170 171 172 173 |
# File 'lib/esi/cache.rb', line 169 def cached?( uri, params ) key = cache_key(uri,params) fragment = @semaphore.synchronize { @cache[key] } fragment and fragment.valid? end |
#delete(key) ⇒ Object
202 203 204 |
# File 'lib/esi/cache.rb', line 202 def delete( key ) @semaphore.synchronize { @cache.delete(key) } end |
#delete_unlocked(key) ⇒ Object
206 207 208 |
# File 'lib/esi/cache.rb', line 206 def delete_unlocked( key ) @cache.delete(key) end |
#get(uri, params) ⇒ Object
175 176 177 178 179 |
# File 'lib/esi/cache.rb', line 175 def get( uri, params ) key = cache_key(uri,params) fragment = @semaphore.synchronize { @cache[key] } fragment.dup if fragment and fragment.respond_to?(:dup) end |
#keys(&block) ⇒ Object
194 195 196 197 198 199 200 |
# File 'lib/esi/cache.rb', line 194 def keys(&block) @semaphore.synchronize do @cache.each do|key,data| yield key, data end end end |
#put(uri, params, max_age, body) ⇒ Object
181 182 183 184 185 186 187 |
# File 'lib/esi/cache.rb', line 181 def put( uri, params, max_age, body ) key = cache_key(uri,params) @semaphore.synchronize do @cache[key] = Fragment.new(uri,max_age,body) sweep_unlocked! end end |
#sweep! ⇒ Object
run through the cache and dump anything that has expired
190 191 192 |
# File 'lib/esi/cache.rb', line 190 def sweep! @semaphore.synchronize { sweep_unlocked! } end |
#sweep_unlocked! ⇒ Object
210 211 212 |
# File 'lib/esi/cache.rb', line 210 def sweep_unlocked! @cache.reject! {|k,v| !v.valid? } end |