]>
git.proxmox.com Git - ceph.git/blob - ceph/src/pybind/mgr/dashboard/plugins/ttl_cache.py
2 This is a minimal implementation of TTL-ed lru_cache function.
4 Based on Python 3 functools and backports.functools_lru_cache.
6 from __future__
import absolute_import
8 from functools
import wraps
9 from collections
import OrderedDict
10 from threading
import RLock
14 def ttl_cache(ttl
, maxsize
=128, typed
=False):
15 if typed
is not False:
16 raise NotImplementedError("typed caching not supported")
18 def decorating_function(function
):
26 "hits={}, misses={}, expired={}, maxsize={}, currsize={}".format(
27 stats
[0], stats
[1], stats
[2], maxsize
, len(cache
)))
30 def wrapper(*args
, **kwargs
):
31 key
= args
+ tuple(kwargs
.items())
35 (ret
, ts
) = cache
[key
]
44 ret
= function(*args
, **kwargs
)
46 if len(cache
) == maxsize
:
47 cache
.popitem(last
=False)
50 cache
[key
] = (ret
, ts
)
55 return decorating_function