测试Flask-Cache的缓存命中 [英] Testing the cache hits for Flask-Cache

查看:127
本文介绍了测试Flask-Cache的缓存命中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cache.memoize来记住Flask-Cache的功能.如何获取在修饰函数中设置的缓存键?如何在测试过程中测试该功能是否已缓存?

I am using cache.memoize to memoize a function with Flask-Cache. How can I get the cache key which got set in the decorated function? How can I test that the function is cached during testing?

from flask import Flask
from flask.ext.cache import Cache

app = Flask(__name__)
cache = Cache(app, config={'CACHE_TYPE': 'simple'})

@cache.memoize(timeout=10)
def get_news(nid, lang=None):
    return nid, lang

@app.route('/news/<str:nid>')
def news(news_id):
    return 'News: ' + get_news(news_id)

推荐答案

使用memoize时,缓存键是在后台生成的,永远不需要手动访问. Flask-Cache正在为您处理缓存并检索该函数的结果.但是,如果您对Flask-Cache的工作方式感兴趣,可以

When using memoize, the cache key is generated behind the scenes and should never need to be accessed manually. Flask-Cache is handling caching and retrieving the result of the function for you. But if you're interested in how Flask-Cache does it, you can look at the source. It is a hash of the function name, arguments, and a uuid.

您不需要测试缓存命中,因为Flask-Cache已经过测试.您应该测试自己的代码,而不是库代码.但是,如果您对Flask-Cache的工作方式感兴趣,可以看看测试.它记住一个返回当前时间的函数,然后检查睡眠后返回的值是否相同.

You shouldn't need to test cache hits, because Flask-Cache is already tested. You should test your own code, not library code. But if you're interested in how Flask-Cache does it, you can look at the tests. It memoizes a function that returns the current time, then checks if the return is the same after sleeping.

这篇关于测试Flask-Cache的缓存命中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆