Last active
December 28, 2022 13:12
-
-
Save arusahni/9434953 to your computer and use it in GitHub Desktop.
Flask nocache decorator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import make_response | |
from functools import wraps, update_wrapper | |
from datetime import datetime | |
def nocache(view): | |
@wraps(view) | |
def no_cache(*args, **kwargs): | |
response = make_response(view(*args, **kwargs)) | |
response.headers['Last-Modified'] = datetime.now() | |
response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0' | |
response.headers['Pragma'] = 'no-cache' | |
response.headers['Expires'] = '-1' | |
return response | |
return update_wrapper(no_cache, view) |
To simplify, I found this code is OK:
def no_http_cache(view):
@wraps(view)
def no_cache_view(*args, **kwargs):
response = make_response(view(*args, **kwargs))
response.headers.set('Cache-Control', 'no-store, no-cache, must-revalidate, private, max-age=0')
return response
return no_cache_view
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks a lot!