Commit b65bff93 by Will Daly

Version assessment model cache keys

parent 23e49f80
...@@ -7,7 +7,6 @@ from copy import deepcopy ...@@ -7,7 +7,6 @@ from copy import deepcopy
import logging import logging
from django.core.cache import cache from django.core.cache import cache
from django.utils.translation import ugettext as _
from rest_framework import serializers from rest_framework import serializers
from openassessment.assessment.models import ( from openassessment.assessment.models import (
Assessment, AssessmentPart, Criterion, CriterionOption, Rubric, Assessment, AssessmentPart, Criterion, CriterionOption, Rubric,
...@@ -19,6 +18,26 @@ from openassessment.assessment.models import ( ...@@ -19,6 +18,26 @@ from openassessment.assessment.models import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# Current version of the models in the cache
# Increment this to ignore assessment models currently in the cache
# when model fields change.
CACHE_VERSION = 1
def _versioned_cache_key(key):
"""
Add a version number to a cache key, so we
Args:
key (unicode): The original, unversioned, cache key.
Returns:
unicode: Cache key with the version appended.
"""
return u"{}.v{}".format(key, CACHE_VERSION)
class InvalidRubric(Exception): class InvalidRubric(Exception):
"""This can be raised during the deserialization process.""" """This can be raised during the deserialization process."""
def __init__(self, errors): def __init__(self, errors):
...@@ -202,9 +221,11 @@ def full_assessment_dict(assessment, rubric_dict=None): ...@@ -202,9 +221,11 @@ def full_assessment_dict(assessment, rubric_dict=None):
Returns: Returns:
dict with keys 'rubric' (serialized Rubric model) and 'parts' (serialized assessment parts) dict with keys 'rubric' (serialized Rubric model) and 'parts' (serialized assessment parts)
""" """
assessment_cache_key = "assessment.full_assessment_dict.{}.{}.{}".format( assessment_cache_key = _versioned_cache_key(
"assessment.full_assessment_dict.{}.{}.{}".format(
assessment.id, assessment.submission_uuid, assessment.scored_at.isoformat() assessment.id, assessment.submission_uuid, assessment.scored_at.isoformat()
) )
)
assessment_dict = cache.get(assessment_cache_key) assessment_dict = cache.get(assessment_cache_key)
if assessment_dict: if assessment_dict:
return assessment_dict return assessment_dict
......
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