Created
December 23, 2011 20:10
-
-
Save mtigas/1515240 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.utils.encoding import smart_str | |
from django.utils.hashcompat import md5_constructor | |
def short_key(key, key_prefix, version): | |
""" | |
Like the default `KEY_FUNCTION`, but hashes strings longer | |
than 250 characters for safety with memcached. Uses python hash() | |
only when key is too long for memcached. Does not do anything | |
about memcached's restricted character set, only the key | |
length restriction. Due to keeping as much of the original | |
key as possible, keys are somewhat introspectable. | |
""" | |
k = ":".join([key_prefix, str(version), smart_str(key)]) | |
if len(k) <= 250: | |
return k | |
else: | |
# hash() isn't necessarily cryptographically secure, but | |
# the values are unique enough for this use. | |
# see http://effbot.org/zone/python-hash.htm for algorithm | |
return k[:220] + ".trunc." + str(hash(k[220:])) | |
def md5_key(key, key_prefix, version): | |
""" | |
Like the default `KEY_FUNCTION`, but md5 to hash the input key. | |
Causes output key to be within memcached's key length restriction | |
and restricted ASCII character set (provided the `key_prefix` and | |
`version` are). Keys are no longer introspectable. | |
""" | |
return ":".join([ | |
key_prefix, | |
str(version), | |
md5_constructor(smart_str(key)).hexdigest() | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment