]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/lru_cache.py
1 # -*- coding: utf-8 -*-
3 This is a minimal implementation of lru_cache function.
5 Based on Python 3 functools and backports.functools_lru_cache.
8 from collections
import OrderedDict
9 from functools
import wraps
10 from threading
import RLock
13 def lru_cache(maxsize
=128, typed
=False):
14 if typed
is not False:
15 raise NotImplementedError("typed caching not supported")
17 def decorating_function(function
):
21 setattr(function
, 'cache_info', lambda:
22 "hits={}, misses={}, maxsize={}, currsize={}".format(
23 stats
[0], stats
[1], maxsize
, len(cache
)))
26 def wrapper(*args
, **kwargs
):
27 key
= args
+ tuple(kwargs
.items())
35 ret
= function(*args
, **kwargs
)
36 if len(cache
) == maxsize
:
37 cache
.popitem(last
=False)
43 return decorating_function