-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption_2_dict.py
48 lines (33 loc) · 1.04 KB
/
option_2_dict.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
42
43
44
45
46
47
48
#!/usr/bin/env python
'''
This caching option uses a global dictionary to store results.
This is a better result than using global variables, since we now have "keys"
for each operation.
The downside is (possibly) we need to control key generation, and each function
needs to check the cache. Not very DRY!
'''
import time
from pprint import pprint as pp
CACHE = {}
def do_long_op_one(arg1, arg2):
key = 'do_long_op_one|%r|%r' % (arg1, arg2)
if key in CACHE:
return CACHE[key]
# simulate long operation
time.sleep(5)
CACHE[key] = arg1 * arg2
return CACHE[key]
def clear_cache():
CACHE.clear()
def main():
print time.asctime()
print "The result:", do_long_op_one(1, 2)
print time.asctime()
print "The result:", do_long_op_one(1, 2)
print "The result:", do_long_op_one(3, 4)
print "The result:", do_long_op_one(3, 4)
print "The result:", do_long_op_one(10, 10)
print "CACHE is:"
pp(CACHE)
if __name__ == '__main__':
main()