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

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

Parameters:

  • method_name (Symbol)

    the method name

Returns:

  • (Integer)

    zero when the method has no cache yet



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_memosObject

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

Parameters:

  • method_name (Symbol)

    the method name



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

Parameters:

  • method_name (Symbol)

    the method name

  • args (Array)

    positional arguments used for the call

  • kwargs (Hash)

    keyword arguments used for the call

Returns:

  • (Boolean)

    true if an entry was removed, false otherwise



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_keysArray<Symbol>

Return the names of methods that currently have caches on this instance

Returns:

  • (Array<Symbol>)

    method names with initialized caches



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

Parameters:

  • method_name (Symbol)

    the method name

Returns:

  • (Hash, nil)

    :hits, :misses, :hit_rate or nil if not cached



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

Parameters:

  • method_name (Symbol)

    the method name

  • args (Array)

    positional arguments used for the call

  • kwargs (Hash)

    keyword arguments used for the call

Returns:

  • (Boolean)

    true if a non-expired cached value exists



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