一个轻量级 Python 装饰器的缓存库 - wrapcache

jopen 8年前

A python Function / Method OUTPUT cache system base on function Decorators.

基本信息

github 地址:https://github.com/hustcc/wrapcache

如何使用

使用简单,只需要要在方法上面加一个装饰器即可缓存,并且设置缓存过期时间。

import wrapcache  @wrapcache.wrapcache(timeout = 60)  def need_cache_function(input, t = 2, o = 3):      sleep(2)      return random.randint(1, 100)

以上即可,第一次运行需要 2 秒,第二次运行(过期时间 60 秒之内)瞬间给出缓存结果。适合于小场景的方法缓存。

安装方法

安装方法:

pip install wrapcache

即可,当然你也可以直接下载 github 代码,然后放到自己项目中即可。

支持 python2 , python3 各版本。

TODO

目前是使用内存 dict 存储缓存,后续要支持将 redis 和 memcached 等服务器中,只需要补充 adapter 中代码,实现对应方法即可

欢迎 push requst 和 issue 。

来自: http://segmentfault.com/a/1190000004243894