The Cache panel currently determines whether a cache.get() operation is a hit or a miss solely by checking whether the returned value is None.
This leads to incorrect hit/miss reporting in the following cases:
1. Missing key with a default value
cache.get("missing", "default")
The cache backend does not contain the key, so this is a cache miss. However, the Cache panel reports it as a cache hit because the returned value is the supplied default ("default"), which is not None.
2. Cached value is None
cache.set("key", None)
cache.get("key")
The key exists in the cache, so this is a cache hit. However, the Cache panel reports it as a cache miss because the cached value is None.
Expected behavior
The Cache panel should distinguish between:
- a missing cache key, and
- a cache key whose stored value is
None (or any value equal to the caller's supplied default).
Background
Django's cache framework recommends using a unique sentinel object when calling cache.get() to distinguish between a missing key and a stored value of None, see the highlighted guidance here. The current implementation does not account for this behavior, which results in incorrect cache hit/miss statistics for the scenarios described above.
The Cache panel currently determines whether a
cache.get()operation is a hit or a miss solely by checking whether the returned value isNone.This leads to incorrect hit/miss reporting in the following cases:
1. Missing key with a default value
The cache backend does not contain the key, so this is a cache miss. However, the Cache panel reports it as a cache hit because the returned value is the supplied default (
"default"), which is notNone.2. Cached value is
NoneThe key exists in the cache, so this is a cache hit. However, the Cache panel reports it as a cache miss because the cached value is
None.Expected behavior
The Cache panel should distinguish between:
None(or any value equal to the caller's supplied default).Background
Django's cache framework recommends using a unique sentinel object when calling
cache.get()to distinguish between a missing key and a stored value ofNone, see the highlighted guidance here. The current implementation does not account for this behavior, which results in incorrect cache hit/miss statistics for the scenarios described above.