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..7abd6a29 100644 --- a/src/django_mysql/cache.py +++ b/src/django_mysql/cache.py @@ -442,16 +442,17 @@ 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) 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 affected_rows > 0 _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):