Class: Mize::DefaultCache
- Inherits:
-
Object
- Object
- Mize::DefaultCache
- Includes:
- CacheProtocol
- Defined in:
- lib/mize/default_cache.rb
Overview
The default in-memory cache backend.
A plain Hash wrapper that implements just enough of the Mize cache
protocol to back memoization out of the box: read / write /
exist? / delete / clear / each_name.
Individual operations are atomic under the GVL. For multi-step
atomicity (e.g. check→compute→write), wrap the call in
Mize.synchronize.
Because it is backed by a plain Hash, entries are never evicted until you
clear the cache explicitly (or the object is garbage-collected). For
persistent or evicting behaviour, swap in a different
Mize::CacheProtocol implementation via Mize.configure.
Instance Method Summary collapse
-
#clear(options = nil) ⇒ self
Clear the cache by removing all entries from the cache.
-
#delete(name, options = nil) ⇒ Object?
Delete a cache entry by name.
-
#each_name {|name| ... } ⇒ self
Each name of the cache is yielded to the block.
-
#exist?(name, options = nil) ⇒ Boolean
Determine whether a cache entry exists in this cache.
-
#initialize ⇒ Mize::DefaultCache
constructor
Create an empty cache.
-
#initialize_dup(other) ⇒ Mize::DefaultCache
Initialize a duplicate of this cache.
-
#read(name, options = nil) ⇒ Object?
Read a value from the cache by name.
-
#write(name, value, options = nil) ⇒ Object
Write a value to the cache by name.
Constructor Details
#initialize ⇒ Mize::DefaultCache
Create an empty cache.
21 22 23 |
# File 'lib/mize/default_cache.rb', line 21 def initialize @data = {} end |
Instance Method Details
#clear(options = nil) ⇒ self
Clear the cache by removing all entries from the cache.
28 29 30 31 |
# File 'lib/mize/default_cache.rb', line 28 def clear( = nil) @data.clear self end |
#delete(name, options = nil) ⇒ Object?
Delete a cache entry by name. If an entry does not exist in the cache, it will do nothing.
70 71 72 |
# File 'lib/mize/default_cache.rb', line 70 def delete(name, = nil) @data.delete(name) end |
#each_name {|name| ... } ⇒ self
Each name of the cache is yielded to the block.
78 79 80 81 |
# File 'lib/mize/default_cache.rb', line 78 def each_name(&block) @data.each_key(&block) self end |
#exist?(name, options = nil) ⇒ Boolean
Determine whether a cache entry exists in this cache.
38 39 40 |
# File 'lib/mize/default_cache.rb', line 38 def exist?(name, = nil) @data.key?(name) end |
#initialize_dup(other) ⇒ Mize::DefaultCache
Initialize a duplicate of this cache.
The underlying store is shallow-copied so the original and the duplicate
diverge immediately (this is what prototype relies on to hand each
memoized object its own independent cache).
91 92 93 94 |
# File 'lib/mize/default_cache.rb', line 91 def initialize_dup(other) super other.instance_variable_set :@data, @data.dup end |
#read(name, options = nil) ⇒ Object?
Read a value from the cache by name. If the entry does not exist in the
cache, it will return nil.
48 49 50 |
# File 'lib/mize/default_cache.rb', line 48 def read(name, = nil) @data.fetch(name, nil) end |
#write(name, value, options = nil) ⇒ Object
Write a value to the cache by name. If an entry with the same name already exists in the cache, it will be overwritten.
59 60 61 |
# File 'lib/mize/default_cache.rb', line 59 def write(name, value, = nil) @data.store(name, value) end |