【Python模块】Python 缓存机制与 functools.lru_cache

http://blog.konghy.cn/2016/04/20/python-cache/

缓存是一种将定量数据【同一份数据需要多次使用的】加以保存以备迎合后续获取需求的处理方式,旨在加快数据获取的速度。

被 lru_cache 装饰的函数会有 cache_clear 和 cache_info 两个方法,分别用于清除缓存和查看缓存信息。以下为一个简单的 lru_cache 的使用效果:

from functools import lru_cache

@lru_cache(None)
def add(x, y):
    print("calculating: %s + %s" % (x, y))
    return x + y

print(add(1, 2))
print(add(1, 2))
print(add(2, 3))

https://www.cnblogs.com/cuiyubo/p/8375859.html
【搬运】给lru_cache传递的maxsize参数是同时来限制存储在缓存中的项目数量。
一旦你记忆一个函数,它将只为你调用的每一组参数计算一次输出。第一次调用之后的每次调用都将快速从缓存中检索出来。
https://www.jb51.net/article/128669.htm
【搬运】LRU(Least Recently Used)最近最少使用,当LRU缓存达到设定的最大值时将缓存中近期最少使用的对象移除。

XR代码: 分钢管{长度:价格} (递归)
#某长度为len的钢管,迭代切分呈length,cut ,使总lenth_prices最大

length_prices_v = {
    1: 1, 
    2: 5,
    3: 8, 
    4: 9,
    5: 10,
    6: 17,
    7: 17,
    8: 20,
    9: 24,
    10: 30
}
##########################################

from collections import defaultdict  ##
length_price = defaultdict(int)      ##
print(type(length_price))
#defaultdict 的真正意义实现一种全局的初始化,访问任何键都不会抛 KeyError 的异常;

for k,v in length_prices_v.items():
    length_price[k]=v

length = 5

cut_methods= ([((length,i),length_price[length]+length_price[l

你可能感兴趣的:(python模块,Python缓存)