New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
functools.lru_cache user specified cachedict support #70270
Comments
Currently, lru_cache will automatically construct a Python dictionary in the function as cachedict. IMHO, it will be much more flexible to let users specified their cachedict, so they can use any kind of dict-like calss as their cachedict. Thus, users can use any dictionary implementation and save result in any form they want. for example : use OrderedDict .. code-block:: python from functools import lru_cache
from collections import OrderedDict
@lru_cache(maxsize=None, cache=OrderedDict())
def func(*args, **kwargs):
pass save by pickle .. code-block:: python import os
import pickle
from functools import lru_cache
filename = "cache.pickle"
cache = {}
def load_cache():
global cache
if os.path.isfile(filename):
with open(filename, "rb") as f:
cache = pickle.load(f)
def store_cache():
with open(filename, "wb") as f:
pickle.dump(cache, f)
load_cache()
@lru_cache(maxsize=None, cache=cache)
def func(*args, **kwargs):
pass |
Given that lru_cache uses the cache dict in very specific ways, supporting arbitrary mapping types would be extremely hard. Among other things:
|
Marking as closed/rejected for the reasons lists by Josh. The functools.lru_cache() decorator is somewhat tightly focused and is trying to do one thing well. Another reason is that at some point, we want to be able to change the internals (perhaps using the new compact/ordereddict) and that would be precluded by this feature request. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: