Commit cd3e712c authored by Giorgos Korfiatis's avatar Giorgos Korfiatis
Browse files

astakos: Cache visible resources in GET /quotas

Speed up GET /quotas by caching the list of API visible resources.
For consistency, apply the cache in GET /resources, too.
parent 3a8fe241
......@@ -38,7 +38,9 @@ from django.db import transaction
from snf_django.lib import api
from snf_django.lib.api.faults import BadRequest, ItemNotFound
from django.core.cache import cache
from astakos.im import settings
from astakos.im import register
from astakos.im.quotas import get_user_quotas, service_get_quotas
......@@ -49,10 +51,20 @@ from .util import (json_response, is_integer, are_integer,
user_from_token, component_from_token)
def get_visible_resources():
key = "resources"
result = cache.get(key)
if result is None:
cache.set(key, register.get_api_visible_resources(),
settings.RESOURCE_CACHE_TIMEOUT)
result = cache.get(key)
return result
@api.api_method(http_method='GET', token_required=True, user_required=False)
@user_from_token
def quotas(request):
visible_resources = register.get_api_visible_resources()
visible_resources = get_visible_resources()
resource_names = [r.name for r in visible_resources]
result = get_user_quotas(request.user, resources=resource_names)
return json_response(result)
......@@ -73,7 +85,7 @@ def service_quotas(request):
@api.api_method(http_method='GET', token_required=False, user_required=False)
def resources(request):
resources = register.get_api_visible_resources()
resources = get_visible_resources()
result = register.resources_to_dict(resources)
return json_response(result)
......
......@@ -216,3 +216,7 @@ ADMIN_STATS_PERMITTED_GROUPS = getattr(settings,
ENDPOINT_CACHE_TIMEOUT = getattr(settings,
'ASTAKOS_ENDPOINT_CACHE_TIMEOUT',
60)
RESOURCE_CACHE_TIMEOUT = getattr(settings,
'ASTAKOS_RESOURCE_CACHE_TIMEOUT',
60)
......@@ -142,3 +142,6 @@
## Timeout in seconds for caching endpoints in POST /tokens
# ASTAKOS_ENDPOINT_CACHE_TIMEOUT = 60
## Timeout in seconds for caching visible resources in GET /quotas
# ASTAKOS_RESOURCE_CACHE_TIMEOUT = 60
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment