Skip to content

Commit

Permalink
fix(quick): cache entire project directory request
Browse files Browse the repository at this point in the history
The projects don't change often, so we should be able to cache them for a while.
Additionally, every hour we will regenerate their cached values (take ~7m right now).

So the quick fix is two-fold:

 - cache the entire result and keep for an hour
 - regenerate per project cache every hour

Once the page cache expires, queries should take ~10-20 seconds, but only once until the next hour.

#4725: /rest/v1/project_directory performance
  • Loading branch information
MichaelAkvo committed Dec 9, 2021
1 parent 151cae5 commit 6a315de
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion akvo/rest/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from django.db.models import Q
from django.shortcuts import get_object_or_404
from django.views.decorators.cache import cache_page
from rest_framework.authentication import SessionAuthentication
from rest_framework.decorators import api_view, authentication_classes, permission_classes
from rest_framework.permissions import IsAuthenticated
Expand All @@ -15,7 +16,7 @@
from geojson import Feature, Point, FeatureCollection

from akvo.codelists.store.default_codelists import SECTOR_CATEGORY
from akvo.rest.cache import serialized_project
from akvo.rest.cache import PROJECT_DIRECTORY_CACHE, serialized_project
from akvo.rest.serializers import (ProjectSerializer, ProjectExtraSerializer,
ProjectExtraDeepSerializer,
ProjectIatiExportSerializer,
Expand Down Expand Up @@ -262,6 +263,8 @@ class ProjectUpViewSet(ProjectViewSet):
# Project directory
###############################################################################

# Cache for one hour
@cache_page(timeout=60 * 60, cache=PROJECT_DIRECTORY_CACHE)
@api_view(['GET'])
def project_directory(request):
"""Return the values for various project filters.
Expand Down
3 changes: 2 additions & 1 deletion akvo/settings/90-finish.conf
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,6 @@ CRONJOBS = [
('* * * * *', 'django.core.management.call_command', ['iati_import']),
('* * * * *', 'django.core.management.call_command', ['iati_export']),
('10 2 * * *', 'django.core.management.call_command', ['a4a_optimy_import']),
('*/5 * * * *', 'django.core.management.call_command', ['perform_iati_checks'])
('*/5 * * * *', 'django.core.management.call_command', ['perform_iati_checks']),
('0 */1 * * *', 'django.core.management.call_command', ['populate_project_directory_cache']),
]

0 comments on commit 6a315de

Please # to comment.