1

I am trying to cache 2 functions in flask app which I successfully did, using cachetools. The format of URL request to API is something like: URL- '***.com/rec/some_id/0/23413444;2134134124;4352352345'

But this logic have some limitations when I want to cache those function and the URL request changes. It would be best if those functions are cached till the time the 'some_id' in URL remains same. As soon as the 'some_id' is changed and passed through URL it must start from beginning without using the cached function.

Here is what I tried.

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)

What must be done to implement the desired logic? It would be great if someone could help me on this.

MK-MSA
  • 11
  • 1

0 Answers0