thadeusb / flask-cache

Cache extension for Flask
http://packages.python.org/Flask-Cache/
Other
697 stars 185 forks source link

flask caching - Set permanent memory location of stored cache data #206

Open Callum-Freeburn27 opened 3 years ago

Callum-Freeburn27 commented 3 years ago

I am trying to store a class instance that is created in one request and then accessed in other requests. As the first request will determine the criteria of the class object. I have tried to implement the cache flask module with the code below.

cache.init_app(app=app, config={"CACHE_TYPE": "simple",'CACHE_DIR': Path('/tmp')})

@app.route('/init/') def init(env): if env == "test": cache.set("currentENV", service('chromedriver', service.test)) response.IsSuccess = True elif env == "real": cache.set("currentENV", service('chromedriver', service.real)) currentENV = cache.get("currentENV") print(currentENV) the class instance will then be accessed in another request as below

@app.route('/login//') def login(MSISDN, password): currentENV = cache.get("currentENV") print(currentENV) response = currentENV.login(MSISDN, password) print(currentENV) return json.loads(response.toJson())

However the problem is that is says the class instance is missing, I have printed the class instance out and it says that they are in differnet memory location. I am very unsure why this is happening. I believe this is the issue and If i can permanently set the memory location of the instance, it would solve my problem. Or if there another way to pass a class instance to differnet requests that would solve my problem as well

<Connect.service object at 0x7fc0a619a910> 127.0.0.1 - - [20/Aug/2021 15:41:42] "GET /login/user1/user1 HTTP/1.1" 200 - <Connect.service object at 0x7fc0a619a370> I have tried the solutions from both these pages and have had no luck.

Store large data or a service connection per Flask session

Are global variables thread-safe in Flask? How do I share data between requests?