Skip to content

Instantly share code, notes, and snippets.

@bulv1ne
Created September 23, 2013 22:29
Show Gist options
  • Save bulv1ne/6677855 to your computer and use it in GitHub Desktop.
Save bulv1ne/6677855 to your computer and use it in GitHub Desktop.
Flask decorator for just caching and not caring about anything else
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