简体   繁体   English

Flask 缓存一些数据但不是全部

[英]Flask cache some data but not all

I have a function that I am trying to calculate the run time duration.我有一个 function 我正在尝试计算运行时间。 when I use the Flask cache, it won't go into the function and won't calculate the duration.当我使用 Flask 缓存时,它不会 go 进入 function 并且不会计算持续时间。

@app.route('/', methods=['get'])
@cache.cached(timeout=60)
def request_process_duration():
    start = time.time()
    for i in range(5):
        time.sleep(1)
    duration = time.time() - start
    return jsonify(duration)

This function output was 5 for 60 seconds (cache timeout) I want to get an output of 5 in the first run, and then for the next 60 seconds much lower output. This function output was 5 for 60 seconds (cache timeout) I want to get an output of 5 in the first run, and then for the next 60 seconds much lower output.

when I use the Flask cache, it won't go into the function and won't calculate the duration.当我使用 Flask 缓存时,它不会 go 进入 function 并且不会计算持续时间。

As you may already understand this is because only the return value of that function is cached.正如您可能已经理解的那样,这是因为仅缓存了 function 的返回值。

One approach to solve this might be to create your own decorator to time the function.解决这个问题的一种方法可能是创建自己的装饰器来为 function 计时。

So initialize the app, and configure the cache:所以初始化应用程序,并配置缓存:

from flask import Flask, jsonify
import time 

app = Flask(__name__)

from flask_caching import Cache

config = {
    "DEBUG": True,          # some Flask specific configs
    "CACHE_TYPE": "simple", # Flask-Caching related configs
    "CACHE_DEFAULT_TIMEOUT": 300
}

app.config.from_mapping(config)
cache = Cache(app)

Then define a decorator.然后定义一个装饰器。 This one will print the output in seconds:这将在几秒钟内打印 output:

def timeit(method):
    def timed(*args, **kw):
        ts = time.time()
        result = method(*args, **kw)
        te = time.time()        
        print ((te-ts), 'seconds')
        return result
    return timed

Finally decorate the function you wish to time.最后装饰你想要计时的function。 The decorator lines must be placed in this order:装饰器行必须按以下顺序放置:

@app.route('/', methods=['get'])
@timeit
@cache.cached(timeout=60)
def request_process_duration():
    for i in range(5):
        time.sleep(1)
    return {'status':'success'}

When tested with two subsequent requests, this gives the ouput at the server console:当使用两个后续请求进行测试时,这会在服务器控制台上给出输出:

4.977460145950317 seconds
172.17.0.1 - - [07/Aug/2020 14:57:04] "GET / HTTP/1.1" 200 -
0.00011014938354492188 seconds
172.17.0.1 - - [07/Aug/2020 14:57:05] "GET / HTTP/1.1" 200 -

The only problem with this approach is that the calculated time is only available within the decorator itself.这种方法的唯一问题是计算出的时间仅在装饰器本身内可用。 I had searched on how to pass this back to the decorated function which doesn't appear to help because the end time is calculated after the return value of method is assigned to result .我已经搜索了如何将其传递回装饰的 function这似乎没有帮助,因为结束时间是在method的返回值分配给result之后计算的。

Perhaps you could solve this by writing that value to a log or a database, if required.如果需要,也许您可以通过将该值写入日志或数据库来解决此问题。 Sorry this is as far as my skills take me:)抱歉,就我的技能而言,这就是我的能力:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM