-
Notifications
You must be signed in to change notification settings - Fork 7
/
memoizer.py
41 lines (34 loc) · 1.11 KB
/
memoizer.py
1
2
3
4
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
class Memoizer (object):
FLUSH_THRESHOLD = 1000
"""Given a function, caches the results of the function for sets of arguments
and returns the cached result where possible. Do not use if you have
very large possible combinations of args, or we'll run out of RAM."""
def __init__(self, fn, extractor=None):
self.fn = fn
self.cache = { }
self.extractor = extractor or (lambda baz: baz)
def __call__(self, *args):
if len(self.cache) > Memoizer.FLUSH_THRESHOLD:
self.flush()
key = self.extractor(args)
if not key in self.cache:
value = self.fn(*args)
self.cache[key] = value
return value
return self.cache[key]
def flush(self):
self.cache.clear()
def flush_key(self, *args):
try:
del self.cache[args]
except KeyError:
pass
def __contains__(self, *args):
return args in self.cache
def set_key(self, value, *args):
self.cache[args] = value
def record(self, args, value):
self.cache[self.extractor(args)] = value
class DBMemoizer (Memoizer):
def __init__(self, fn):
Memoizer.__init__(self, fn, lambda args: args[1:])