How to configure session in python flask using cachelib
What is a Session?
Session in real life and computer world mean the same i.e. total time for an activity. Lets say I have 1 hour of swimming session, similar way the time between user logs in and user logs out to perform activities that application offers.
How to manage a Session? or What is Session Management?
Managing a session by storing data on both client and server side. Data that helps managing
1. Authentication
2. Authorization
3. Security
4. Access-Control
5. History etc.
As mentioned earlier, there are two types of sessions
Client Side Session
Storing the session or user related data on the client side i.e. On browser for web application, on mobile for Mobile application and on client machine for computer based applications in the local storage or cookies. On client side, we should avoid storing sensitive information directly as it is accessible to everyone who uses the client. Any ways, we can store encrypted data.
Server Side Session
Storing the session or user related data on the server side so that response is sent to right client. In some cases, session history like chat history is also saved as it helps AI models to get context so that it can answer better for Chatgpt like applications.
Now lets jump in to coding.
In the following flask application will store the server side sessions on the filesystem of server using Cachelib (new update)
Install virtual environment
python -m venv venv
Activate virtual environment
venv\Scripts\activate (Windows)
Install required packages
pip install flask flask-session cachelib
as we are storing session on server file system so, need to install cachelib
Create a file called flask_app.py and add the following code
# Import packages
from flask import Flask, make_response, session
from flask_session import Session
from cachelib.file import FileSystemCache
import os
# Initiating the Flask app
app = Flask(__name__)
# Flask session configuration variables
SESSION_TYPE = 'cachelib'
SESSION_SERIALIZATION_FORMAT = 'json'
SESSION_CACHELIB = FileSystemCache(threshold=500, cache_dir=f"{os.path.dirname(__file__)}/sessions")
app.config.from_object(__name__)
# Initiate the session
Session(app)
# add some routes
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
@app.route("/set-session")
def set_session():
session['id'] = '12345'
return make_response({"response":"id session key is set"},200)
@app.route("/get-session")
def get_session():
return make_response({"response":f"the id is {session.get('id')}"},200)
Start the server by running the command
flask --app flask_app run
in the browser, go to url http://127.0.0.1:5000/ and you should see Hello, World
Now that we confirmed Server is running, Go to http://127.0.0.1:5000/ set-session , This will set a session variable id = ‘12345
and to retrieve the session variable, go to http://127.0.0.1:5000/ get-session and you will see something like this
Next steps is to try different configuration by refereeing to flask-session docs.