基于url请求字符串缓存flask应用程序函数

2024-05-08 14:27:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用cachetools在flask应用程序中缓存2个函数,我成功地做到了这一点。这个 API的URL请求格式如下: URL-'***.com/rec/some_id/0/23413444;2134134124;435223345'

但是,当我想要缓存这些函数和URL请求更改时,这种逻辑有一些限制。最好将这些函数缓存到URL中的“some_id”保持不变为止。一旦“some_id”被更改并通过URL传递,它就必须从头开始,而不使用缓存函数

这是我试过的

from flask import Flask
from cachetools import cached, TTLCache

app = Flask(__name__)
cache1 = TTLCache(maxsize=100, ttl=60)
cache2 = TTLCache(maxsize=100, ttl=60)

@cached(cache1)
funtion1():
   do_something...
   return a

@cached(cache2)
function2():
   do_something...
   return b

@app.route('/rec/<some_id>/<itr>/<int_list:item>')
def get_rec(model_id,item,itr):
    a=function1()
    b=function2()
    result= jsonify(a,b)
    return result

If __name__ == '__main__':
    app.run(threaded=True)

为了实现所需的逻辑,必须做些什么?如果有人能在这方面帮助我,那就太好了


Tags: 函数fromimportidappurlflaskreturn