Login

Safer cache key generation

Author:
cmheisel
Posted:
November 23, 2008
Language:
Python
Version:
1.0
Score:
2 (after 2 ratings)

If any cache keys you generate include user (staff or public) supplied data, they may: be too long, contain invalid characters (at least by memcache's standards), or both.

This helper will sub out any invalid characters and md5 the key if it's too long.

Additionally, it'll insert the CACHE_MIDDLEWARE_KEY_PREFIX from django.conf.settings for you. In your memcache instances are used by multiple applications (Django or otherwise) this can help ensure your keys are unique to that a particular app on a particular site.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import md5
from django.conf import settings
def safe_cache_key(value):
    '''Returns an md5 hexdigest of value if len(value) > 250. Replaces invalid memcache
       control characters with an underscore. Also adds the CACHE_MIDDLEWARE_KEY_PREFIX
       to your keys automatically.
    '''
    for char in value:
      if ord(char) < 33:
          value = value.replace(char, '_')
    
    value = "%s_%s" % (settings.CACHE_MIDDLEWARE_KEY_PREFIX, value)
    
    if len(value) <= 250:
        return value
    
    return md5.new(value).hexdigest()

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

pigletto (on November 24, 2008):

I was looking for this kind of cache key generator for memcached recently and here it is :)

One thing that might be considered when using this snippet is to replace md5 by hashlib library if you tend to use newer python version (md5 is deprecated in python2.5).

#

bthomas (on December 18, 2008):

Actually, you can use django.utils.hashcompat.md5_constructor, which will use hashlib when available, and fallback to the old md5 import. The last line would be md5_constructor(value).digest()

#

Please login first before commenting.