小智 5
威尔很可能是正确的,但万一你想尝试,我认为子类 TTLCache 并覆盖一个函数应该可以工作:
from cachetools import Cache, TTLCache
cache = TTLItemCache(maxsize=2, ttl=100)
# ttl=100
cache.__setitem__('key1', 'val1')
# ttl=200
cache.__setitem__('key2', 'val2', ttl=200)
class TTLItemCache(TTLCache):
def __setitem__(self, key, value, cache_setitem=Cache.__setitem__, ttl=None):
super(TTLItemCache, self).__setitem__(key, value)
if ttl:
link = self._TTLCache__links.get(key, None)
if link:
link.expire += ttl - self.ttl
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3385 次 |
| 最近记录: |