From aaa383b8ca714b0f84db959339fe9a1496e76d78 Mon Sep 17 00:00:00 2001 From: Will Daly Date: Tue, 7 May 2013 15:48:51 -0400 Subject: [PATCH] safe_key() now hashes the prefix/version as well, just in case these are configured to be too long in the settings. --- common/djangoapps/util/memcache.py | 6 ++---- common/djangoapps/util/tests/test_memcache.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/common/djangoapps/util/memcache.py b/common/djangoapps/util/memcache.py index db921d9845..ee450e68cb 100644 --- a/common/djangoapps/util/memcache.py +++ b/common/djangoapps/util/memcache.py @@ -41,11 +41,9 @@ def safe_key(key, key_prefix, version): # Attempt to combine the prefix, version, and key combined = ":".join([key_prefix, version, key]) - # If the total length is too long for memcache, hash the key - # and combine the parts again + # If the total length is too long for memcache, hash it if len(combined) > 250: - key = fasthash(key) - combined = ":".join([key_prefix, version, key]) + combined = fasthash(combined) # Return the result return combined diff --git a/common/djangoapps/util/tests/test_memcache.py b/common/djangoapps/util/tests/test_memcache.py index 85b60c75f1..de8d352c38 100644 --- a/common/djangoapps/util/tests/test_memcache.py +++ b/common/djangoapps/util/tests/test_memcache.py @@ -4,6 +4,7 @@ Tests for memcache in util app from django.test import TestCase from django.core.cache import get_cache +from django.conf import settings from util.memcache import safe_key @@ -51,8 +52,17 @@ class MemcacheTest(TestCase): def test_long_key_prefix_version(self): + # Long key key = safe_key('a' * 300, 'prefix', 'version') - self.assertEqual(key[0:15], 'prefix:version:') + self.assertTrue(self._is_valid_key(key)) + + # Long prefix + key = safe_key('key', 'a' * 300, 'version') + self.assertTrue(self._is_valid_key(key)) + + # Long version + key = safe_key('key', 'prefix', 'a' * 300) + self.assertTrue(self._is_valid_key(key)) def test_safe_key_unicode(self):