What's the best way to cache a function in python with an optional caching parameter?
I've a function which performs a database request and does not take any arguments.
Most of the times it's okay if the function uses the cached result but sometimes I want to send a new database request.
from functools import lru_cache
@lru_cache
def my_database_request_function(use_cache: bool = False):
if use_cache:
# takes response from cache
else:
# makes database request
return response
The implementation above won't work because the second time the functions gets called with the use_cache=False parameter it will not make a new request.