Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Updated caching to pass test when caching is enabled by default.
Browse files Browse the repository at this point in the history
  • Loading branch information
ShawnMilo committed Aug 11, 2015
1 parent 5b66ed9 commit e5e1c96
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
2 changes: 1 addition & 1 deletion lore/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,6 @@ def get_var(name, default):
},
"indexing": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache",
"TIMEOUT": get_var("CACHE_TIMEOUT", "0"),
"TIMEOUT": get_var("CACHE_TIMEOUT", "60"),
}
}
18 changes: 8 additions & 10 deletions search/search_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,17 @@
cache = caches["indexing"]


def get_course_metadata(course_id, force_update=False):
def get_course_metadata(course_id):
"""
Caches and returns course metadata.
Args:
course_id (int): Primary key of learningresources.models.Course
Returns:
data (dict): Metadata about course.
"""
if force_update is True:
cache.clear()
key = "course_metadata_{0}".format(course_id)
data = cache.get(key, {})
if data == {}:
log.debug("skm nothing in cache")
course = Course.objects.select_related("repository").get(id=course_id)
data["run"] = course.run
data["course_number"] = course.course_number
Expand All @@ -53,7 +50,7 @@ def get_course_metadata(course_id, force_update=False):
return data


def get_vocabs(course_id, resource_id, force_update=False):
def get_vocabs(course_id, resource_id, solo_update=False):
"""
Caches and returns taxonomy metadata for a course.
Args:
Expand All @@ -63,24 +60,25 @@ def get_vocabs(course_id, resource_id, force_update=False):
data (dict): Vocab/term data for course.
"""
key = "vocab_cache_{0}".format(resource_id)
if force_update is True:
cache.clear()
cached = cache.get(key)
if cached is not None:
if (solo_update is False) and (cached is not None):
return cached

# Pre-populate the cache with blank values in case there are no
# terms for that LearningResource. Otherwise, looking up the vocabularies
# for that resource will refill the cache for the entire course.
# for that resource will refill the cache for the entire course. If there
# is already a value, retain it.
resource_ids = LearningResource.objects.all().values_list('id', flat=True)
for resource_id in resource_ids:
rkey = "vocab_cache_{0}".format(resource_id)
cache.set(rkey, {})
cache.set(rkey, cache.get(rkey, {}))
value = {}

term_cache = defaultdict(lambda: defaultdict(list))
rels = LearningResource.terms.related.through.objects.select_related(
"term").filter(learningresource__course__id=course_id)
if solo_update is True:
rels = rels.filter(learningresource_id=resource_id)
for rel in rels.iterator():
term_cache[rel.learningresource_id][
rel.term.vocabulary_id].append(rel.term_id)
Expand Down
4 changes: 3 additions & 1 deletion search/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.db.models.signals import m2m_changed
from django.dispatch import receiver

from search.search_indexes import LearningResourceIndex
from search.search_indexes import LearningResourceIndex, get_vocabs

log = logging.getLogger(__name__)

Expand All @@ -23,4 +23,6 @@ def handle_m2m_save(sender, **kwargs):
instance = kwargs.pop("instance", None)
if instance.__class__.__name__ != "LearningResource":
return
# Update cache for the LearningResource if it's already set.
get_vocabs(instance.course_id, instance.id, solo_update=True)
LearningResourceIndex().update_object(instance)
19 changes: 8 additions & 11 deletions search/tests/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,36 +177,33 @@ def test_course_cache(self):
"""
Test caching -- enabled and disabled -- for course metadata.
"""
def three_times(force_update):
def three_times():
"""Get course metadata three times."""
for _ in range(0, 3):
get_course_metadata(self.course.id, force_update=force_update)
get_course_metadata(self.course.id)

set_cache_timeout(0)
with self.assertNumQueries(3):
three_times(force_update=True)
log.debug("skm ran three_times")
three_times()

set_cache_timeout(60)
with self.assertNumQueries(1):
three_times(force_update=False)
three_times()

def thrice(self):
"""Hit vocabs three times with and without caching."""
def three_times(force_update):
def three_times():
"""Get vocab data three times."""
for _ in range(0, 3):
get_vocabs(
self.course.id, self.resource.id, force_update=force_update
)
get_vocabs(self.course.id, self.resource.id)

set_cache_timeout(0)
with self.assertNumQueries(6):
three_times(force_update=True)
three_times()

set_cache_timeout(60)
with self.assertNumQueries(2):
three_times(force_update=False)
three_times()

def test_term_cache_with_data(self):
"""
Expand Down

0 comments on commit e5e1c96

Please sign in to comment.