flask-cache icon indicating copy to clipboard operation
flask-cache copied to clipboard

flask caching - Set permanent memory location of stored cache data

Open Callum-Freeburn27 opened this issue 4 years ago • 0 comments

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?

Callum-Freeburn27 avatar Aug 20 '21 06:08 Callum-Freeburn27