python中函数的缓存结果

cachep的Python项目详细描述


https://travis-ci.com/scidam/cachepy.svg?branch=dev

python的缓存工具集。两种都经过测试 python 2.7.x和3.4+(<;3.8)。

功能

  • Memory-based and file-based caches;
  • Ability to set the TTL (time-to-live) and NOC (the number of calls) on caches;
  • Encryption of the cached data (symmetric encryption algorithm (RSA) is used);
  • LFU (least frequently used) and MFU (most frequently used) cache-clearing strategies;
  • caches of limited size;

注释

  • Encryption functionality requires the PyCryptodome package to be installed;
  • File-based caches save the cached data in a file; files with cached data should be cleaned up manually, if needed.

示例

fromcachepyimport*mycache=Cache()# save the cached data to memory without encryption@mycachedefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I am called...')returnx**2my_heavy_function(2)# "Hi, I am called..." will be printed to stdout only once# return 4my_heavy_function(2)# return 4

要将数据存储到文件中,需要初始化一个decorator,如下所示:

# create cache-to-file decoratorfilecache=FileCache('mycache')# mycache.dat file will be created;# `.dat` extension is appended automatically to the filename# (depends on the shelve module implementation);

它的行为与基于内存的行为相同,但所有缓存的数据都存储在 指定的文件。

可以设置生存时间(TTL)和/或最大呼叫次数(NOC)。 对于初始化装饰程序时缓存的数据:

importtimefromcachepyimport*cache_with_ttl=Cache(ttl=2)# ttl given in seconds@cache_with_ttldefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I am called...')returnx**2my_heavy_function(3)# Hi, I am called... will be printed# return 9my_heavy_function(3)# 'Hi, I am called ...' will not be printed# return 9time.sleep(2)my_heavy_function(3)# 'Hi, I am called ...' will be printed again# return 9
cache_with_noc=Cache(noc=2)# the number-of-calls: noc = 2@cache_with_nocdefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I am called...')returnx**2my_heavy_function(3)my_heavy_function(3)# 'Hi, I am called ...' will not be printedmy_heavy_function(3)# 'Hi, I am called ...' will be printed again

定义时,可以很容易地同时使用nocttl参数 缓存装饰器:

cache_with_noc_ttl=Cache(noc=2,ttl=1)@cache_with_noc_ttldefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I am called...')returnx**2my_heavy_function(3)my_heavy_function(3)# 'Hi, I am called ...' will not be printedmy_heavy_function(3)# 'Hi, I am called ...' will be printed (noc is# reached, recompute the func value)time.sleep(2)# get ttl expiredmy_heavy_function(3)# 'Hi, I am called ...' will be printed again

可以通过提供非空的密钥参数来加密缓存的数据 密码(使用RSA加密算法):

cache_to_file_ttl_noc=FileCache('mycache',noc=2,ttl=2,key='mypassword')@cache_to_file_ttl_nocdefmy_heavy_function(x):'''Performs heavy computations'''print('Hi, I am called...')returnx**2my_heavy_function(2)# 'Hi, I am called...' will be printedmy_heavy_function(2)# 'Hi, I am called...' will not be printed

my_heavy_functioncache_to_file_ttl_noc修饰时,如图所示 在上面的例子中,将计算值2**2=4 计算将存储在名为mycache.dat的文件中。沿着 根据计算结果,将存储附加信息 在文件mycache.dat中。附加信息包括: 1)结果的过期时间(从TTL计算得出); 2)noc和3)已执行的 修饰(my_heavy_function)。

只有安装了pycryptodome包并且 参数(代表密码的非空字符串)传递给 缓存构造函数。它还可以使用旧的pycrypto包。

如果将非空的参数传递给缓存构造函数 但未找到pycryptome,在这种情况下会发出特别警告 (“未安装PyCryptoDome。数据不会被加密 缓存将照常工作,但没有加密功能。

有限制的缓存

标准缓存构造函数用于初始化容量不受限制的缓存。 还有容量有限的缓存。 这种缓存由名为limitedcachelimitedfilecache的构造函数初始化。 这些构造函数有 参数> CIT> CaseEXSimule(存储在缓存中的最大项目数) 算法(缓存清除算法)。可用的算法 lfu(默认值,表示最不常用)和mfu(最常用)。 当algorithm='lfu'时,将从缓存中删除最不常用的项, 如果筋疲力尽。如果算法='mfu',则所有的行为都是相同的, 唯一的区别是删除了最常用的项。

测试

python -m  cachepy.test

待办事项

  • Writing backend for redis server

日志列表

  • Version 1.1
  • Version 1.0 (broken installation via pip/pipenv)
  • Version 0.1
    • initial release

作者

Dmitry Kislov <kislov@easydan.com>

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

推荐PyPI第三方库


热门话题
java共享首选项保存复选框操作   Android上的文本视图上的java Drop shadow   java在windows上将firebase添加到JavaFX   java Android MotionEvent指针索引混乱   java一些文本被添加到JavaScript的变量中   如何使用Java将十六进制转换为rgb?   java非服务JSP   java将侦听器添加到ArrayList   java在使用Scanner类获取输入时遇到问题   使用Mockito将非空列表作为参数进行匹配的java   java Liferay Maven Service Builder Portlet(开发者工作室)   如何在Java8中创建好的迭代   java JTable单元格不可编辑,但可单击   java决定Android应用程序的使用次数   java如何在没有ActiveX的情况下创建多个文件下载程序?   Java/Android,多类   JavaJSF外部链接   java[org.springframework.web.multipart.support.MissingServletRequestPartException:不存在所需的请求部分“image”]   java正则表达式选择直到字符串而不是子字符串