-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlru_cache
More file actions
executable file
·40 lines (32 loc) · 855 Bytes
/
Copy pathlru_cache
File metadata and controls
executable file
·40 lines (32 loc) · 855 Bytes
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
#!/usr/bin/env python
from functools import lru_cache
called_with = []
@lru_cache(maxsize=3, typed=False)
def foo(x):
called_with.append(x)
return {'val': x}
a = foo(3)
m = foo(4)
foo(5)
b = foo(3) # Uses cache
assert(a is b)
assert(b['val'] == 3)
a['val'] = 'updated'
assert(b['val'] == 'updated')
foo(3) # Uses cache
foo(1) # Causes eviction of 4
foo(3) # Uses cache
c = foo(4) # calls foo again
assert(c is not m)
assert(called_with == [3,4,5,1,4])
info = foo.cache_info() # == CacheInfo(hits=3, misses=5, maxsize=3, currsize=3))
assert(info.hits == 3)
assert(info.misses == 5)
assert(info.maxsize == 3)
assert(info.currsize == 3)
foo.cache_clear()
info = foo.cache_info() # == CacheInfo(hits=0, misses=0, maxsize=3, currsize=0))
assert(info.hits == 0)
assert(info.misses == 0)
assert(info.maxsize == 3)
assert(info.currsize == 0)