Skip to content

Commit c5c3456

Browse files
authored
Adds cache_clear() function to clear the cache. (iamsinghrajat#23)
* Adds cache_clear() function to clear the cache. * Add docstring to cache_clear().
1 parent 9925f07 commit c5c3456

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

cache/async_lru.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ def __init__(self, maxsize=128):
88
:param maxsize: Use maxsize as None for unlimited size cache
99
"""
1010
self.lru = LRU(maxsize=maxsize)
11+
12+
def cache_clear(self):
13+
"""
14+
Clears the LRU cache.
15+
16+
This method empties the cache, removing all stored
17+
entries and effectively resetting the cache.
18+
19+
:return: None
20+
"""
21+
self.lru.clear()
1122

1223
def __call__(self, func):
1324
async def wrapper(*args, use_cache=True, **kwargs):
@@ -19,5 +30,6 @@ async def wrapper(*args, use_cache=True, **kwargs):
1930
return self.lru[key]
2031

2132
wrapper.__name__ += func.__name__
33+
wrapper.__dict__['cache_clear'] = self.cache_clear
2234

2335
return wrapper

cache/async_ttl.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ def __init__(self, time_to_live=60, maxsize=1024, skip_args: int = 0):
4848
self.ttl = self._TTL(time_to_live=time_to_live, maxsize=maxsize)
4949
self.skip_args = skip_args
5050

51+
def cache_clear(self):
52+
"""
53+
Clears the TTL cache.
54+
55+
This method empties the cache, removing all stored
56+
entries and effectively resetting the cache.
57+
58+
:return: None
59+
"""
60+
self.ttl.clear()
61+
5162
def __call__(self, func):
5263
async def wrapper(*args, use_cache=True, **kwargs):
5364
key = KEY(args[self.skip_args:], kwargs)
@@ -60,5 +71,6 @@ async def wrapper(*args, use_cache=True, **kwargs):
6071
return val
6172

6273
wrapper.__name__ += func.__name__
74+
wrapper.__dict__['cache_clear'] = self.cache_clear
6375

6476
return wrapper

tests/lru_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
async def func(wait: int):
1010
await asyncio.sleep(wait)
1111

12+
@AsyncLRU(maxsize=128)
13+
async def cache_clear_fn(wait: int):
14+
await asyncio.sleep(wait)
15+
1216

1317
class TestClassFunc:
1418
@AsyncLRU(maxsize=128)
@@ -104,9 +108,30 @@ def test_cache_refreshing_lru():
104108
assert t1 - t3 <= 0.1
105109

106110

111+
def test_cache_clear():
112+
# print("call function. Cache miss.")
113+
t1 = time.time()
114+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
115+
t2 = time.time()
116+
# print("call function again. Cache hit")
117+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
118+
t3 = time.time()
119+
cache_clear_fn.cache_clear()
120+
# print("Call cache_clear() to clear the cache.")
121+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
122+
t4 = time.time()
123+
# print("call function third time. Cache miss)")
124+
125+
assert t2 - t1 > 1, t2 - t1 # Cache miss
126+
assert t3 - t2 < 1, t3 - t2 # Cache hit
127+
assert t4 - t3 > 1, t4 - t3 # Cache miss
128+
129+
130+
107131
if __name__ == "__main__":
108132
test()
109133
test_obj_fn()
110134
test_class_fn()
111135
test_skip_args()
112136
test_cache_refreshing_lru()
137+
test_cache_clear()

tests/ttl_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ async def short_cleanup_fn(wait: int):
2222
await asyncio.sleep(wait)
2323
return wait
2424

25+
@AsyncTTL(time_to_live=3)
26+
async def cache_clear_fn(wait: int):
27+
await asyncio.sleep(wait)
28+
return wait
29+
2530

2631
def cache_hit_test():
2732
t1 = time.time()
@@ -69,8 +74,27 @@ def test_cache_refreshing_ttl():
6974
assert t1 > t2
7075
assert t1 - t3 <= 0.1
7176

77+
def cache_clear_test():
78+
# print("call function. Cache miss.")
79+
t1 = time.time()
80+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
81+
t2 = time.time()
82+
# print("call function again. Cache hit")
83+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
84+
t3 = time.time()
85+
cache_clear_fn.cache_clear()
86+
# print("Call cache_clear() to clear the cache.")
87+
asyncio.get_event_loop().run_until_complete(cache_clear_fn(1))
88+
t4 = time.time()
89+
# print("call function third time. Cache miss)")
90+
91+
assert t2 - t1 > 1, t2 - t1 # Cache miss
92+
assert t3 - t2 < 1, t3 - t2 # Cache hit
93+
assert t4 - t3 > 1, t4 - t3 # Cache miss
94+
7295

7396
if __name__ == "__main__":
7497
cache_hit_test()
7598
cache_expiration_test()
7699
test_cache_refreshing_ttl()
100+
cache_clear_test()

0 commit comments

Comments
 (0)