Commit b54ff425 by David Ormsbee

add caching to User and UserProfile

--HG--
branch : dormsbee_performance
parent 463ba9a1
...@@ -9,6 +9,7 @@ from lxml import etree ...@@ -9,6 +9,7 @@ from lxml import etree
try: # This lets us do __name__ == ='__main__' try: # This lets us do __name__ == ='__main__'
from django.conf import settings from django.conf import settings
from django.core.cache import cache
from student.models import UserProfile from student.models import UserProfile
from student.models import UserTestGroup from student.models import UserTestGroup
from mitxmako.shortcuts import render_to_response, render_to_string from mitxmako.shortcuts import render_to_response, render_to_string
...@@ -144,7 +145,16 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None): ...@@ -144,7 +145,16 @@ def propogate_downward_tag(element, attribute_name, parent_attribute = None):
def user_groups(user): def user_groups(user):
# TODO: Rewrite in Django # 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): def course_xml_process(tree):
''' Do basic pre-processing of an XML tree. Assign IDs to all ''' Do basic pre-processing of an XML tree. Assign IDs to all
...@@ -161,7 +171,7 @@ def course_file(user): ...@@ -161,7 +171,7 @@ def course_file(user):
''' Given a user, return course.xml ''' Given a user, return course.xml
''' '''
# TODO: Cache. # TODO: Cache.
filename = UserProfile.objects.get(user=user).courseware filename = user.profile_cache.courseware # UserProfile.objects.get(user=user).courseware
groups = user_groups(user) groups = user_groups(user)
......
...@@ -12,6 +12,8 @@ file and check it in at the same time as your model changes. To do that, ...@@ -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.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from cache_toolbox import cache_model, cache_relation
class StudentModule(models.Model): class StudentModule(models.Model):
# For a homework problem, contains a JSON # For a homework problem, contains a JSON
# object consisting of state # object consisting of state
...@@ -50,3 +52,4 @@ class StudentModule(models.Model): ...@@ -50,3 +52,4 @@ class StudentModule(models.Model):
return self.module_type+'/'+self.student.username+"/"+self.module_id+'/'+str(self.state)[:20] return self.module_type+'/'+self.student.username+"/"+self.module_id+'/'+str(self.state)[:20]
cache_model(StudentModule)
\ No newline at end of file
...@@ -247,7 +247,7 @@ def profile(request): ...@@ -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, context={'name':user_info.name,
'username':request.user.username, 'username':request.user.username,
'location':user_info.location, 'location':user_info.location,
......
...@@ -13,6 +13,8 @@ import uuid ...@@ -13,6 +13,8 @@ import uuid
from django.db import models from django.db import models
from django.contrib.auth.models import User from django.contrib.auth.models import User
from cache_toolbox import cache_model, cache_relation
class UserProfile(models.Model): class UserProfile(models.Model):
class Meta: class Meta:
db_table = "auth_userprofile" db_table = "auth_userprofile"
...@@ -20,7 +22,7 @@ class UserProfile(models.Model): ...@@ -20,7 +22,7 @@ class UserProfile(models.Model):
## CRITICAL TODO/SECURITY ## CRITICAL TODO/SECURITY
# Sanitize all fields. # Sanitize all fields.
# This is not visible to other users, but could introduce holes later # 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) name = models.CharField(blank=True, max_length=255, db_index=True)
language = 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) location = models.CharField(blank=True, max_length=255, db_index=True)
...@@ -54,3 +56,4 @@ class Registration(models.Model): ...@@ -54,3 +56,4 @@ class Registration(models.Model):
self.user.save() self.user.save()
#self.delete() #self.delete()
cache_relation(User.profile)
...@@ -86,7 +86,7 @@ def logout_user(request): ...@@ -86,7 +86,7 @@ def logout_user(request):
def change_setting(request): def change_setting(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return redirect('/') 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: if 'location' in request.POST:
# print "loc" # print "loc"
up.location=request.POST['location'] up.location=request.POST['location']
...@@ -171,7 +171,7 @@ def create_account(request, post_override=None): ...@@ -171,7 +171,7 @@ def create_account(request, post_override=None):
u.save() u.save()
r.register(u) r.register(u)
up=UserProfile(user=u) up = UserProfile(user=u)
up.name=post_vars['name'] up.name=post_vars['name']
up.language=post_vars['language'] up.language=post_vars['language']
up.location=post_vars['location'] up.location=post_vars['location']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment