Cache anonymous user id on the user object, so that queries aren't made many times over during module rendering

This commit is contained in:
Calen Pennington
2013-12-12 21:45:22 -05:00
parent 560347d0ef
commit 41dfa3e552

View File

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