Move caching to memcached

This commit is contained in:
Tanner Collin 2020-02-07 09:06:02 +00:00
parent 28e79b9362
commit 5d4ef292d6
5 changed files with 23 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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:

View File

@ -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