From e37eb49065abfda70abdfcf5f70ddd748f49aa74 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 15 Mar 2012 14:45:27 -0400 Subject: [PATCH 1/3] Shift wiki and course.xml caching to use newly defined 'general' cache, separate from sessions left in 'default' --HG-- branch : cache_separation --- courseware/content_parser.py | 3 ++- simplewiki/models.py | 3 ++- util/cache.py | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 util/cache.py diff --git a/courseware/content_parser.py b/courseware/content_parser.py index cc02597cb3..2c6a515c63 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -11,10 +11,11 @@ from lxml import etree try: # This lets us do __name__ == ='__main__' from django.conf import settings - from django.core.cache import cache + from student.models import UserProfile from student.models import UserTestGroup from mitxmako.shortcuts import render_to_response, render_to_string + from util.cache import cache except: settings = None diff --git a/simplewiki/models.py b/simplewiki/models.py index 58173e0ca0..33c9b0403e 100644 --- a/simplewiki/models.py +++ b/simplewiki/models.py @@ -3,7 +3,6 @@ import os from django import forms from django.contrib.auth.models import User -from django.core.cache import cache from django.core.urlresolvers import reverse from django.db import models from django.db.models import signals @@ -11,6 +10,8 @@ from django.utils.translation import ugettext_lazy as _ from markdown import markdown from settings import * +from util.cache import cache + class ShouldHaveExactlyOneRootSlug(Exception): pass diff --git a/util/cache.py b/util/cache.py new file mode 100644 index 0000000000..b0c370a035 --- /dev/null +++ b/util/cache.py @@ -0,0 +1,16 @@ +""" +This module aims to give a little more fine-tuned control of caching and cache +invalidation. Import these instead of django.core.cache. + +Note that 'default' is being preserved for user session caching, which we're +not migrating so as not to inconvenience users by logging them all out. +""" +from django.core import cache + +# If we can't find a 'general' CACHE defined in settings.py, we simply fall back +# to returning the default cache. This will happen with dev machines. +try: + cache = cache.get_cache('general') +except ValueError: + cache = cache.cache + From f4625003a79a2a2d47c521d0eb3d45dca0c92725 Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 15 Mar 2012 15:21:39 -0400 Subject: [PATCH 2/3] change config to not cache by default, unless overridden by server-specific settings.py --HG-- branch : cache_separation --- courseware/content_parser.py | 5 +---- settings.py | 7 +++++++ util/cache.py | 1 + 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/courseware/content_parser.py b/courseware/content_parser.py index 2c6a515c63..eb1678536d 100644 --- a/courseware/content_parser.py +++ b/courseware/content_parser.py @@ -158,10 +158,7 @@ def user_groups(user): cache_expiration = 60 * 60 # one hour # Kill caching on dev machines -- we switch groups a lot - if "dev" not in settings.DEFAULT_GROUPS: - group_names = cache.get(fasthash(key)) - else: - group_names = None + group_names = cache.get(fasthash(key)) if group_names is None: group_names = [u.name for u in UserTestGroup.objects.filter(users=user)] diff --git a/settings.py b/settings.py index cce3dfad4e..561fc550af 100644 --- a/settings.py +++ b/settings.py @@ -163,6 +163,13 @@ MAKO_TEMPLATES = {} LOGGING_ENV = "dev" # override this in different environments +# Default dev cache (i.e. no caching) +CACHES = { + 'default': { + 'BACKEND': 'django.core.cache.backends.dummy.DummyCache', + } +} + # Make sure we execute correctly regardless of where we're called from execfile(os.path.join(BASE_DIR, "settings.py")) diff --git a/util/cache.py b/util/cache.py index b0c370a035..1a01b3d7fd 100644 --- a/util/cache.py +++ b/util/cache.py @@ -6,6 +6,7 @@ Note that 'default' is being preserved for user session caching, which we're not migrating so as not to inconvenience users by logging them all out. """ from django.core import cache +import settings # If we can't find a 'general' CACHE defined in settings.py, we simply fall back # to returning the default cache. This will happen with dev machines. From d9253a0c0fcc11088c0c0208721fb0f7cbef59bf Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Thu, 15 Mar 2012 15:25:39 -0400 Subject: [PATCH 3/3] remove unnecessary settings import --HG-- branch : cache_separation --- util/cache.py | 1 - 1 file changed, 1 deletion(-) diff --git a/util/cache.py b/util/cache.py index 1a01b3d7fd..b0c370a035 100644 --- a/util/cache.py +++ b/util/cache.py @@ -6,7 +6,6 @@ Note that 'default' is being preserved for user session caching, which we're not migrating so as not to inconvenience users by logging them all out. """ from django.core import cache -import settings # If we can't find a 'general' CACHE defined in settings.py, we simply fall back # to returning the default cache. This will happen with dev machines.