From 7aa9b4906412adfcd7b67f13852d4277a03002a1 Mon Sep 17 00:00:00 2001 From: Jannis Leidel Date: Fri, 8 Mar 2013 12:05:03 +0100 Subject: [PATCH] Use slugify on the cache key and append it with a hash of the full key to prevent too long keys (looking at you, Memcache). --- avatar/util.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/avatar/util.py b/avatar/util.py index ddce817..0f47df7 100644 --- a/avatar/util.py +++ b/avatar/util.py @@ -1,5 +1,7 @@ from django.conf import settings from django.core.cache import cache +from django.utils.hashcompat import md5_constructor +from django.templates.defaultfilters import slugify from django.contrib.auth.models import User @@ -14,7 +16,9 @@ def get_cache_key(user_or_username, size, prefix): """ if isinstance(user_or_username, User): user_or_username = user_or_username.username - return '%s_%s_%s' % (prefix, user_or_username, size) + key = '%s_%s_%s' % (prefix, user_or_username, size) + return '%s_%s' % (slugify(key)[:100], md5_constructor(key).hexdigest()) + def cache_result(func): """