--- a/src/p4l/utils.py Fri Sep 20 22:21:48 2013 +0200
+++ b/src/p4l/utils.py Sat Sep 21 23:49:04 2013 +0200
@@ -2,11 +2,13 @@
import codecs
import logging
import math
+import hashlib
import sys
import unicodedata
from django.conf import settings
from django.core.validators import URLValidator
+from django.utils.http import urlquote_plus
import requests
@@ -184,4 +186,24 @@
def strip_accents(value):
return ''.join(c for c in unicodedata.normalize('NFD', value)
if unicodedata.category(c) != 'Mn')
+
+
+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.
+ '''
+ value = urlquote_plus(value)
+ 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 hashlib.md5(value).hexdigest()
+
+
\ No newline at end of file