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.4.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.
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 |