pythonキャッシュメカニズム
763 ワード
from functools import lru_cache
@lru_cache(maxsize=128, typed=True)
maxsize ,typed True
from async_lru import alru_cache
@alru_cache()
import functools
def hash_dict(func):
"""Transform mutable dictionnary
Into immutable
Useful to be compatible with cache
"""
class HDict(dict):
def __hash__(self):
return hash(frozenset(self.items()))
@functools.wraps(func)
async def wrapped(*args, **kwargs):
args = tuple([HDict(arg) if isinstance(arg, dict) else arg for arg in args])
kwargs = {k: HDict(v) if isinstance(v, dict) else v for k, v in kwargs.items()}
return await func(*args, **kwargs)
return wrapped