Commit 33c5f1825ca3e232e4370cb91b31ebb7bcb3abb7

Authored by Andrew Buss
1 parent 5a0ce27d14
Exists in master

somewhat smarter cache rice

Showing 2 changed files with 14 additions and 10 deletions Side-by-side Diff

flashcards/models.py View file @ 33c5f18
1 1 from django.contrib.auth.models import AbstractUser, UserManager
2 2 from django.contrib.auth.tokens import default_token_generator
  3 +from django.core.cache import cache
3 4 from django.core.exceptions import ValidationError
4 5 from django.core.exceptions import PermissionDenied, SuspiciousOperation
5 6 from django.core.mail import send_mail
... ... @@ -53,8 +54,6 @@
53 54 return self._create_user(email, password, True, True, **extra_fields)
54 55  
55 56  
56   -
57   -
58 57 class User(AbstractUser, SimpleEmailConfirmationUserMixin):
59 58 """
60 59 An extension of Django's default user model.
... ... @@ -332,9 +331,17 @@
332 331  
333 332 @property
334 333 def lecture_times(self):
335   - lecture_periods = self.lectureperiod_set.all()
336   - if not lecture_periods.exists(): return ''
337   - return ''.join(map(lambda x: x.weekday_letter, lecture_periods)) + ' ' + lecture_periods[0].short_start_time
  334 + data = cache.get("section_%d_lecture_times" % self.pk)
  335 + if not data:
  336 + lecture_periods = self.lectureperiod_set.all()
  337 + if lecture_periods.exists():
  338 + data = ''.join(map(lambda x: x.weekday_letter, lecture_periods)) + ' ' + lecture_periods[
  339 + 0].short_start_time
  340 + else:
  341 + data = ''
  342 + cache.set("section_%d_lecture_times" % self.pk, data, 24*60*60)
  343 + return data
  344 +
338 345  
339 346 @property
340 347 def long_name(self):
flashcards/views.py View file @ 33c5f18
... ... @@ -79,11 +79,8 @@
79 79 """
80 80 query = request.GET.get('q', None)
81 81 if not query: return Response('[]')
82   - data = cache.get("section_search_"+query)
83   - if not data:
84   - qs = Section.search(query.split(' '))[:20]
85   - data = SectionSerializer(qs, many=True).data
86   - cache.set("section_search_"+query, data, 10*60)
  82 + qs = Section.search(query.split(' '))[:20]
  83 + data = SectionSerializer(qs, many=True).data
87 84 return Response(data)
88 85  
89 86 @detail_route(methods=['GET'])