3

I am trying to cache the result of a time consuming request.

First I have a flask template as follow :

@app.route("/")
@app.route("/tabs", methods=['GET','POST'])
def tab():
return render_template("tabs.html")

@app.route("/graph", methods=['GET','POST'])
def graph():
#Some code
return render_template("chart.html", the_div=div, the_script=script,
                       form=form, tables=table, titles = testyear)

@app.route("/prices", methods=['GET','POST'])
def prices():
#Some other code
return render_template("prices.html", PlotGroup=PlotGroup, 
ScriptGroup=ScriptGroup, DivGroup=DivGroup)

I have at the top of my code initialized the app, the cache and a time_out :

# Checking is prod to change server from 5000 to 5001
IS_PROD = sys.argv[1] == "prod"
# Setting up cache timer
CACHE_TIMEOUT = 20
# Defining the Flask App
app = Flask(__name__, template_folder='Template')
# define the cache config :
app.config['CACHE_TYPE'] = 'simple'
app.cache = Cache(app)

I have also created a config class :

class Config(object):
JOBS = [
    {
        'id' : 'refresh_cache',
        'func' : 'main:get_my_cache',
        'trigger' : 'interval',
        'seconds' : 5
    }
]

SCHEDULER_API_ENABLED = True

With a function "get_my_cache()" defined as below :

@app.cache.cached(timeout = CACHE_TIMEOUT, key_prefix='my-cache')
def get_my_cache():
cacheval = app.cache.get('my-cache')
print(cacheval)
if cacheval is None:
    #cacheval1, cacheval2 = DataHandling.extract_full_table()
    cacheval1, cacheval2 = DataHandling.offlinedata()
    cacheval = [cacheval1, cacheval2]
    print("Cache updated at : " + time.strftime("%b %d %Y - %H:%M:%S"))
    app.cache.set('my-cache', [cacheval1, cacheval2])
return cacheval[0], cacheval[1]

In the main section I load everything :

if __name__ == '__main__':
app.config.from_object(Config())
scheduler = APScheduler()
scheduler.init_app(app)
scheduler.start()

if IS_PROD:
    app.run(host='0.0.0.0',debug=False, port=5000)
else:
    app.run(debug=True, port=5001)

So if I understand well from the timeline below :

None
Cache updated at : Jun 19 2017 - 11:25:58
None
Cache updated at : Jun 19 2017 - 11:26:23
None
Cache updated at : Jun 19 2017 - 11:26:25
127.0.0.1 - - [19/Jun/2017 11:26:25] "GET /graph HTTP/1.1" 200 -
  1. My scheduler is checking my cache every 5 seconds (timing is for testing it will be longer in reality) and I see effectively a cache update every 25 seconds.

  2. What is my problem is that when I refresh the page, I see a cache update after 2 sec of the last update... From my understanding it seems that there are two kind of cache : one for the page (localhost/graph) and another set up by the scheduler. Even if both are related with the same key_prefix...

I understood that the could be related to different threads? Could it be the issue?

davidism
  • 121,510
  • 29
  • 395
  • 339
Charles
  • 31
  • 3
  • I have runned some differend caching option, i am now using a TTLCache (in the cachetools package). I really see the thread issue. The APSheduler is using Thread-1 to Thread-10 while the Flask is running on MainThread. I have then changed the cache to "global cache" but without solving the problem. – Charles Jun 20 '17 at 09:44

1 Answers1

0
def task1(app):
with app.app_context():
    #cache.set("t1","123")
    x=cache.get("t1")
    print(x)

class Config(object):
JOBS = [ {  # add task1
        'id': 'job1',
        'func': '__main__:task1',
        'args': (3, 4,app),
        'trigger': 'interval',
        'seconds': 5,
    }]