方法和结果的简单ttl缓存。
method-cache的Python项目详细描述
python方法缓存
它的功能
方法缓存可用于以灵活的方式缓存方法的结果。 这个libary没有依赖项!
当前只有TTL缓存可用
缓存存储可以按类别分段。
具有@cache
装饰符的方法保存在这些类别中。
示例
frommethodcacheimportcache,Storeimporttime# TTL in Seconds (Default TTl is 60)st=Store(ttl=60*300)# Define Store for this Cache, and overwrite Store TTL. And categorize this cache object to demo@cache(store=st,ttl=5,category="demo")defdoothings(a,b,c):# Do here what ever you want.# eg. MySQL querys or other slow operationstime.sleep(2)returna+b+c# Slow Executiondoothings(1,2,3)# Slow Executiondoothings(4,5,6)# Fast Execution because the Cache was useddoothings(1,2,3)time.sleep(2)# Slow Execution because the Cache TTL is Expireddoothings(1,2,3)
文档
或者
显示example.py