Improve readability of cached_endpoint.

Switched the conditional and pulled it out into a separate function
so I can return early and dedent the rest.
master
voussoir 2021-01-05 13:07:03 -08:00
parent b9ad785f4d
commit af73bc580f
1 changed files with 19 additions and 13 deletions

View File

@ -40,21 +40,27 @@ def cached_endpoint(max_age):
}
def wrapper(function):
@functools.wraps(function)
def wrapped(*args, **kwargs):
if (not state['max_age']) or (time.time() - state['last_run'] > state['max_age']):
def get_value(*args, **kwargs):
if state['max_age'] and (time.time() - state['last_run']) > state['max_age']:
return state['stored_value']
value = function(*args, **kwargs)
if isinstance(value, flask.Response):
if value.headers.get('Content-Type'):
state['headers']['Content-Type'] = value.headers.get('Content-Type')
value = value.response
if value != state['stored_value']:
state['stored_value'] = value
state['stored_etag'] = passwordy.random_hex(20)
state['headers']['ETag'] = state['stored_etag']
state['last_run'] = time.time()
else:
value = state['stored_value']
return value
@functools.wraps(function)
def wrapped(*args, **kwargs):
value = get_value(*args, **kwargs)
client_etag = request.headers.get('If-None-Match', None)
if client_etag == state['stored_etag']: