python3内存缓存_Python内存缓存工具 – minicache

?

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值