From 5d4ef292d68ef2bd94c7005435560af6c36c1a84 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Fri, 7 Feb 2020 09:06:02 +0000 Subject: [PATCH] Move caching to memcached --- apiserver/apiserver/api/utils.py | 4 +++- apiserver/apiserver/api/views.py | 10 +++++++--- apiserver/apiserver/settings.py | 8 ++++++++ apiserver/docs/source/dev.rst | 5 ++++- apiserver/requirements.txt | 1 + 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/apiserver/apiserver/api/utils.py b/apiserver/apiserver/api/utils.py index 17682f0..d549f8e 100644 --- a/apiserver/apiserver/api/utils.py +++ b/apiserver/apiserver/api/utils.py @@ -11,6 +11,7 @@ from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import letter from django.db.models import Sum +from django.core.cache import cache from . import models, serializers try: @@ -126,17 +127,18 @@ def tally_membership_months(member, fake_date=None): return True -search_strings = {} def gen_search_strings(): ''' Generate a cache dict of names to member ids for rapid string matching ''' + search_strings = {} for m in models.Member.objects.all(): string = '{} {}'.format( m.preferred_name, m.last_name, ).lower() search_strings[string] = m.id + cache.set('search_strings', search_strings) LARGE_SIZE = 1080 diff --git a/apiserver/apiserver/api/views.py b/apiserver/apiserver/api/views.py index 24ac485..a40e8af 100644 --- a/apiserver/apiserver/api/views.py +++ b/apiserver/apiserver/api/views.py @@ -3,6 +3,7 @@ from django.shortcuts import get_object_or_404 from django.db.models import Max from django.http import HttpResponse from django.core.files.base import File +from django.core.cache import cache from rest_framework import viewsets, views, mixins, generics, exceptions from rest_framework.decorators import action from rest_framework.permissions import BasePermission, IsAuthenticated, SAFE_METHODS @@ -50,11 +51,13 @@ class SearchViewSet(Base, Retrieve): queryset = models.Member.objects.all() search = self.request.data.get('q', '').lower() - if not utils.search_strings: + if not cache.touch('search_strings'): utils.gen_search_strings() # init cache + search_strings = cache.get('search_strings', {}) + if len(search): - choices = utils.search_strings.keys() + choices = search_strings.keys() # get exact starts with matches results = [x for x in choices if x.startswith(search)] @@ -69,7 +72,7 @@ class SearchViewSet(Base, Retrieve): # remove dupes, truncate list results = list(OrderedDict.fromkeys(results))[:NUM_SEARCH_RESULTS] - result_ids = [utils.search_strings[x] for x in results] + result_ids = [search_strings[x] for x in results] result_objects = [queryset.get(id=x) for x in result_ids] queryset = result_objects @@ -308,6 +311,7 @@ class IpnView(views.APIView): return Response(200) + class RegistrationView(RegisterView): serializer_class = serializers.RegistrationSerializer diff --git a/apiserver/apiserver/settings.py b/apiserver/apiserver/settings.py index 359bc88..e2240ee 100644 --- a/apiserver/apiserver/settings.py +++ b/apiserver/apiserver/settings.py @@ -117,6 +117,14 @@ DATABASES = { } } +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', + 'LOCATION': '127.0.0.1:11211', + 'TIMEOUT': None, + } +} + # Password validation # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators diff --git a/apiserver/docs/source/dev.rst b/apiserver/docs/source/dev.rst index aa6b4a8..2664128 100644 --- a/apiserver/docs/source/dev.rst +++ b/apiserver/docs/source/dev.rst @@ -9,8 +9,11 @@ Install dependencies: .. sourcecode:: bash - # Python: + # Misc: $ sudo apt update + $ sudo apt install memcached + + # Python: $ sudo apt install python3 python3-pip python-virtualenv python3-virtualenv # Yarn / nodejs: diff --git a/apiserver/requirements.txt b/apiserver/requirements.txt index 9c34821..3f7dbe5 100644 --- a/apiserver/requirements.txt +++ b/apiserver/requirements.txt @@ -28,6 +28,7 @@ pyparsing==2.4.6 PyPDF2==1.26.0 python-dateutil==2.8.1 python-Levenshtein==0.12.0 +python-memcached==1.59 python3-openid==3.1.0 pytz==2019.3 reportlab==3.5.34