Module: SafeMemoize::PublicMethods

Included in:
InstanceMethods
Defined in:
lib/safe_memoize/public_methods.rb

Instance Method Summary collapse

Instance Method Details

#clear_memo_hooks(hook_type = nil) ⇒ Object



51
52
53
54
55
# File 'lib/safe_memoize/public_methods.rb', line 51

def clear_memo_hooks(hook_type = nil)
  with_memo_lock do
    _clear_memo_hooks(hook_type)
  end
end

#memo_count(*method_name) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/safe_memoize/public_methods.rb', line 15

def memo_count(*method_name)
  scoped_method = safe_memo_scoped_method(method_name)

  with_memo_lock do
    safe_memo_count_for(scoped_method)
  end
end

#memo_keys(*method_name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/safe_memoize/public_methods.rb', line 23

def memo_keys(*method_name)
  scoped_method = safe_memo_scoped_method(method_name)

  with_memo_lock do
    safe_memo_keys_for(scoped_method)
  end
end

#memo_values(*method_name) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/safe_memoize/public_methods.rb', line 31

def memo_values(*method_name)
  scoped_method = safe_memo_scoped_method(method_name)

  with_memo_lock do
    safe_memo_values_for(scoped_method)
  end
end

#memoized?(method_name, *args, **kwargs, &block) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
# File 'lib/safe_memoize/public_methods.rb', line 5

def memoized?(method_name, *args, **kwargs, &block)
  return false if block

  cache_key = safe_memo_cache_key(method_name, args, kwargs)

  with_memo_lock do
    memo_cache_hit?(cache_key)
  end
end

#on_memo_evict(&block) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
# File 'lib/safe_memoize/public_methods.rb', line 45

def on_memo_evict(&block)
  raise ArgumentError, "block required" unless block

  register_memo_hook(:on_evict, &block)
end

#on_memo_expire(&block) ⇒ Object

Raises:

  • (ArgumentError)


39
40
41
42
43
# File 'lib/safe_memoize/public_methods.rb', line 39

def on_memo_expire(&block)
  raise ArgumentError, "block required" unless block

  register_memo_hook(:on_expire, &block)
end

#reset_all_memosObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/safe_memoize/public_methods.rb', line 76

def reset_all_memos
  with_memo_lock do
    if defined?(@__safe_memo_cache__) && @__safe_memo_cache__
      @__safe_memo_cache__.each do |key, record|
        call_memo_hooks(:on_evict, key, record)
      end
    end
    @__safe_memo_cache__ = {}
  end
end

#reset_memo(method_name, *args, **kwargs) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/safe_memoize/public_methods.rb', line 57

def reset_memo(method_name, *args, **kwargs)
  method_name = method_name.to_sym

  matcher = memo_matcher_for(method_name, args, kwargs)

  with_memo_lock do
    with_memo_cache do |cache|
      cache.delete_if do |key, record|
        if matcher.call(key)
          call_memo_hooks(:on_evict, key, record)
          true
        else
          false
        end
      end
    end
  end
end