Skip to content

Commit 2499617

Browse files
authored
Add return value for MySQLCache.touch() (#938)
1 parent 8da03cd commit 2499617

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

HISTORY.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
History
33
=======
44

5+
* Make ``MySQLCache.touch()`` return ``True`` if the key was touched, ``False`` otherwise.
6+
This return value was missing since the method was added for Django 2.1.
7+
58
4.7.1 (2022-08-11)
69
------------------
710

src/django_mysql/cache.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -442,16 +442,17 @@ def clear(self) -> None:
442442

443443
def touch(
444444
self, key: str, timeout: Any = DEFAULT_TIMEOUT, version: int | None = None
445-
) -> None:
445+
) -> bool:
446446
key = self.make_key(key, version=version)
447447
self.validate_key(key)
448448
exp = self.get_backend_timeout(timeout)
449449
db = router.db_for_write(self.cache_model_class)
450450
table = connections[db].ops.quote_name(self._table)
451451
with connections[db].cursor() as cursor:
452-
cursor.execute(
452+
affected_rows = cursor.execute(
453453
self._touch_query.format(table=table), [exp, key, self._now()]
454454
)
455+
return affected_rows > 0
455456

456457
_touch_query = collapse_spaces(
457458
"""

tests/testapp/test_cache.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,20 +349,23 @@ def test_clear(self):
349349

350350
def test_touch_without_timeout(self):
351351
cache.set("key1", "spam", timeout=0.1)
352-
cache.touch("key1", timeout=0.4)
352+
result = cache.touch("key1", timeout=0.4)
353+
assert result is True
353354
time.sleep(0.2)
354355
assert "key1" in cache
355356

356357
def test_touch_with_timeout(self):
357358
cache.set("key1", "spam", timeout=0.1)
358-
cache.touch("key1")
359+
result = cache.touch("key1")
360+
assert result is True
359361
time.sleep(0.2)
360362
assert "key1" in cache
361363

362364
def test_touch_already_expired(self):
363365
cache.set("key1", "spam", timeout=0.1)
364366
time.sleep(0.2)
365-
cache.touch("key1", timeout=0.4)
367+
result = cache.touch("key1", timeout=0.4)
368+
assert result is False
366369
assert "key1" not in cache
367370

368371
def test_long_timeout(self):

0 commit comments

Comments
 (0)