This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
devicehub-teal/ereuse_devicehub/teal/cache.py
Cayo Puigdefabregas e6c07851d4 fix lint checks
2023-03-27 10:55:26 +02:00

29 lines
705 B
Python

import datetime
from functools import wraps
from flask import make_response
def cache(expires: datetime.timedelta = None):
"""Sets HTTP cache for now + passed-in time.
Example usage::
@app.route('/map')
@header_cache(expires=datetime.datetime(seconds=50))
def index():
return render_template('index.html')
"""
def cache_decorator(view):
@wraps(view)
def cache_func(*args, **kwargs):
r = make_response(view(*args, **kwargs))
r.expires = datetime.datetime.now(datetime.timezone.utc) + expires
r.cache_control.public = True
return r
return cache_func
return cache_decorator