Search code examples
pythonpython-3.xlru

Use multiple keys with LRUCache


Can I use multiple/combination of keys in the LRUCache implementation from Cachetools? I want to use it like below

def fun(a,b): pass
x = LRUCache(maxsize=100,missing=fun)

and call it, I tried like below

x[a][b]

and

x[(a,b)]

but doesn't work


Solution

  • missing must be a function of a single argument, but you could use a lambda to wrap fun and unpack the tuple:

    x = LRUCache(maxsize=100, missing=lambda args: fun(*args))
    

    and call with:

    x[(a, b)]