From c328de33721fd5fa7fa3b953e2a8b466b62798ce Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Wed, 12 Oct 2022 11:40:24 -0400 Subject: [PATCH] cache: Log a warning when attempting to store a whole QuerySet. As noted in the previous commit, this causes bloat in memcached, for no purpose. Log a warning when `cache_with_key` sees a QuerySet returned from the function it is decorating. --- zerver/lib/cache.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/zerver/lib/cache.py b/zerver/lib/cache.py index 7cafb56049..063019c4c4 100644 --- a/zerver/lib/cache.py +++ b/zerver/lib/cache.py @@ -26,6 +26,7 @@ from django.conf import settings from django.core.cache import caches from django.core.cache.backends.base import BaseCache from django.db.models import Q +from django.db.models.query import QuerySet from django.http import HttpRequest from typing_extensions import ParamSpec @@ -173,6 +174,12 @@ def cache_with_key( return val[0] val = func(*args, **kwargs) + if isinstance(val, QuerySet): # type: ignore[misc] # https://github.com/typeddjango/django-stubs/issues/704 + logging.warning( + "cache_with_key attempted to store a full QuerySet object -- flattening using list()", + stack_info=True, + ) + val = list(val) cache_set(key, val, cache_name=cache_name, timeout=timeout)