Created
September 23, 2013 22:29
-
-
Save bulv1ne/6677855 to your computer and use it in GitHub Desktop.
Flask decorator for just caching and not caring about anything else
This file contains hidden or 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
import hashlib | |
from flask import request, make_response, Response | |
import datetime | |
from functools import wraps | |
def just_cache(f): | |
@wraps(f) | |
def _wrap(*args, **kwargs): | |
if 'If-Modified-Since' in request.headers: | |
return Response(status=304) | |
data = f(*args, **kwargs) | |
resp = make_response(data, 200) | |
resp.headers.add('Last-Modified', datetime.datetime(2013,9,1)) | |
resp.headers.add('Expires', datetime.datetime(2013,9,1) + datetime.timedelta(seconds=315360000)) | |
resp.headers.add('Cache-Control', 'public, max-age=315360000') | |
resp.headers.add('ETag', hashlib.sha1(data).hexdigest()) | |
return resp | |
return _wrap |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment