def wrapper(*args, **kwds): key = args if kwds: key += tuple(sorted(kwds.items())) if use_hash_any: key = hash_any(key) try: result = cache.pop(key) wrapper.hits += 1 except KeyError: result = user_function(*args, **kwds) wrapper.misses += 1 if len(cache) >= maxsize: cache.popitem(0) # purge least recently used cache entry cache[key] = result # record recent use of this key return result
def wrapper(*args, **kwargs): key = args if kwargs: key += (kwarg_mark, ) + tuple(sorted(kwargs.items())) if use_hash_any: key = hash_any(key) # get cache entry or compute if not found try: result = cache[key] use_count[key] += 1 wrapper.hits += 1 except KeyError: # need to add something to the cache, make room if necessary if len(cache) == maxsize: for k, _ in nsmallest(maxsize // 10 or 1, use_count.iteritems(), key=itemgetter(1)): del cache[k], use_count[k] cache[key] = user_function(*args, **kwargs) result = cache[key] use_count[key] += 1 wrapper.misses += 1 return result
def wrapper(*args, **kwargs): key = args if kwargs: key += (kwarg_mark,) + tuple(sorted(kwargs.items())) if use_hash_any: key = hash_any(key) # get cache entry or compute if not found try: result = cache[key] use_count[key] += 1 wrapper.hits += 1 except KeyError: # need to add something to the cache, make room if necessary if len(cache) == maxsize: for k, _ in nsmallest(maxsize // 10 or 1, use_count.iteritems(), key=itemgetter(1)): del cache[k], use_count[k] cache[key] = user_function(*args, **kwargs) result = cache[key] use_count[key] += 1 wrapper.misses += 1 return result