In order to cache some data, I am calling cache.set method. However it is throwing KeyError. The error log:
File \"D:\\Sample_Project\\SomeRouter.py\", line
The problem was I was accessing cache
object outside request context i.e. in "SomeRouter"
module, and because of which it was unaware that under which context it is been used.
In server
module where request has been received, the cache knows about the app application, but during cache.set(cKey, data)
in SomeRouter
module, it throws KeyError. This error is justified as mentioned above.
The resolution is to push the application context as below:
from server import app, cache
# Using context
with app.app_context():
cache.set(cKey, data)
This will push a new application context (using the application of app).
All thanks to Mark Hildreth, for his great answer on context in flask