Skip to content

Fix cache panel miss counting #1629

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,12 @@ def _store_call_info(
else:
self.hits += 1
elif name == "get_many":
for key, value in return_value.items():
if value is None:
self.misses += 1
else:
self.hits += 1
if "keys" in kwargs:
keys = kwargs["keys"]
else:
keys = args[0]
self.hits += len(return_value)
self.misses += len(keys) - len(return_value)
time_taken *= 1000

self.total_time += time_taken
Expand Down
2 changes: 2 additions & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Change log
instances isn't a valid use of Django but, again, django-debug-toolbar
shouldn't crash.
* Added pyflame (for flame graphs) to the list of third-party panels.
* Fixed the cache panel to correctly count cache misses from the get_many()
cache method.

3.4.0 (2022-05-03)
------------------
Expand Down
17 changes: 17 additions & 0 deletions tests/panels/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ def test_recording_caches(self):
second_cache.get("foo")
self.assertEqual(len(self.panel.calls), 2)

def test_hits_and_misses(self):
cache.cache.clear()
cache.cache.get("foo")
self.assertEqual(self.panel.hits, 0)
self.assertEqual(self.panel.misses, 1)
cache.cache.set("foo", 1)
cache.cache.get("foo")
self.assertEqual(self.panel.hits, 1)
self.assertEqual(self.panel.misses, 1)
cache.cache.get_many(["foo", "bar"])
self.assertEqual(self.panel.hits, 2)
self.assertEqual(self.panel.misses, 2)
cache.cache.set("bar", 2)
cache.cache.get_many(keys=["foo", "bar"])
self.assertEqual(self.panel.hits, 4)
self.assertEqual(self.panel.misses, 2)

def test_get_or_set_value(self):
cache.cache.get_or_set("baz", "val")
self.assertEqual(cache.cache.get("baz"), "val")
Expand Down