-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.py
22 lines (20 loc) · 1.09 KB
/
main_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
from conditional_cache import lru_cache, ttl_cache
# Example usage
@ttl_cache(maxsize=32, ttl=5, condition=lambda result: result == True)
def foo(item_id: int) -> bool:
# Simulate a database query
print(f"Executed for: {item_id}")
return item_id % 2 == 0 # Only even IDs will be cached
# Testing the decorator
print(foo(item_id=1)) # This call's result is not cached due to condition
print(foo(item_id=2)) # This call's result is cached
print(foo(item_id=2)) # This call retrieves the result from cache
print(foo(item_id=3)) # This call's result is not cached due to condition
print(foo(item_id=3)) # This call's result is not cached due to condition
print(foo(item_id=4)) # This call retrieves the result from cache
print(foo(item_id=4)) # This call retrieves the result from cache
print(foo(item_id=2)) # This call retrieves the result from cache
foo.cache_remove(item_id=2) # Remove the result from cache
print(foo(item_id=2)) # This call's result is cached
print(foo(item_id=2)) # This call's result is retrieved from cache
print(foo(item_id=4)) # This call retrieves the result from cache