From 8560521641831036aec9a60e1955dcb6cd79780f Mon Sep 17 00:00:00 2001 From: Adrian Date: Tue, 16 Feb 2021 11:49:55 +0100 Subject: [PATCH] Fix add() in RedisCache without a timeout When there is no timeout, this resulted in the key expiring immediately after setting it, instead of not having an expiry at all. --- flask_caching/backends/rediscache.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/flask_caching/backends/rediscache.py b/flask_caching/backends/rediscache.py index d42ee62b..44b449f7 100644 --- a/flask_caching/backends/rediscache.py +++ b/flask_caching/backends/rediscache.py @@ -135,11 +135,14 @@ def set(self, key, value, timeout=None): def add(self, key, value, timeout=None): timeout = self._normalize_timeout(timeout) dump = self.dump_object(value) - return self._write_client.setnx( + created = self._write_client.setnx( name=self._get_prefix() + key, value=dump - ) and self._write_client.expire( - name=self._get_prefix() + key, time=timeout ) + if created and timeout != -1: + self._write_client.expire( + name=self._get_prefix() + key, time=timeout + ) + return created def set_many(self, mapping, timeout=None): timeout = self._normalize_timeout(timeout)