mariushelf / shelved_cache

Persistent Cache implementation for Python cachetools.
MIT License
30 stars 3 forks source link

Functions with the same arguments conflict with each other #7

Open uninstall-your-browser opened 1 month ago

uninstall-your-browser commented 1 month ago

When 2 functions have different names but the same parameter signature, they conflict with each other when using this utility

import cachetools
from shelved_cache import PersistentCache
from cachetools import LRUCache

pc = PersistentCache(LRUCache, filename="/tmp/test_cache", maxsize=1000)

@cachetools.cached(pc)
def square(x):
    return x * x

@cachetools.cached(pc)
def cube(x):
    return x * x * x

if __name__ == '__main__':
    print(f"square(2) = {square(2)}") # = 4
    print(f"cube(2) = {cube(2)}") # = 4 !?

Expected that cube(2) = 8. The key function in the decorator should at least consider something like method.__qualname__ to prevent this in most cases This seems a bit more complex than that, cachetools relies on each function/method having a unique cache/dict, so sharing the cache with cachetools.cached will have this problem unless you have another wrapper like with asynccached or cachedasyncmethod and add the function name (or something else that identifies the function) to the key args

tadamcz commented 1 month ago

wow, thanks for posting this issue! I was on the fence about using this library... not anymore

mariushelf commented 1 month ago

Thanks for posting this, and already diving into it. @uninstall-your-browser, would you like to create a PR for this? Life is a bit busy at the moment for me, so I don't have time to work on this package in the near future.