Skip to content

Commit

Permalink
Add return value for MySQLCache.touch() (#938)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored Aug 22, 2022
1 parent 8da03cd commit 2499617
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------

Expand Down
5 changes: 3 additions & 2 deletions src/django_mysql/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down
9 changes: 6 additions & 3 deletions tests/testapp/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 2499617

Please sign in to comment.