Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@arusahni
Last active December 28, 2022 13:12
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arusahni/9434953 to your computer and use it in GitHub Desktop.
Save arusahni/9434953 to your computer and use it in GitHub Desktop.
Flask nocache decorator
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)
@agnosticdev
Copy link

This works great! Thank you very much.

@sharadbhat
Copy link

Thanks a lot!

@flniu
Copy link

flniu commented Nov 13, 2020

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

@flniu
Copy link

flniu commented Nov 13, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment