【问题标题】:Test if a function/region is cached in Beaker/Dogpile测试一个函数/区域是否缓存在 Beaker/Dogpile
【发布时间】:2015-02-06 14:31:54
【问题描述】:

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

【问题讨论】:

    标签: python beaker dogpile.cache


    【解决方案1】:

    假设你有一个用烧杯缓存的方法:

    @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 一起工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-22
      • 2011-04-13
      • 1970-01-01
      • 2016-11-16
      • 2021-02-17
      • 2020-05-13
      相关资源
      最近更新 更多