?
minicache
Python内存缓存工具,应用于Python2或者3. ?
当前功能特征
简单的集和获取工作流
记忆类方法和功能装饰
启用和禁用功能
不需要安装额外的包
安装
从PyPI安装:
pip install minicache
或者使用另外的命令:
pip install https://github.com/duboviy/minicache/archive/master.zip
或者从源文件使用:
python setup.py install
示例
基础使用方式
>>> from minicache import cache
>>> cache.has('key')
False
>>> cache.get('key', default='default')
'default'
>>> cache.update('key', 'value')
>>> cache.get('key')
'value'
>>> cache.disable()
>>> cache.get('key')
装饰和上下文管理
from minicache import cache
import time
@cache.this
def somefunc():
time.sleep(5)
return "this will be cached, and you won't have to wait a second time!"
def test_somefunc():
somefunc()
somefunc()
with cache.temporarily_disabled():
# now we'll have to wait again
somefunc()
记忆类方法装饰:
class Foo(Cacheable):
def __init__(self):
super(Foo, self).__init__()
self._bar = 5
@property
@Cacheable.cached
def m1(self):
print('actual call property...', self._bar)
return self._bar
@Cacheable.cached
def m2(self, a, b, k=4, m=10):
s = a + b + k + m
print('actual call method...', a, b, k, m, s)
return s
@Cacheable.cached
def m3(self, a=3, b=2):
s = a + b
print('actual call method (kwargs only)', a, b, s)
return s