Skip to content
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

Closed
wdv4758h mannequin opened this issue Jan 11, 2016 · 3 comments
Closed

functools.lru_cache user specified cachedict support #70270

wdv4758h mannequin opened this issue Jan 11, 2016 · 3 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@wdv4758h
Copy link
Mannequin

wdv4758h mannequin commented Jan 11, 2016

BPO 26082
Nosy @rhettinger, @MojoVampire
Files
  • functools.lru_cache-user-specified-cachedict.patch: add user specified cachedict support
  • 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:

    assignee = 'https://github.com/rhettinger'
    closed_at = <Date 2017-04-25.14:36:07.821>
    created_at = <Date 2016-01-11.19:23:57.160>
    labels = ['type-feature', 'library']
    title = 'functools.lru_cache user specified cachedict support'
    updated_at = <Date 2017-04-25.14:36:07.820>
    user = 'https://bugs.python.org/wdv4758h'

    bugs.python.org fields:

    activity = <Date 2017-04-25.14:36:07.820>
    actor = 'rhettinger'
    assignee = 'rhettinger'
    closed = True
    closed_date = <Date 2017-04-25.14:36:07.821>
    closer = 'rhettinger'
    components = ['Library (Lib)']
    creation = <Date 2016-01-11.19:23:57.160>
    creator = 'wdv4758h'
    dependencies = []
    files = ['41584']
    hgrepos = []
    issue_num = 26082
    keywords = ['patch']
    message_count = 3.0
    messages = ['258001', '258049', '292261']
    nosy_count = 3.0
    nosy_names = ['rhettinger', 'josh.r', 'wdv4758h']
    pr_nums = []
    priority = 'normal'
    resolution = 'rejected'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue26082'
    versions = ['Python 3.6']

    @wdv4758h
    Copy link
    Mannequin Author

    wdv4758h mannequin commented Jan 11, 2016

    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

    @wdv4758h wdv4758h mannequin added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Jan 11, 2016
    @MojoVampire
    Copy link
    Mannequin

    MojoVampire mannequin commented Jan 12, 2016

    Given that lru_cache uses the cache dict in very specific ways, supporting arbitrary mapping types would be extremely hard. Among other things:

    1. The C code uses the concrete dict APIs (including private APIs) that would not work on arbitrary mappings that don't directly inherit from dict (and often wouldn't work properly even if they do inherit from dict). Changing to use the abstract mapping APIs would slow all use cases for an extremely uncommon use case.

    2. The C code is operating under the assumption that specific operations cannot release the GIL (e.g. dict insertion and deletion is done after precomputing the hash of the key, so it's impossible for Python byte code to be executed), so it can safely ignore thread safety issues. If a non-dict mapping was provided, implemented in Python rather than C, these assumptions could easily be violated.

    3. This is basically a superset of the request from bpo-23030, which rhettinger has rejected (you can read the rationale there)

    @rhettinger
    Copy link
    Contributor

    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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant