python - 测试函数/区域是否缓存在 Beaker/Dogpile 中

标签 python beaker dogpile.cache

使用 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

请注意,您必须使用函数的“缓存”版本包装要缓存的方法,并将烧杯缓存装饰器放在缓存版本上。当然,除非您找到使 patchthis black magic 一起工作的方法。 .

关于python - 测试函数/区域是否缓存在 Beaker/Dogpile 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27366413/

相关文章:

python - 将稀疏字典表示转换为密集数据帧

python - 根据实例变量有条件地禁用缓存装饰器

python - 如何在不安装的情况下使用烧杯?

python - 使用 dogpile 根据修改时间缓存下载的文件

python - 检查变量是否设置为 NO_VALUE

python - 从 Pandas 中的 iterrows() 获取行位置而不是行索引

python - 从 STEP 文件解析 BReps

python - 通过 Python Flask 服务器提供服务时,Vue 应用程序不会加载

python - Beaker 中数据库和 sql 后端之间的区别?