Module: Mize::CacheMethods
- Included in:
- Memoize
- Defined in:
- lib/mize/cache_methods.rb
Overview
Per-object memoization bookkeeping.
Mixed into any module that calls memoize. Each object gets its own
lazily-created cache instance (a copy of Mize.default_cache), and the
key-building helpers below produce stable, argument-sensitive cache keys.
State is deliberately kept per-object: two instances of the same memoized class do not share a cache, which is the expected semantics for instance methods.
Instance Method Summary collapse
-
#__mize_cache__ ⇒ Mize::CacheProtocol
private
Return the cache object, creating one on first use.
-
#__mize_cache__=(cache) ⇒ Mize::CacheProtocol
private
Set the cache object to
cache. -
#build_key(name, *args) ⇒ String
private
Build a
nameprefixed key for the argumentsargs. -
#build_key_prefix(name) ⇒ String
private
Build a key prefix for
name. -
#memoize_apply_visibility(id) { ... } ⇒ Object
private
Apply the visibility of method
idto the wrapper method of this method. -
#mize_cache_clear ⇒ self
Clear cached values for all methods/functions of this object.
-
#mize_cache_clear_name(name) ⇒ self
Clear all cached results for the method/function
name.
Instance Method Details
#__mize_cache__ ⇒ Mize::CacheProtocol (private)
Return the cache object, creating one on first use.
The first access makes a fresh copy of Mize.default_cache (via
prototype / "dup") so that each object holds an independent store.
50 51 52 53 54 55 56 |
# File 'lib/mize/cache_methods.rb', line 50 def __mize_cache__ if defined?(@__mize_cache__) @__mize_cache__ else self.__mize_cache__ = Mize.default_cache.prototype end end |
#__mize_cache__=(cache) ⇒ Mize::CacheProtocol (private)
Set the cache object to cache.
40 41 42 |
# File 'lib/mize/cache_methods.rb', line 40 def __mize_cache__=(cache) @__mize_cache__ = cache end |
#build_key(name, *args) ⇒ String (private)
Build a name prefixed key for the arguments args.
The argument list is +Marshal#+d so structurally-identical arguments always collide on the same key regardless of object identity.
75 76 77 |
# File 'lib/mize/cache_methods.rb', line 75 def build_key(name, *args) "#{build_key_prefix(name)}/#{Marshal.dump(args)}" end |
#build_key_prefix(name) ⇒ String (private)
Build a key prefix for name.
62 63 64 |
# File 'lib/mize/cache_methods.rb', line 62 def build_key_prefix(name) "mize/#{name}" end |
#memoize_apply_visibility(id) { ... } ⇒ Object (private)
Apply the visibility of method id to the wrapper method of this method.
The block is expected to (re)define id as public; afterwards the
original visibility is re-applied in the ensure clause so a memoized
private method stays private.
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/mize/cache_methods.rb', line 88 def memoize_apply_visibility(id) visibility = instance_eval do case when private_method_defined?(id) :private when protected_method_defined?(id) :protected end end yield ensure visibility and __send__(visibility, id) end |
#mize_cache_clear ⇒ self
Clear cached values for all methods/functions of this object.
17 18 19 20 |
# File 'lib/mize/cache_methods.rb', line 17 def mize_cache_clear __mize_cache__.clear self end |
#mize_cache_clear_name(name) ⇒ self
Clear all cached results for the method/function name.
26 27 28 29 30 31 32 |
# File 'lib/mize/cache_methods.rb', line 26 def mize_cache_clear_name(name) name = build_key_prefix(name) __mize_cache__.each_name do |n| n =~ %r{\A#{Regexp.quote(name)}/} and __mize_cache__.delete(n) end self end |