Performance: Session data storage
This affects page load speed and interaction responsiveness by how session data is stored and accessed during requests.
from flask import session # Store only a small user ID in session cookie session['user_id'] = user.id # Retrieve full user data from server-side cache or database as needed
from flask import session # Storing large data directly in client-side cookie session session['user_data'] = large_user_object # This stores all data in cookie, sent with every request
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large client-side session cookie | Minimal | 0 | Low but delayed by network | [X] Bad |
| Small client-side session cookie + server data fetch | Minimal | 0 | Low and fast | [OK] Good |
| Server-side session with Redis | Minimal | 0 | Low and fast | [OK] Good |
app.secret_key to enable session encryption.app.secret_key = 'mysecret' is valid; others are invalid method or attribute names.from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'key'
@app.route('/')
def index():
session['count'] = session.get('count', 0) + 1
return str(session['count'])from flask import Flask, session
app = Flask(__name__)
@app.route('/')
def index():
session['user'] = 'Alice'
return session['user']app.secret_key to be set for security.app.secret_key, so session will not work properly.session['lang'] = 'en' is correct to store the language.session.get('lang', 'default') safely reads the value with a fallback if missing.Pass the test and this pattern moves unknown