def cachedmethod(f, cache=LRUDict(capacity=MAX_CACHE_ITEMS)): """ Method with a cached content >>> __ = cachedmethod(lambda _: _) >>> __(1) 1 >>> __ = cachedmethod(lambda *args, **kwargs: args[0]) >>> __(2) 2 >>> __ = cachedmethod(lambda *args, **kwargs: list(kwargs.values())[0]) >>> __(foobar=3) 3 Reference: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ """ @functools.wraps(f) def _(*args, **kwargs): key = int( hashlib.md5("|".join(str(_) for _ in (f, args, kwargs)).encode( UNICODE_ENCODING)).hexdigest(), 16) & 0x7fffffffffffffff try: with _lock: result = cache[key] except KeyError: result = f(*args, **kwargs) with _lock: cache[key] = result return result return _
def cachedmethod(f, cache=LRUDict(capacity=MAX_CACHE_ITEMS)): """ Method with a cached content Reference: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ """ @functools.wraps(f) def _(*args, **kwargs): key = int( hashlib.md5("|".join(str(_) for _ in (f, args, kwargs))).hexdigest(), 16) & 0x7fffffffffffffff try: with _lock: result = cache[key] except KeyError: result = f(*args, **kwargs) with _lock: cache[key] = result return result return _
def cachedmethod(f): """ Method with a cached content >>> __ = cachedmethod(lambda _: _) >>> __(1) 1 >>> __(1) 1 >>> __ = cachedmethod(lambda *args, **kwargs: args[0]) >>> __(2) 2 >>> __ = cachedmethod(lambda *args, **kwargs: next(iter(kwargs.values()))) >>> __(foobar=3) 3 Reference: http://code.activestate.com/recipes/325205-cache-decorator-in-python-24/ """ _cache[f] = LRUDict(capacity=MAX_CACHE_ITEMS) @functools.wraps(f) def _f(*args, **kwargs): try: key = int( hashlib.md5("|".join(str(_) for _ in (f, args, kwargs)).encode( UNICODE_ENCODING)).hexdigest(), 16) & 0x7fffffffffffffff except ValueError: # https://github.com/sqlmapproject/sqlmap/issues/4281 (NOTE: non-standard Python behavior where hexdigest returns binary value) result = f(*args, **kwargs) else: try: with _cache_lock: result = _cache[f][key] except KeyError: result = f(*args, **kwargs) with _cache_lock: _cache[f][key] = result return result return _f