python持久化缓存_Cashier 持久化缓存python函数

Cashier

Persistent caching for python functions

Simply add a decorator to a python function and cache the results for future use. Extremely handy when you are dealing with I/O heavy operations which seldom changes or CPU intensive functions as well.

Anatomically, once a function is called, result from the function is cached into an SQLite3 database locally, with an expiry time. There is a maximum length for the cache to prevent cache flooding the file system.

Installation

pip install cashier

Or you can clone the source and run setup.py

git clone git@github.com:atmb4u/cashier.git

cd cashier

python setup.py install

Usage

from cashier import cache

@cache()

def complex_function(a,b,c,d):

return complex_calculation(a,b,c,d)

If you go ahead on the above configuration, following are the default values

cache_file : .cache

cache_time : 84600

cache_length : 10000

retry_if_blank : False

Advanced Usage

from cashier import cache

@cache(cache_file="sample.db", cache_time=7200, cache_length=1000,

retry_if_blank=True)

def complex_function(a, b, c, d):

return complex_calculation(a, b, c, d)

cache_file : SQLite3 file name to which cached data should be written into (defaults to .cache)

cache_time : how long should the data be cached in seconds (defaults to 1 day)

cache_length : how many different arguments and corresponding data should be cached (defaults to 10000)

retry_if_blank : If True, will retry for the data if blank data is cached ( default is False)

Performance Benchmark

For reproducing results, run python test.py from the project root.

No Cache Run: 9.932126 seconds

First Caching Run: 9.484081 seconds

Cached Run: 0.606016 seconds (16 x faster)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值