quick cache是一个易于使用和设置的缓存实现。
quick-cache的Python项目详细描述
一个快速易用的python缓存系统。
您可以通过
安装quick_cachepip install --user quick_cache
并使用以下命令将其导入到python中:
fromquick_cacheimportQuickCache
按如下方式创建缓存对象:
defmsg(message,*args,**kwargs):print(message.format(*args,**kwargs),file=sys.stderr)cache=QuickCache(base_file,quota=500,ram_quota=100,warnings=msg)
其中base_file是一个可选文件,其content使 缓存(即,当文件内容更改时,缓存为 无效;对于大型文件,可能需要使用中的mtime 下面的cache对象)和msg是可选的格式化函数 它会打印警告(默认情况下是None,而不打印 任何东西;当实际计算速度更快时发出警告 而不是从缓存中读取结果,或者 情况发生)。{Tt4} $和^ {Tt5}$是可选的最大值 缓存大小(以RAM和磁盘为单位,以MB为单位)。
然后可以通过以下途径使用缓存功能:
withcache.get_hnd({# object identifying the task to cache# can be any combination of keys and values"param_a":5,"input_file_c":os.path.getmtime(input_file_c),# for file change time...})ashnd:ifnothnd.has():res=hnd.write(do_compute())# compute your result hereelse:res=hnd.read()# your result is in res
用于创建句柄的缓存对象唯一定义任务。 对象应包含任务和任务的所有参数 计算本身应该是确定性的。