From 292cbc8bdc858b1c7cb97f85787f5cfacbf778a8 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Mon, 6 Oct 2014 03:01:46 +0200 Subject: [PATCH] Hashes cache keys for memcached support. --- cachalot/monkey_patch.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index e4c5e1d..c5542da 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from collections import defaultdict, Iterable from functools import wraps +from hashlib import md5 import re from django.conf import settings @@ -30,6 +31,10 @@ PATCHED = False MISS_VALUE = '[[Missing cache key]]' +def _get_query_cache_key(compiler): + return md5('%s:%s' % compiler.as_sql()).hexdigest() + + def _get_tables(query): """ Returns a ``set`` of all database table names used by ``query``. @@ -175,7 +180,7 @@ def _patch_orm_read(): return original(compiler, *args, **kwargs) try: - cache_key = compiler.as_sql() + cache_key = _get_query_cache_key(compiler) except EmptyResultSet: return original(compiler, *args, **kwargs)