具有定时缓存和缓存感知速率限制选项的缓存包
memorize的Python项目详细描述
安装
要安装memory,只需使用pip:
$ pip install memorize
或直接从源安装以包含最新更改:
$ pip install git+https://github.com/saporitigianni/memorize.git
或克隆然后安装:
$ git clone https://github.com/saporitigianni/memorize.git
$ cd memorize
$ python3 setup.py install
用法
此类扩展functools.lru_缓存功能,以添加定时缓存和缓存感知速率限制 (例如,如果从缓存返回呼叫结果,则该特定呼叫不会影响速率限制)
- 约束:
- 由于字典用于缓存结果,函数的位置参数和关键字参数必须是散列的(而不是列表、dict、set或任何其他未定义hash()的对象)
- 即使是空的,也要使用括号:@memory()而不是@memory
- 在限制速率时,必须同时提供调用和句点参数,否则将引发错误
- 默认设置:
- 超时=无(可以是int或float)
- maxsize=none(可以是int)
- 键入=false(可以为true)
- 调用=无(可以是int)
- 句点=无(可以是int或float)
- aware=false(可以是true)
frommemorizeimportmemorize# If you want to use all default settings@memorize()deffib(n):ifn<2:returnnelse:returnfib(n-2)+fib(n-1)# With memorization fib(20) will be run 21 times instead of 21891 times# without memorizationfib(20)# If you want to cache a maximum of 128 calls, cached by different types,# each for 10 seconds use:@memorize(timeout=10,maxsize=128,typed=True)# If you want to implement caching aware rate limiting then use the following:# This will limit to no more than 10 calls for every 5 second period and if a# result is returned from the cache it does not count towards the 10 calls.@memorize(calls=10,period=5,aware=True)
贡献
在进行希望在代码中采用的更改之前,请阅读CONTRIBUTING文档。
行为准则
在memorize项目的代码库中进行交互的每个人,问题 跟踪程序、聊天室和邮件列表应遵循 PyPA Code of Conduct.:八达通:
以太网0xAD1F09626B8E701D5F0F4A237193DF73D3C445
BTC 199zsvqcusefv8yjdyqhuqzmlcyh75dqnv
LTC润滑油7vxc43ttpsqum1jazfmshktau1rs9