Skip to content

Instantly share code, notes, and snippets.

@mtigas mtigas/
Created Dec 23, 2011

What would you like to do?
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
# hash() isn't necessarily cryptographically secure, but
# the values are unique enough for this use.
# see 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([
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.