5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/safe_memoize/class_methods.rb', line 5
def memoize(method_name, ttl: nil, max_size: nil, ttl_refresh: false, if: nil, unless: nil, shared: false, key: nil)
method_name = method_name.to_sym
visibility = memoized_method_visibility(method_name)
config = SafeMemoize.configuration
ttl = config.default_ttl if ttl.nil?
max_size = config.default_max_size if max_size.nil?
cond_if = binding.local_variable_get(:if)
cond_unless = binding.local_variable_get(:unless)
ttl = if ttl.nil?
nil
else
ttl = Float(ttl)
raise ArgumentError, "ttl must be non-negative" if ttl < 0
ttl
end
max_size = if max_size.nil?
nil
else
raise ArgumentError, "max_size must be a positive integer" unless max_size.is_a?(Integer)
raise ArgumentError, "max_size must be positive" unless max_size > 0
max_size
end
raise ArgumentError, "ttl_refresh: requires a ttl: to be set" if ttl_refresh && ttl.nil?
if cond_if && cond_unless
raise ArgumentError, "cannot specify both :if and :unless"
end
raise ArgumentError, ":if must be callable" if cond_if && !cond_if.respond_to?(:call)
raise ArgumentError, ":unless must be callable" if cond_unless && !cond_unless.respond_to?(:call)
raise ArgumentError, ":key must be callable" if key && !key.respond_to?(:call)
__safe_memo_class_key_generators__[method_name] = key if key
condition = if cond_if
cond_if
elsif cond_unless
->(result) { !cond_unless.call(result) }
end
if shared
klass = self
shared_mutex = klass.send(:__safe_memo_shared_mutex__)
mod = Module.new do
define_method(method_name) do |*args, **kwargs, &block|
return super(*args, **kwargs, &block) if block
cache_key = compute_cache_key(method_name, args, kwargs)
shared_mutex.synchronize do
shared_cache = klass.send(:__safe_memo_shared_cache__)
record = shared_cache[cache_key]
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
record_live = record && (record[:expires_at].nil? || record[:expires_at] > now)
if record_live
if max_size
lru = klass.send(:__safe_memo_shared_lru_order__)[method_name] ||= {}
lru.delete(cache_key)
lru[cache_key] = true
end
record[:expires_at] = memo_expires_at(ttl) if ttl_refresh
record_cache_hit(method_name, args, kwargs)
call_memo_hooks(:on_hit, cache_key, record)
record[:value]
else
call_memo_hooks(:on_expire, cache_key, record) if record && !record_live
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
value = super(*args, **kwargs)
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
new_record = memo_record(value, expires_at: memo_expires_at(ttl))
if !condition || condition.call(value)
if max_size
lru = klass.send(:__safe_memo_shared_lru_order__)[method_name] ||= {}
lru.delete_if { |key, _| !shared_cache.key?(key) }
if lru.size >= max_size
lru_key = lru.keys.first
lru.delete(lru_key)
evicted = shared_cache.delete(lru_key)
call_memo_hooks(:on_evict, lru_key, evicted) if evicted
end
end
shared_cache[cache_key] = new_record
if max_size
lru = klass.send(:__safe_memo_shared_lru_order__)[method_name] ||= {}
lru[cache_key] = true
end
call_memo_hooks(:on_store, cache_key, new_record)
end
record_cache_miss(method_name, args, kwargs, elapsed_time)
call_memo_hooks(:on_miss, cache_key, new_record)
value
end
end
end
send(visibility, method_name)
end
prepend mod
return
end
mod = Module.new do
define_method(method_name) do |*args, **kwargs, &block|
return super(*args, **kwargs, &block) if block
cache_key = compute_cache_key(method_name, args, kwargs)
if max_size || condition || ttl_refresh
memo_mutex!.synchronize do
record = memo_cache_record(cache_key)
if record
lru_touch(method_name, cache_key) if max_size
record[:expires_at] = memo_expires_at(ttl) if ttl_refresh
record_cache_hit(method_name, args, kwargs)
call_memo_hooks(:on_hit, cache_key, record)
memo_record_value(record)
else
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
value = super(*args, **kwargs)
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
new_record = memo_record(value, expires_at: memo_expires_at(ttl))
if !condition || condition.call(value)
lru_evict_if_over_limit(method_name, max_size) if max_size
@__safe_memo_cache__ ||= {}
@__safe_memo_cache__[cache_key] = new_record
lru_touch(method_name, cache_key) if max_size
call_memo_hooks(:on_store, cache_key, new_record)
end
record_cache_miss(method_name, args, kwargs, elapsed_time)
call_memo_hooks(:on_miss, cache_key, new_record)
value
end
end
else
if (record = memo_cache_record(cache_key))
record_cache_hit(method_name, args, kwargs)
call_memo_hooks(:on_hit, cache_key, record)
return memo_record_value(record)
end
start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
result = memo_fetch_or_store(cache_key, ttl: ttl) { super(*args, **kwargs) }
elapsed_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
with_memo_lock do
record_cache_miss(method_name, args, kwargs, elapsed_time)
new_record = memo_cache_record(cache_key)
call_memo_hooks(:on_store, cache_key, new_record)
call_memo_hooks(:on_miss, cache_key, new_record)
end
result
end
end
send(visibility, method_name)
end
prepend mod
end
|