From: jordan Date: Thu, 10 Nov 2011 14:46:37 +0000 (+0000) Subject: OSQA-776, limit the cache key length in the infer_cache_key method, adding a setting... X-Git-Tag: live~117 X-Git-Url: https://git.openstreetmap.org./osqa.git/commitdiff_plain/01e93f28b2829fd3924a3b6021a201c1b6ed3d72 OSQA-776, limit the cache key length in the infer_cache_key method, adding a setting that defaults to 235 symbols git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@1200 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- diff --git a/forum/models/base.py b/forum/models/base.py index 2cfe7db..33bf0c8 100644 --- a/forum/models/base.py +++ b/forum/models/base.py @@ -8,6 +8,7 @@ from urllib import quote_plus, urlencode from django.db import models, IntegrityError, connection, transaction from django.utils.http import urlquote as django_urlquote from django.utils.html import strip_tags +from django.conf import settings as django_settings from django.core.urlresolvers import reverse from django.contrib.contenttypes import generic from django.contrib.contenttypes.models import ContentType @@ -323,7 +324,12 @@ class BaseModel(models.Model): pk = [v for (k,v) in querydict.items() if k in ('pk', 'pk__exact', 'id', 'id__exact' ) or k.endswith('_ptr__pk') or k.endswith('_ptr__id')][0] - return cls._generate_cache_key(pk) + cache_key = cls._generate_cache_key(pk) + + if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH: + cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH] + + return cache_key except: return None diff --git a/forum/models/tag.py b/forum/models/tag.py index 067488b..73b1e30 100644 --- a/forum/models/tag.py +++ b/forum/models/tag.py @@ -1,6 +1,8 @@ import datetime from base import * +from django.conf import settings as django_settings +from django.core.cache.backends.base import BaseCache from django.utils.translation import ugettext as _ from django.utils.encoding import smart_unicode, force_unicode @@ -45,7 +47,12 @@ class Tag(BaseModel): @classmethod def infer_cache_key(cls, querydict): if 'name' in querydict: - return cls._generate_cache_key(cls.safe_cache_name(querydict['name'])) + cache_key = cls._generate_cache_key(cls.safe_cache_name(querydict['name'])) + + if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH: + cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH] + + return cache_key return None diff --git a/forum/models/user.py b/forum/models/user.py index b852813..e8871f4 100644 --- a/forum/models/user.py +++ b/forum/models/user.py @@ -1,5 +1,6 @@ from base import * from utils import PickledObjectField +from django.conf import settings as django_settings from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned from django.contrib.contenttypes.models import ContentType from django.contrib.auth.models import User as DjangoUser, AnonymousUser as DjangoAnonymousUser @@ -462,7 +463,10 @@ class UserProperty(BaseModel): @classmethod def infer_cache_key(cls, querydict): if 'user' in querydict and 'key' in querydict: - return cls._generate_cache_key("%s:%s" % (querydict['user'].id, querydict['key'])) + cache_key = cls._generate_cache_key("%s:%s" % (querydict['user'].id, querydict['key'])) + if len(cache_key) > django_settings.CACHE_MAX_KEY_LENGTH: + cache_key = cache_key[:django_settings.CACHE_MAX_KEY_LENGTH] + return cache_key return None diff --git a/settings.py b/settings.py index a1044e5..c895381 100644 --- a/settings.py +++ b/settings.py @@ -7,6 +7,8 @@ SITE_ID = 1 ADMIN_MEDIA_PREFIX = '/admin_media/' SECRET_KEY = '$oo^&_m&qwbib=(_4m_n*zn-d=g#s0he5fx9xonnym#8p6yigm' +CACHE_MAX_KEY_LENGTH = 235 + TEMPLATE_LOADERS = [ 'django.template.loaders.filesystem.load_template_source', 'django.template.loaders.app_directories.load_template_source',