From b54ff42532e61dba7d9de8276ee7463191f7e060 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 23 Feb 2012 20:09:15 -0500 Subject: [PATCH] add caching to User and UserProfile --HG-- branch : dormsbee_performance --- courseware/content_parser.py | 14 ++++++++++++-- courseware/models.py | 3 +++ courseware/views.py | 2 +- student/models.py | 5 ++++- student/views.py | 4 ++-- 5 files changed, 22 insertions(+), 6 deletions(-) diff --git a/courseware/content_parser.py b/courseware/content_parser.py index 78d435557a..07ad98a64b 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -9,6 +9,7 @@ from lxml import etree try: # This lets us do __name__ == ='__main__' from django.conf import settings + from django.core.cache import cache from student.models import UserProfile from student.models import UserTestGroup from mitxmako.shortcuts import render_to_response, render_to_string @@ -144,7 +145,16 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None): def user_groups(user): # TODO: Rewrite in Django - return [u.name for u in UserTestGroup.objects.raw("select * from auth_user, student_usertestgroup, student_usertestgroup_users where auth_user.id = student_usertestgroup_users.user_id and student_usertestgroup_users.usertestgroup_id = student_usertestgroup.id and auth_user.id = %s", [user.id])] + key = 'user_group_names_{user.id}'.format(user=user) + cache_expiration = 60 * 60 * 4 # four hours + group_names = cache.get(key) + if group_names is None: + group_names = [u.name for u in UserTestGroup.objects.filter(users=user)] + cache.set(key, group_names, cache_expiration) + + return group_names + + # return [u.name for u in UserTestGroup.objects.raw("select * from auth_user, student_usertestgroup, student_usertestgroup_users where auth_user.id = student_usertestgroup_users.user_id and student_usertestgroup_users.usertestgroup_id = student_usertestgroup.id and auth_user.id = %s", [user.id])] def course_xml_process(tree): ''' Do basic pre-processing of an XML tree. Assign IDs to all @@ -161,7 +171,7 @@ def course_file(user): ''' Given a user, return course.xml ''' # TODO: Cache. - filename = UserProfile.objects.get(user=user).courseware + filename = user.profile_cache.courseware # UserProfile.objects.get(user=user).courseware groups = user_groups(user) diff --git a/courseware/models.py b/courseware/models.py index a41e58eb81..3fdaabe4d8 100644 --- a/courseware/models.py +++ b/courseware/models.py @@ -12,6 +12,8 @@ file and check it in at the same time as your model changes. To do that, from django.db import models from django.contrib.auth.models import User +from cache_toolbox import cache_model, cache_relation + class StudentModule(models.Model): # For a homework problem, contains a JSON # object consisting of state @@ -50,3 +52,4 @@ class StudentModule(models.Model): return self.module_type+'/'+self.student.username+"/"+self.module_id+'/'+str(self.state)[:20] +cache_model(StudentModule) \ No newline at end of file diff --git a/courseware/views.py b/courseware/views.py index b6f2644123..fcf3bfe9e5 100644 --- a/courseware/views.py +++ b/courseware/views.py @@ -247,7 +247,7 @@ def profile(request): ] - user_info=UserProfile.objects.get(user=request.user) + user_info = request.user.profile_cache # UserProfile.objects.get(user=request.user) context={'name':user_info.name, 'username':request.user.username, 'location':user_info.location, diff --git a/student/models.py b/student/models.py index b9554369bc..8c9a71c753 100644 --- a/student/models.py +++ b/student/models.py @@ -13,6 +13,8 @@ import uuid from django.db import models from django.contrib.auth.models import User +from cache_toolbox import cache_model, cache_relation + class UserProfile(models.Model): class Meta: db_table = "auth_userprofile" @@ -20,7 +22,7 @@ class UserProfile(models.Model): ## CRITICAL TODO/SECURITY # Sanitize all fields. # This is not visible to other users, but could introduce holes later - user = models.ForeignKey(User, unique=True, db_index=True) + user = models.OneToOneField(User, unique=True, db_index=True, related_name='profile') name = models.CharField(blank=True, max_length=255, db_index=True) language = models.CharField(blank=True, max_length=255, db_index=True) location = models.CharField(blank=True, max_length=255, db_index=True) @@ -54,3 +56,4 @@ class Registration(models.Model): self.user.save() #self.delete() +cache_relation(User.profile) diff --git a/student/views.py b/student/views.py index da82ee3ad1..5e3cb7b9b0 100644 --- a/student/views.py +++ b/student/views.py @@ -86,7 +86,7 @@ def logout_user(request): def change_setting(request): if not request.user.is_authenticated(): return redirect('/') - up=UserProfile.objects.get(user=request.user) + up = request.user.profile_cache # UserProfile.objects.get(user=request.user) if 'location' in request.POST: # print "loc" up.location=request.POST['location'] @@ -171,7 +171,7 @@ def create_account(request, post_override=None): u.save() r.register(u) - up=UserProfile(user=u) + up = UserProfile(user=u) up.name=post_vars['name'] up.language=post_vars['language'] up.location=post_vars['location']