From 41dfa3e5527bc3e446d2fb2f0e15962b0ac28d2f Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Thu, 12 Dec 2013 21:45:22 -0500 Subject: [PATCH] Cache anonymous user id on the user object, so that queries aren't made many times over during module rendering --- common/djangoapps/student/models.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 8f1ac09a0651..8921a078ba4f 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -66,6 +66,10 @@ def anonymous_id_for_user(user, course_id): if user.is_anonymous(): return None + cached_id = getattr(user, '_anonymous_id', {}).get(course_id) + if cached_id is not None: + return cached_id + # include the secret key as a salt, and to make the ids unique across different LMS installs. hasher = hashlib.md5() hasher.update(settings.SECRET_KEY) @@ -94,6 +98,11 @@ def anonymous_id_for_user(user, course_id): # continue pass + if not hasattr(user, '_anonymous_id'): + user._anonymous_id = {} + + user._anonymous_id[course_id] = digest + return digest