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.
This commit is contained in:
Alex Vandiver
2022-10-12 11:40:24 -04:00
committed by Tim Abbott
parent 55c0e670d9
commit c328de3372

View File

@@ -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)