[Python]缓存cachetools与TTLCache简介

cachetools是一个Python第三方库,提供了多种缓存算法的实现。

cachetools

使用前需要先安装pip install cachetools,说明文档参见https://cachetools.readthedocs.io/en/latest/

cachetools提供了五种缓存类型:

  • Cached
  • LRUCache
  • TTLCache
  • LFUCache
  • RRCache

缓存策略

cachetools提供了以下常见的缓存策略(当需要为新的条目腾出空间时,如何移除已有条目):

  • Least Recently Used (LRU): 移除最近最少使用的条目;这是一种常见的缓存策略,适用于大多数场景。
  • Most Recently Used (MRU): 移除最近最常使用的条目;这种策略适用于一些特殊场景,例如当最近访问的数据不太可能再次访问时。
  • Random Replacement (RR): 随机移除一个条目。;这种策略实现简单,但性能相对较差。
  • First-In-First-Out (FIFO): 按照条目添加到缓存的顺序进行移除;这种策略适用于一些特定场景,例如缓存的数据具有固定生命周期。

cachetools中允许自定义缓存策略,只需继承cachetools.Cache类,并实现相应的方法。

from cachetools import Cache

class SizeLimitedCache(Cache):
    def __init__(self, maxsize):
        super().__init__(maxsize=maxsize)

    def __getitem__(self, key, cache_getitem=Cache.__getitem__):
        return cache_getitem(self, key)

    def __setitem__(self, key, value, cache_setitem=Cache.__setitem__):
        if len(self) >= self.maxsize:
            self.popitem()  # 删除一个缓存项
        cache_setitem(self, key, value)


# 使用自定义缓存策略
custom_cache = SizeLimitedCache(maxsize=10)
for i in range(20):
    key = "key-{}".format(i)
    custom_cache[key] = i
    print(custom_cache.items())

缓存操作

创建缓存时,都需指定最大条目数:lru = cachetools.LRUCache(maxsize=100)

  • maxsize参数代表的是缓存中可以存储的最大条目数量

缓存的添加、获取等类似dict操作:

# 添加缓存项
lru["key"] = "value"

# 获取缓存项
# value = lru.get("key") # 不存在时,返回None
value = lru.get("key", "default_value")

# 删除缓存项
if "key" in lru:
    del lru["key"]

# 更新缓存项
lru["key"] = "new_value"

其他有用函数与属性:

  • clear():清空Cache;
  • popitem():根据策略,移除一项;
  • currsize:获取当前缓存条目数量;

TTLCache

TTLCache可设定过期时间,过期后缓存条目自动失效:

  • expire(self, time=None):移除过期(若time为None在使用当前时间)条目;
  • get时,不会更新过期时间;只有重新设定数据(cache[key]=data),才会更新过期时间;
  • 已满时添加新条目,会先移除过期条目,然后尝试移除第一条。
def expensive_fun(arg1, arg2):
    key = (arg1, arg2)

    # 如果缓存中已经有计算结果,直接返回
    result = cache.get(key)
    if result is not None:
        cache[key] = result  # extend expire-time
        return result, 'cached'

    # 执行计算并将结果保存到缓存中
    time.sleep(1)  # expensive calc
    result = arg1 + arg2
    cache[key] = result
    return result, 're-calc'

## cache test
cache = TTLCache(maxsize=5, ttl=10)
for i in range(10):
    print(datetime.now(), expensive_fun(i, 2))
    print(cache.items())

print(cache.items())

for i in range(5):
    time.sleep(3)
    print(datetime.now(), expensive_fun(1, 2)) 
    print(cache.items())

# 等待缓存项已经过期
time.sleep(11)

# 再次调用需要重新计算,因为缓存项已经被清除了
print(datetime.now(), expensive_fun(1, 2)) 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值