memcache.py 553 Bytes
Newer Older
1 2 3 4 5
"""
This module provides a KEY_FUNCTION suitable for use with a memcache backend
so that we can cache any keys, not just ones that memcache would ordinarily accept
"""
from django.utils.encoding import smart_str
6 7 8 9 10 11
import hashlib
import urllib

def fasthash(string):
    m = hashlib.new("md4")
    m.update(string)
12
    return m.hexdigest()
13 14

def safe_key(key, key_prefix, version):
15
    safe_key = urllib.quote_plus(smart_str(key))
16 17

    if len(safe_key) > 250:
18
        safe_key = fasthash(safe_key)
19 20

    return ":".join([key_prefix, str(version), safe_key])