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): ...@@ -41,11 +41,9 @@ def safe_key(key, key_prefix, version):
# Attempt to combine the prefix, version, and key # Attempt to combine the prefix, version, and key
combined = ":".join([key_prefix, version, key]) combined = ":".join([key_prefix, version, key])
# If the total length is too long for memcache, hash the key # If the total length is too long for memcache, hash it
# and combine the parts again
if len(combined) > 250: if len(combined) > 250:
key = fasthash(key) combined = fasthash(combined)
combined = ":".join([key_prefix, version, key])
# Return the result # Return the result
return combined return combined
...@@ -4,6 +4,7 @@ Tests for memcache in util app ...@@ -4,6 +4,7 @@ Tests for memcache in util app
from django.test import TestCase from django.test import TestCase
from django.core.cache import get_cache from django.core.cache import get_cache
from django.conf import settings
from util.memcache import safe_key from util.memcache import safe_key
...@@ -51,8 +52,17 @@ class MemcacheTest(TestCase): ...@@ -51,8 +52,17 @@ class MemcacheTest(TestCase):
def test_long_key_prefix_version(self): def test_long_key_prefix_version(self):
# Long key
key = safe_key('a' * 300, 'prefix', 'version') 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): 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