Skip to content

Implement CacheStatTracker.get_or_set #1570

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
Jan 11, 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
7 changes: 6 additions & 1 deletion debug_toolbar/panels/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def get(self, *args, **kwargs):
def set(self, *args, **kwargs):
return self.cache.set(*args, **kwargs)

@send_signal
def get_or_set(self, *args, **kwargs):
return self.cache.get_or_set(*args, **kwargs)

@send_signal
def touch(self, *args, **kwargs):
return self.cache.touch(*args, **kwargs)
Expand Down Expand Up @@ -169,6 +173,7 @@ def __init__(self, *args, **kwargs):
("add", 0),
("get", 0),
("set", 0),
("get_or_set", 0),
("touch", 0),
("delete", 0),
("clear", 0),
Expand Down Expand Up @@ -197,7 +202,7 @@ def _store_call_info(
backend=None,
**kw,
):
if name == "get":
if name == "get" or name == "get_or_set":
if return_value is None:
self.misses += 1
else:
Expand Down
1 change: 1 addition & 0 deletions docs/changes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Change log
Next version
------------

* Track calls to :py:meth:`django.core.caches.cache.get_or_set`.
* Removed support for Django < 3.2.
* Updated check ``W006`` to look for
``django.template.loaders.app_directories.Loader``.
Expand Down
69 changes: 69 additions & 0 deletions tests/panels/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,75 @@ def test_recording_caches(self):
second_cache.get("foo")
self.assertEqual(len(self.panel.calls), 2)

def test_get_or_set_value(self):
cache.cache.get_or_set("baz", "val")
self.assertEqual(cache.cache.get("baz"), "val")
calls = [
(call["name"], call["args"], call["kwargs"]) for call in self.panel.calls
]
self.assertEqual(
calls,
[
("get_or_set", ("baz", "val"), {}),
("get", ("baz",), {}),
],
)
self.assertEqual(
self.panel.counts,
{
"add": 0,
"get": 1,
"set": 0,
"get_or_set": 1,
"touch": 0,
"delete": 0,
"clear": 0,
"get_many": 0,
"set_many": 0,
"delete_many": 0,
"has_key": 0,
"incr": 0,
"decr": 0,
"incr_version": 0,
"decr_version": 0,
},
)

def test_get_or_set_does_not_override_existing_value(self):
cache.cache.set("foo", "bar")
cached_value = cache.cache.get_or_set("foo", "other")
self.assertEqual(cached_value, "bar")
calls = [
(call["name"], call["args"], call["kwargs"]) for call in self.panel.calls
]
self.assertEqual(
calls,
[
("set", ("foo", "bar"), {}),
("get_or_set", ("foo", "other"), {}),
],
)
self.assertEqual(
self.panel.counts,
{
"add": 0,
"get": 0,
"set": 1,
"get_or_set": 1,
"touch": 0,
"delete": 0,
"clear": 0,
"get_many": 0,
"set_many": 0,
"delete_many": 0,
"has_key": 0,
"incr": 0,
"decr": 0,
"incr_version": 0,
"decr_version": 0,
},
)

def test_insert_content(self):
"""
Test that the panel only inserts content after generate_stats and
Expand Down