Skip to content

Cache panel incorrectly reports hits and misses for cache.get() when a default value is supplied or None is cached #2417

Description

@Harsh3006

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions