/
example.py
121 lines (94 loc) · 3.04 KB
/
example.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
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
"""Test LRUCache Implementation."""
from time import sleep
from lrucache.lru_cache import LRUCache
from lrucache.memoize_decorator import memoized_cache
lru = LRUCache(maxSize=4)
lru.insert_key_value("a", 99)
lru["b"] = 202
lru["c"] = 203
lru["d"] = 204
lru["e"] = 205
lru.get_value("a")
lru.get("a", None)
print(lru)
print(lru.values())
# LRUCache(timeout=None, size=4, data={'b': 202, 'c': 203, 'd': 204, 'e': 205})
# [202, 203, 204, 205]
# Test memoization
# Sample Usage with the decorator (maxSize)
@memoized_cache(cache=LRUCache(maxSize=5))
def get_random(max_value):
import random
return random.random() * max_value
print(get_random(1)) # 0.8926141265778564
print(get_random(1)) # 0.8926141265778564
print(get_random(3)) # 1.9138579745807172
print(get_random(4)) # 3.5772986693895916
print(get_random(5)) # 0.5001657308606899
print(get_random(5)) # 0.5001657308606899
print(get_random(7)) # 0.7652871914811611
print(get_random(8)) # 0.4366852743803227
print(get_random(1)) # 0.6811475789638526
print(get_random.misses)
# => 7
print(get_random.hits)
# => 2
# cache info
get_random.cache_info()
# => memoized_cache(hits=2, misses=7, maxsize=5, currsize=5)
# check the cache stored key, value, items pairs
print(get_random.cache.keys())
# => dict_keys([-5205072475343462643, 8575776084210548143, -2238842041537299568, -8811688270097994377, 2613783748954017437])
print(get_random.cache.values())
# => [1.9216226691107239, 3.442601057826532, 0.6831533160972438, 7.40200570325546, 0.37636284785825047]
print(get_random.cache.items())
# => [
# (-5205072475343462643, 1.9216226691107239), (8575776084210548143, 3.442601057826532),
# (-2238842041537299568, 0.6831533160972438), (-8811688270097994377, 7.40200570325546),
# (2613783748954017437, 0.37636284785825047)
# ]
# Simple report on performance
report = f'Hit %: {(float(get_random.hits) / (get_random.hits + get_random.misses))}'
print(report)
# => Hit %: 0.2222222222222222
# Test time constraint
timed_lru = LRUCache(maxSize = 4, timeout=10)
timed_lru["a"] = 202
timed_lru["b"] = 203
timed_lru["c"] = 204
timed_lru["d"] = 205
timed_lru["e"] = 206
print(timed_lru) # LRUCache(timeout=10, size=4, data={'b': 203, 'c': 204, 'd': 205, 'e': 206})
sleep(60)
timed_lru["d"] = 203
timed_lru["e"] = 204
timed_lru["f"] = 205
timed_lru["g"] = 206
timed_lru["h"] = 207
print(timed_lru) # LRUCache(timeout=10, size=4, data={'e': 204, 'f': 205, 'g': 206, 'h': 207})
sleep(30)
print(timed_lru) # LRUCache(timeout=10, size=4, data={})
timed_lru.stop_timer()
# Sample Usage with the decorator (maxSize and timeout)
@memoized_cache(cache=LRUCache(maxSize=5, timeout=5))
def get_random(max_value):
import random
return random.random() * max_value
get_random(1)
get_random(1)
get_random(3)
get_random(4)
get_random(5)
get_random(5)
get_random(7)
get_random(8)
get_random(1)
get_random.cache_info()
print(get_random.cache.values())
print(get_random.cache.items())
print(get_random.cache.keys())
print(get_random.cache.values())
print(get_random.cache.values())
sleep(20)
get_random.cache_info()
get_random.cache.stop_timer()