Module: Philiprehberger::Memo
- Defined in:
- lib/philiprehberger/memo.rb,
lib/philiprehberger/memo/cache.rb,
lib/philiprehberger/memo/version.rb,
lib/philiprehberger/memo/wrapper.rb
Overview
Practical memoization with TTL, LRU eviction, and thread-safety
Defined Under Namespace
Modules: ClassMethods, Wrapper Classes: Cache, Error
Constant Summary collapse
- VERSION =
'0.6.0'
Class Method Summary collapse
Instance Method Summary collapse
-
#cache_size(method_name) ⇒ Integer
Return the number of cached entries for a memoized method.
-
#clear_all_memos ⇒ Object
Clear all memoized caches on this instance.
-
#clear_memo(method_name) ⇒ Object
Clear memoized cache for a specific method.
-
#forget_memo(method_name, *args, **kwargs) ⇒ Boolean
Remove a specific cached call signature without clearing the full cache.
-
#memo_keys ⇒ Array<Symbol>
Return the names of methods that currently have caches on this instance.
-
#memo_stats(method_name) ⇒ Hash?
Return cache stats for a specific memoized method.
-
#memoized?(method_name, *args, **kwargs) ⇒ Boolean
Check whether a call signature has been memoized for a method.
-
#total_memo_stats ⇒ Hash
Aggregate hit/miss stats across every memoized method on this instance.
Class Method Details
.included(base) ⇒ Object
12 13 14 |
# File 'lib/philiprehberger/memo.rb', line 12 def self.included(base) base.extend(ClassMethods) end |
Instance Method Details
#cache_size(method_name) ⇒ Integer
Return the number of cached entries for a memoized method
62 63 64 65 |
# File 'lib/philiprehberger/memo.rb', line 62 def cache_size(method_name) cache = memo_cache_for(method_name) cache ? cache.size : 0 end |
#clear_all_memos ⇒ Object
Clear all memoized caches on this instance
90 91 92 93 94 |
# File 'lib/philiprehberger/memo.rb', line 90 def clear_all_memos return unless instance_variable_defined?(:@_memo_caches) @_memo_caches.each_value(&:clear) end |
#clear_memo(method_name) ⇒ Object
Clear memoized cache for a specific method
31 32 33 34 |
# File 'lib/philiprehberger/memo.rb', line 31 def clear_memo(method_name) cache = memo_cache_for(method_name) cache&.clear end |
#forget_memo(method_name, *args, **kwargs) ⇒ Boolean
Remove a specific cached call signature without clearing the full cache
82 83 84 85 86 87 |
# File 'lib/philiprehberger/memo.rb', line 82 def forget_memo(method_name, *args, **kwargs) cache = memo_cache_for(method_name) return false unless cache cache.delete([args, kwargs]) end |
#memo_keys ⇒ Array<Symbol>
Return the names of methods that currently have caches on this instance
70 71 72 73 74 |
# File 'lib/philiprehberger/memo.rb', line 70 def memo_keys return [] unless instance_variable_defined?(:@_memo_caches) @_memo_caches.keys end |
#memo_stats(method_name) ⇒ Hash?
Return cache stats for a specific memoized method
40 41 42 43 |
# File 'lib/philiprehberger/memo.rb', line 40 def memo_stats(method_name) cache = memo_cache_for(method_name) cache&.stats end |
#memoized?(method_name, *args, **kwargs) ⇒ Boolean
Check whether a call signature has been memoized for a method
51 52 53 54 55 56 |
# File 'lib/philiprehberger/memo.rb', line 51 def memoized?(method_name, *args, **kwargs) cache = memo_cache_for(method_name) return false unless cache cache.key?([args, kwargs]) end |
#total_memo_stats ⇒ Hash
Aggregate hit/miss stats across every memoized method on this instance.
Returns a hash with totals plus a ‘methods` count. `hit_rate` is computed over the combined hits and misses. Returns zeroes when no memoization has occurred yet.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/philiprehberger/memo.rb', line 103 def total_memo_stats return { hits: 0, misses: 0, hit_rate: 0.0, methods: 0 } unless instance_variable_defined?(:@_memo_caches) hits = 0 misses = 0 @_memo_caches.each_value do |cache| s = cache.stats hits += s[:hits] misses += s[:misses] end total = hits + misses { hits: hits, misses: misses, hit_rate: total.zero? ? 0.0 : hits.to_f / total, methods: @_memo_caches.size } end |