How can I make @functools.lru_cache
decorator ignore some of the function arguments with regard to caching key?
For example, I have a function that looks like this:
def find_object(db_handle, query):
# (omitted code)
return result
If I apply lru_cache
decorator just like that, db_handle
will be included in the cache key. As a result, if I try to call the function with the same query
, but different db_handle
, it will be executed again, which I'd like to avoid. I want lru_cache
to consider query
argument only.
I have at least one very ugly solution. Wrap
db_handle
in a object that's always equals, and unwrap it inside the function.It requires a decorator with quite a bit of helper functions, which makes stack trace quite confusing.
With cachetools you can write: