From 13b457c4409fc536b83556d9258fb880d149bdd8 Mon Sep 17 00:00:00 2001 From: yackob03 Date: Sun, 20 Oct 2013 01:18:31 -0400 Subject: [PATCH] Add caching to the changes api since it is so expensive and can return large results. --- endpoints/api.py | 2 ++ util/cache.py | 12 ++++++++++++ 2 files changed, 14 insertions(+) create mode 100644 util/cache.py diff --git a/endpoints/api.py b/endpoints/api.py index edb49aa0e..e2229d81b 100644 --- a/endpoints/api.py +++ b/endpoints/api.py @@ -20,6 +20,7 @@ from auth.permissions import (ReadRepositoryPermission, AdministerRepositoryPermission) from endpoints import registry from endpoints.web import common_login +from util.cache import cache_control store = storage.load() @@ -380,6 +381,7 @@ def get_image(namespace, repository, image_id): @app.route('/api/repository//image//changes', methods=['GET']) +@cache_control(max_age=60*60) # Cache for one hour @parse_repository_name def get_image_changes(namespace, repository, image_id): permission = ReadRepositoryPermission(namespace, repository) diff --git a/util/cache.py b/util/cache.py new file mode 100644 index 000000000..96160a71e --- /dev/null +++ b/util/cache.py @@ -0,0 +1,12 @@ +from functools import wraps + + +def cache_control(max_age=55): + def wrap(f): + @wraps(f) + def add_max_age(*args, **kwargs): + response = f(*args, **kwargs) + response.headers['Cache-Control'] = 'max-age=%d' % max_age + return response + return add_max_age + return wrap