Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试功能/区域是否缓存在烧杯/狗堆中_Python_Beaker_Dogpile.cache - Fatal编程技术网

Python 测试功能/区域是否缓存在烧杯/狗堆中

Python 测试功能/区域是否缓存在烧杯/狗堆中,python,beaker,dogpile.cache,Python,Beaker,Dogpile.cache,使用python模块烧杯或Dogpile进行缓存,是否可以测试缓存中是否已经存在具有特定键值的区域?假设您有一个使用烧杯进行缓存的方法: @cache_region('foo_term') def cached_method(bar): return actual_method(bar) 然后在您的测试中,您可以修补方法_to_test并断言它被调用/未被调用: def test_cache(): with patch('package.module.actual_method'

使用python模块烧杯或Dogpile进行缓存,是否可以测试缓存中是否已经存在具有特定键值的区域?

假设您有一个使用烧杯进行缓存的方法:

@cache_region('foo_term')
def cached_method(bar):
   return actual_method(bar)
然后在您的测试中,您可以修补方法_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
请注意,您必须使用函数的“缓存”版本包装要缓存的方法,并将烧杯缓存装饰器放在缓存版本上。当然,除非您找到一种方法使
修补程序
可以使用