使用 python 模块 Beaker 或 Dogpile 进行缓存,是否可以测试具有特定键值的区域是否已存在于缓存中?
最佳答案
假设您有一个使用烧杯缓存的方法:
@cache_region('foo_term')
def cached_method(bar):
return actual_method(bar)
然后在您的测试中,您可以修补 method_to_test 并断言它被调用/未被调用:
def test_cache():
with patch('package.module.actual_method') as m:
cached_method(foo)
assert m.call_args_list = [call(foo)] # Asserting it is not cached the first time
cached_method(foo)
assert m.call_args_list = [call(foo)] # Now asserting it is cached
cached_method(bar)
assert m.call_args_list = [call(foo), call(bar)] # asserting `bar' is not cached
请注意,您必须使用函数的“缓存”版本包装要缓存的方法,并将烧杯缓存装饰器放在缓存版本上。当然,除非您找到使 patch
与 this black magic 一起工作的方法。 .
关于python - 测试函数/区域是否缓存在 Beaker/Dogpile 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366413/