safe_key() now hashes the prefix/version as well, just in case
these are configured to be too long in the settings.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
|
||||
Reference in New Issue
Block a user