From 1a504703d6997e708c84545557fc0ccde01afefc Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 22 Aug 2022 14:01:14 +0100 Subject: [PATCH 1/2] Add return value for MySQLCache.touch() --- HISTORY.rst | 3 +++ src/django_mysql/cache.py | 3 ++- tests/testapp/test_cache.py | 9 ++++++--- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 05b1abff..121860de 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,9 @@ History ======= +* Make ``MySQLCache.touch()`` return ``True`` if the key was touched, ``False`` otherwise. + This return value was missing since the method was added for Django 2.1. + 4.7.1 (2022-08-11) ------------------ diff --git a/src/django_mysql/cache.py b/src/django_mysql/cache.py index 4f2e90d9..10c5c8bf 100644 --- a/src/django_mysql/cache.py +++ b/src/django_mysql/cache.py @@ -442,7 +442,7 @@ def clear(self) -> None: def touch( self, key: str, timeout: Any = DEFAULT_TIMEOUT, version: int | None = None - ) -> None: + ) -> bool: key = self.make_key(key, version=version) self.validate_key(key) exp = self.get_backend_timeout(timeout) @@ -452,6 +452,7 @@ def touch( cursor.execute( self._touch_query.format(table=table), [exp, key, self._now()] ) + return True _touch_query = collapse_spaces( """ diff --git a/tests/testapp/test_cache.py b/tests/testapp/test_cache.py index 99a28cba..a6072e0a 100644 --- a/tests/testapp/test_cache.py +++ b/tests/testapp/test_cache.py @@ -349,20 +349,23 @@ def test_clear(self): def test_touch_without_timeout(self): cache.set("key1", "spam", timeout=0.1) - cache.touch("key1", timeout=0.4) + result = cache.touch("key1", timeout=0.4) + assert result is True time.sleep(0.2) assert "key1" in cache def test_touch_with_timeout(self): cache.set("key1", "spam", timeout=0.1) - cache.touch("key1") + result = cache.touch("key1") + assert result is True time.sleep(0.2) assert "key1" in cache def test_touch_already_expired(self): cache.set("key1", "spam", timeout=0.1) time.sleep(0.2) - cache.touch("key1", timeout=0.4) + result = cache.touch("key1", timeout=0.4) + assert result is False assert "key1" not in cache def test_long_timeout(self): From ec3d5513152a9f6b4f0a5638ca9353f0304bc0bc Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 22 Aug 2022 14:05:42 +0100 Subject: [PATCH 2/2] commit return change --- src/django_mysql/cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/django_mysql/cache.py b/src/django_mysql/cache.py index 10c5c8bf..7abd6a29 100644 --- a/src/django_mysql/cache.py +++ b/src/django_mysql/cache.py @@ -449,10 +449,10 @@ def touch( db = router.db_for_write(self.cache_model_class) table = connections[db].ops.quote_name(self._table) with connections[db].cursor() as cursor: - cursor.execute( + affected_rows = cursor.execute( self._touch_query.format(table=table), [exp, key, self._now()] ) - return True + return affected_rows > 0 _touch_query = collapse_spaces( """