Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedisCache now supports function as a key_prefix #109

Merged
merged 1 commit into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 17 additions & 14 deletions flask_caching/backends/rediscache.py
Expand Up @@ -60,6 +60,9 @@ def __init__(
self._write_client = self._read_clients = client
self.key_prefix = key_prefix or ""

def _get_prefix(self):
return self.key_prefix if isinstance(self.key_prefix, str) else self.key_prefix()

def _normalize_timeout(self, timeout):
timeout = BaseCache._normalize_timeout(self, timeout)
if timeout == 0:
Expand Down Expand Up @@ -93,33 +96,33 @@ def load_object(self, value):
return value

def get(self, key):
return self.load_object(self._read_clients.get(self.key_prefix + key))
return self.load_object(self._read_clients.get(self._get_prefix() + key))

def get_many(self, *keys):
if self.key_prefix:
keys = [self.key_prefix + key for key in keys]
keys = [self._get_prefix() + key for key in keys]
return [self.load_object(x) for x in self._read_clients.mget(keys)]

def set(self, key, value, timeout=None):
timeout = self._normalize_timeout(timeout)
dump = self.dump_object(value)
if timeout == -1:
result = self._write_client.set(
name=self.key_prefix + key, value=dump
name=self._get_prefix() + key, value=dump
)
else:
result = self._write_client.setex(
name=self.key_prefix + key, value=dump, time=timeout
name=self._get_prefix() + key, value=dump, time=timeout
)
return result

def add(self, key, value, timeout=None):
timeout = self._normalize_timeout(timeout)
dump = self.dump_object(value)
return self._write_client.setnx(
name=self.key_prefix + key, value=dump
name=self._get_prefix() + key, value=dump
) and self._write_client.expire(
name=self.key_prefix + key, time=timeout
name=self._get_prefix() + key, time=timeout
)

def set_many(self, mapping, timeout=None):
Expand All @@ -131,39 +134,39 @@ def set_many(self, mapping, timeout=None):
for key, value in iteritems_wrapper(mapping):
dump = self.dump_object(value)
if timeout == -1:
pipe.set(name=self.key_prefix + key, value=dump)
pipe.set(name=self._get_prefix() + key, value=dump)
else:
pipe.setex(name=self.key_prefix + key, value=dump, time=timeout)
pipe.setex(name=self._get_prefix() + key, value=dump, time=timeout)
return pipe.execute()

def delete(self, key):
return self._write_client.delete(self.key_prefix + key)
return self._write_client.delete(self._get_prefix() + key)

def delete_many(self, *keys):
if not keys:
return
if self.key_prefix:
keys = [self.key_prefix + key for key in keys]
keys = [self._get_prefix() + key for key in keys]
return self._write_client.delete(*keys)

def has(self, key):
return self._read_clients.exists(self.key_prefix + key)
return self._read_clients.exists(self._get_prefix() + key)

def clear(self):
status = False
if self.key_prefix:
keys = self._read_clients.keys(self.key_prefix + "*")
keys = self._read_clients.keys(self._get_prefix() + "*")
if keys:
status = self._write_client.delete(*keys)
else:
status = self._write_client.flushdb()
return status

def inc(self, key, delta=1):
return self._write_client.incr(name=self.key_prefix + key, amount=delta)
return self._write_client.incr(name=self._get_prefix() + key, amount=delta)

def dec(self, key, delta=1):
return self._write_client.decr(name=self.key_prefix + key, amount=delta)
return self._write_client.decr(name=self._get_prefix() + key, amount=delta)


class RedisSentinelCache(RedisCache):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_backend_cache.py
Expand Up @@ -226,14 +226,14 @@ def make_cache(self, request):
else:
host = redis.Redis()

c = backends.RedisCache(host=host, key_prefix="werkzeug-test-case:")
c = backends.RedisCache(host=host, key_prefix=lambda: "werkzeug-test-case:")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add another test case which tests the key_prefix as a function instead of changing this test case.

yield lambda: c
c.clear()

def test_compat(self, c):
assert c._write_client.set(c.key_prefix + "foo", "Awesome")
assert c._write_client.set(c.key_prefix() + "foo", "Awesome")
assert c.get("foo") == b"Awesome"
assert c._write_client.set(c.key_prefix + "foo", "42")
assert c._write_client.set(c.key_prefix() + "foo", "42")
assert c.get("foo") == 42

def test_empty_host(self):
Expand Down