Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
fd75b49b
Commit
fd75b49b
authored
Feb 24, 2012
by
David Ormsbee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add caching for StudentModule
--HG-- branch : dormsbee_performance
parent
b54ff425
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
30 additions
and
0 deletions
+30
-0
courseware/models.py
+30
-0
No files found.
courseware/models.py
View file @
fd75b49b
...
...
@@ -8,12 +8,18 @@ file and check it in at the same time as your model changes. To do that,
2. ./manage.py schemamigration courseware --auto description_of_your_change
3. Add the migration file created in mitx/courseware/migrations/
ASSUMPTIONS: modules have unique IDs, even across different module_types
"""
from
django.db
import
models
from
django.core.cache
import
cache
from
django.contrib.auth.models
import
User
from
cache_toolbox
import
cache_model
,
cache_relation
CACHE_TIMEOUT
=
60
*
60
*
4
# Set the cache timeout to be four hours
class
StudentModule
(
models
.
Model
):
# For a homework problem, contains a JSON
# object consisting of state
...
...
@@ -51,5 +57,28 @@ class StudentModule(models.Model):
def
__unicode__
(
self
):
return
self
.
module_type
+
'/'
+
self
.
student
.
username
+
"/"
+
self
.
module_id
+
'/'
+
str
(
self
.
state
)[:
20
]
@classmethod
def
get_from_cache
(
cls
,
student
,
module_ids
):
k
=
cls
.
key_for
(
student
,
module_ids
)
student_modules
=
k
.
get
(
k
)
if
student_modules
is
None
:
student_modules
=
StudentModule
.
objects
.
filter
(
student
=
student
,
module_id__in
=
module_ids
)
# It's possible it really doesn't exist...
if
student_modules
is
not
None
:
k
.
set
(
k
,
student_modules
,
CACHE_TIMEOUT
)
return
student_modules
@classmethod
def
clear_cache_for
(
cls
,
student
,
module_ids
):
k
=
cls
.
key_for
(
student_id
,
module_ids
)
cache
.
delete
(
k
)
@classmethod
def
key_for
(
cls
,
student
,
module_ids
):
module_ids_hash
=
md5
(
","
.
join
(
sorted
(
modules_ids
)))
.
hexdigest
()
return
"StudentModule-student_id:{0};module_ids_hash:{1}"
.
format
(
student
.
id
,
module_ids_hash
)
cache_model
(
StudentModule
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment