我想将缓存用于API中的特定功能。我不想逐行修改内部代码,而是想通过使用类似于模拟补丁的技术来实现相同的目的。 例如
@cache_patch('lib.Someobjclass.func1',ttl=200)
@cache_patch('lib.Someotherobjclass.func2',ttl=1000)
function abc(*args, **kwargs):
'''do stuff'''
cache1 = someobj.func1(args,kwargs)
'''do more stuff'''
cache2 = someotherobj.func2(args,kwargs)
有没有可以使用的库或技术?
答案 0 :(得分:0)
我还不是100%清楚您想要什么确切的行为,因此我将假定仅在执行装饰器所应用的函数期间使用缓存。
您将需要
functools.lru_cache
实现缓存importlib
将作为第一个参数给出的类从字符串导入装饰器import importlib
from functools import lru_cache
class Addition:
def __init__(self, a):
self.a = a
def uncached_addition(self, b):
# print is only used to demonstrate if the method is actually called or not
print(f"Computing {self.a} + {b}")
return self.a + b
class cache_patch:
def __init__(self, method_as_str, ttl):
# split the given path into module, class and method name
class_as_str, method_name = method_as_str.rsplit(".", 1)
module_path, class_name = class_as_str.rsplit(".", 1)
self.clazz = getattr(importlib.import_module(module_path), class_name)
self.method_name = method_name
self.ttl = ttl
def __call__(self, func):
def wrapped(*args, **kwargs):
# monkey patch the original method with a cached version
uncached_method = getattr(self.clazz, self.method_name)
cached_method = lru_cache(maxsize=self.ttl)(uncached_method)
setattr(self.clazz, self.method_name, cached_method)
result = func(*args, **kwargs)
# replace cached method with original
setattr(self.clazz, self.method_name, uncached_method)
return result
return wrapped
@cache_patch('__main__.Addition.uncached_addition', ttl=128)
def perform_patched_uncached_addition(a, b):
d = Addition(a=1)
print("Patched nr. 1\t", d.uncached_addition(2))
print("Patched nr. 2\t", d.uncached_addition(2))
print("Patched nr. 3\t", d.uncached_addition(2))
print()
if __name__ == '__main__':
perform_patched_uncached_addition(1, 2)
d = Addition(a=1)
print("Unpatched nr. 1\t", d.uncached_addition(2))
print("Unpatched nr. 2\t", d.uncached_addition(2))
print("Unpatched nr. 3\t", d.uncached_addition(2))
从输出中可以看到,调用perform_patched_uncached_addition
只会输出一次Computing 1 + 2
,然后使用缓存的结果:
Computing 1 + 2
Patched call nr. 1: 3
Patched call nr. 2: 3
Patched call nr. 3: 3
在此函数之外对类的任何调用都将使用该方法的未修补,非缓存版本:
Computing 1 + 2
Unpatched call nr. 1: 3
Computing 1 + 2
Unpatched call nr. 2: 3
Computing 1 + 2
Unpatched call nr. 3: 3
如果计划在多线程环境中使用此方法,则肯定需要注意。您将无法确定何时将应用缓存补丁。
另外,缓存是根据传递给方法的参数(包括self
)完成的。这意味着该类的每个实例都会有自己的“缓存”。
与大多数情况下,functools.lru_cache
上面的代码不同,它被用作装饰器:
class Addition:
def __init__(self, a):
self.a = a
@lru_cache(maxsize=128)
def cached_addition(self, b):
print(f"Computing {self.a} + b")
return self.a + b