Add caching support to catalog

We will now cache the results of the catalog for 60s and not hit the database at all if cached
This commit is contained in:
Joseph Schorr 2018-06-19 13:27:16 -04:00
parent a1c06042c6
commit 2caaf84f31
4 changed files with 53 additions and 10 deletions

View file

@ -2,10 +2,13 @@ import features
from flask import jsonify
from auth.auth_context import get_authenticated_user
from app import model_cache
from auth.auth_context import get_authenticated_user, get_authenticated_context
from auth.registry_jwt_auth import process_registry_jwt_auth
from data.cache import cache_key
from endpoints.decorators import anon_protect
from endpoints.v2 import v2_bp, paginate
from endpoints.v2.models_interface import Repository
from endpoints.v2.models_pre_oci import data_model as model
@ -14,16 +17,23 @@ from endpoints.v2.models_pre_oci import data_model as model
@anon_protect
@paginate()
def catalog_search(start_id, limit, pagination_callback):
include_public = bool(features.PUBLIC_CATALOG)
if not include_public and not get_authenticated_user():
return jsonify({'repositories': []})
def _load_catalog():
include_public = bool(features.PUBLIC_CATALOG)
if not include_public and not get_authenticated_user():
return []
username = get_authenticated_user().username if get_authenticated_user() else None
if username and not get_authenticated_user().enabled:
return jsonify({'repositories': []})
username = get_authenticated_user().username if get_authenticated_user() else None
if username and not get_authenticated_user().enabled:
return []
repos = model.get_visible_repositories(username, start_id, limit, include_public=include_public)
return [repo._asdict() for repo in repos]
context_key = get_authenticated_context().unique_key if get_authenticated_context() else None
catalog_cache_key = cache_key.for_catalog_page(context_key, start_id, limit)
visible_repositories = [Repository(**repo_dict) for repo_dict
in model_cache.retrieve(catalog_cache_key, _load_catalog)]
visible_repositories = model.get_visible_repositories(username, start_id, limit,
include_public=include_public)
response = jsonify({
'repositories': ['%s/%s' % (repo.namespace_name, repo.name)
for repo in visible_repositories][0:limit],