From 709513db65f0d4294be99dbbcfae859c7754e820 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Fri, 19 Jun 2015 19:32:46 +0200 Subject: [PATCH] Avoids caching queries with an unknown parameter type. This fixes #21. --- cachalot/utils.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cachalot/utils.py b/cachalot/utils.py index 88c6e96..0a8129b 100644 --- a/cachalot/utils.py +++ b/cachalot/utils.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals from hashlib import sha1 from time import time +from types import NoneType import django from django.db import connections @@ -13,6 +14,7 @@ if DJANGO_GTE_1_7: from django.utils.module_loading import import_string else: from django.utils.module_loading import import_by_path as import_string +from django.utils.six import text_type, binary_type from .settings import cachalot_settings from .signals import post_invalidation @@ -22,6 +24,8 @@ class UncachableQuery(Exception): pass +CACHABLE_PARAM_TYPES = (bool, int, binary_type, text_type, NoneType) + def get_query_cache_key(compiler): """ Generates a cache key from a SQLCompiler. @@ -36,6 +40,8 @@ def get_query_cache_key(compiler): :rtype: str """ sql, params = compiler.as_sql() + if any(p.__class__ not in CACHABLE_PARAM_TYPES for p in params): + raise UncachableQuery cache_key = '%s:%s:%s' % (compiler.using, sql, params) return sha1(cache_key.encode('utf-8')).hexdigest()