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

29 lines
705 B
Python
Raw Permalink Normal View History

2023-03-21 11:08:13 +00:00
import datetime
from functools import wraps
2023-03-27 08:55:26 +00:00
from flask import make_response
2023-03-21 11:08:13 +00:00
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):
2023-03-27 08:55:26 +00:00
r = make_response(view(*args, **kwargs))
2023-03-21 11:08:13 +00:00
r.expires = datetime.datetime.now(datetime.timezone.utc) + expires
r.cache_control.public = True
return r
return cache_func
return cache_decorator