python utils和decorators,用于使用ttl、maxsize和基于文件的存储的c_ching。

caching的Python项目详细描述


状态

正在工作

缓存

Build StatusCoverage StatusPython Versions

python utils和decorators,用于使用ttl、maxsize和基于文件的存储的c_ching。

安装

pip install caching

用法

fromcachingimportCache# File-based cache with unlimited ttl and maximum of 128 cached results@Cache(ttl=-1,maxsize=128,filepath='/tmp/mycache')deflong_running_function(a,b,*args,c=None,**kwargs):pass# Memory-based cache with limited ttl and maxsize and "least recently used"# cache replacement policy.@Cache(ttl=60,maxsize=128,policy='LRU')deflong_running_function(a,b,*args,c=None,**kwargs):pass

高级用法

fromcachingimportCache# One cache for many functionscache=Cache(filepath='/tmp/mycache',ttl=3600,maxsize=1024)@cachedefpow(x,y):returnx**y@cachedeffactorial(n):ifn==0:return1returnn*factorial(n-1)# Caching the last result and returning it only in case of errors@Cache(maxsize=1,only_on_errors=(ConnectionError,TimeoutError))defapi_request():"""Request some remote resource which sometimes become unavailable.
    If this functions raises ConnectionError or TimeoutError, then the
    last cached result will be returned, if available."""# Custom cache key function@Cache(key=lambdax:x[0])deftoupper(a):globalcall_countcall_count+=1returnstr(a).upper()call_count=0# The key function returns the same result for both 'aaa' and 'azz'# so the cached result from the first call is returned in the second callasserttoupper('aaa')==toupper('azz')=='AAA'assertcall_count==1# Using cache as a key-value storecache=Cache()try:result=cache[1]exceptKeyError:result=calculate_result(1)cache[1]=resultassert1incacheassertcache[1]==resultassertcache.get(1,None)==resultassertcache.get(2,None)isNone# Cleanupimportoscache=Cache(filepath='/tmp/mycache')cache[1]='one'assert1incachecache.clear()# empty the cacheassert1notincacheassertlist(cache.items())==[]assertos.path.isfile('/tmp/mycache')cache.remove()# Empty the cache and remove the underlying fileassertnotos.path.isfile('/tmp/mycache')

功能

  • [X]基于内存和文件的缓存。
  • [X]TTL和MaxSize。
  • [X]与^{TT2}$,^{TT3}$一起工作。
  • [X]使用以下类型的可变函数参数:^{TT4}$,^{TT5}$,^{TT6}$。
  • [X]FIFO、LRU和LFU缓存替换策略。
  • [X]可定制的缓存键功能。
  • []多处理-线程安全。
  • []可插入的外部缓存后端(请参阅redis示例)。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
如何使用Java中的扫描仪读取文本文件中的特定字符?   java如果我们在hibernate中开始事务但不提交它,会发生什么?   Azure CosmosDB Java Springboot中的无服务器帐户不支持spring boot设置提供吞吐量或容器自动导航   附加到新对象的Java注释?   java如何将自定义文本视图添加到。在Kotlin中添加通知操作   java Shibboleth添加_OpenSAMLcookies,导致HTTP头大小>8k   分布式传感器数据(~40Hz)的高效Java观测器设计   java如何在while循环外声明数组,但在while循环中初始化它?   用@XmlElementRef注释的java元素没有显示在JAXB编组字符串中?   java替换二维数组的值   java如何在任务栏上创建Windows7加载栏   java如何在组件注释bean中使用会话或RequestScope bean?   java netbeans freermarker插件错误:在实现版本中请求netbeans桥的插件Lexer   java谷歌地图方向。加载失败,返回服务器错误   java当我试图递归地计算两个值之间的整数之和时,为什么结果返回一个奇怪的值?   java如何通过html文件的用户获取运行时输入,以使用Jsoup进行解析?