Commit aaa383b8 by Will Daly

safe_key() now hashes the prefix/version as well, just in case

these are configured to be too long in the settings.
parent 1b0b365f
......@@ -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):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment