我最近从烧杯切换到dogpile.cache。它在实时代码中运行良好,但我在测试中遇到了问题。如何禁用缓存进行测试?
我目前正在使用
#caching.py
from dogpile.cache import make_region
region = make_region().configure(
'dogpile.cache.redis',
expiration_time = 3600,
arguments = {
'host': '127.0.0.1',
'port': 6379
}
)
#db.py
from .caching import region
@region.cache_on_arguments()
def fetch_from_db(item):
return some_database.lookup(item)
如何换出缓存或禁用它以进行单元测试?
在测试期间,将狗堆配置为使用NullBackend
,这是空对象设计模式的一个示例。
from dogpile.cache import make_region
region = make_region().configure(
'dogpile.cache.null'
)
将装饰器重新定义为标识函数。
即
if __debug__:
def dont_cache():
def noop(f):
return f
return noop
class Nothing:
pass
region = Nothing()
region.cache_on_arguments = dont_cache
else:
from .caching import region