diff options
author | Jordan Cook <jordan.cook@pioneer.com> | 2021-12-01 12:44:48 -0600 |
---|---|---|
committer | Jordan Cook <jordan.cook@pioneer.com> | 2021-12-01 12:45:43 -0600 |
commit | 8e4e1682808b550278192a3598dc573caa9aa308 (patch) | |
tree | 63debd5da89292e00a5565d9e0b1ec65d710f7d9 | |
parent | 7e0690254ed45fa66f8b38571a9efd8fddda73cf (diff) | |
download | requests-cache-8e4e1682808b550278192a3598dc573caa9aa308.tar.gz |
Drop idea of storing each response in a separate hash, and go back to serialized blobs
Can't store nested dicts in a Redis hash
-rw-r--r-- | requests_cache/backends/redis.py | 28 | ||||
-rw-r--r-- | tests/integration/base_storage_test.py | 2 | ||||
-rw-r--r-- | tests/integration/test_redis.py | 4 |
3 files changed, 12 insertions, 22 deletions
diff --git a/requests_cache/backends/redis.py b/requests_cache/backends/redis.py index f40a735..bc8bc15 100644 --- a/requests_cache/backends/redis.py +++ b/requests_cache/backends/redis.py @@ -38,7 +38,6 @@ from redis import Redis, StrictRedis from .._utils import get_valid_kwargs from ..cache_keys import decode, encode -from ..serializers import dict_serializer from . import BaseCache, BaseStorage logger = getLogger(__name__) @@ -65,17 +64,12 @@ class RedisDict(BaseStorage): """A dictionary-like interface for Redis operations. **Notes:** - * Each item is stored as a separate hash - * All keys will be encoded as bytes - * Does not support custom serializers + * All keys will be encoded as bytes, and all values will be serialized * Supports TTL """ - def __init__(self, namespace: str, connection=None, serializer=None, **kwargs): - if serializer: - logger.warning('RedisDict does not support custom serializers') - super().__init__(serializer=dict_serializer, **kwargs) - + def __init__(self, namespace: str, collection_name: str = None, connection=None, **kwargs): + super().__init__(**kwargs) connection_kwargs = get_valid_kwargs(Redis, kwargs) self.connection = connection or StrictRedis(**connection_kwargs) self.namespace = namespace @@ -91,21 +85,17 @@ class RedisDict(BaseStorage): return bool(self.connection.exists(self._bkey(key))) def __getitem__(self, key): - # result = self.connection.get(self._bkey(key)) - result = self.connection.hgetall(self._bkey(key)) - if not result: + result = self.connection.get(self._bkey(key)) + if result is None: raise KeyError return self.serializer.loads(result) def __setitem__(self, key, item): """Save an item to the cache, optionally with TTL""" - # if getattr(item, 'ttl', None): - # self.connection.setex(self._bkey(key), item.ttl, self.serializer.dumps(item)) - # else: - # self.connection.set(self._bkey(key), self.serializer.dumps(item)) - self.connection.hmset(self._bkey(key), self.serializer.dumps(item)) if getattr(item, 'ttl', None): - self.connection.expire(self._bkey(key), item.ttl) + self.connection.setex(self._bkey(key), item.ttl, self.serializer.dumps(item)) + else: + self.connection.set(self._bkey(key), self.serializer.dumps(item)) def __delitem__(self, key): if not self.connection.delete(self._bkey(key)): @@ -152,7 +142,7 @@ class RedisHashDict(BaseStorage): super().__init__(**kwargs) connection_kwargs = get_valid_kwargs(Redis, kwargs) self.connection = connection or StrictRedis(**connection_kwargs) - self._hash_key = f'{namespace}:{collection_name}' + self._hash_key = f'{namespace}-{collection_name}' def __contains__(self, key): return self.connection.hexists(self._hash_key, encode(key)) diff --git a/tests/integration/base_storage_test.py b/tests/integration/base_storage_test.py index 893afdb..e89931c 100644 --- a/tests/integration/base_storage_test.py +++ b/tests/integration/base_storage_test.py @@ -25,7 +25,7 @@ class BaseStorageTest: def teardown_class(cls): for i in range(cls.num_instances): - cls().init_cache(i, clear=True) + cls().init_cache(index=i, clear=True) def test_basic_methods(self): """Test basic dict methods with multiple cache instances: diff --git a/tests/integration/test_redis.py b/tests/integration/test_redis.py index 17efccf..08f1ee0 100644 --- a/tests/integration/test_redis.py +++ b/tests/integration/test_redis.py @@ -19,7 +19,7 @@ def ensure_connection(): class TestRedisDict(BaseStorageTest): storage_class = RedisDict - num_instances = 1 # Only test a single collecton + num_instances = 1 # Only supports a single instance, since it stores items under top-level keys @patch('requests_cache.backends.redis.StrictRedis') def test_connection_kwargs(self, mock_redis): @@ -30,7 +30,7 @@ class TestRedisDict(BaseStorageTest): class TestRedisHashDict(TestRedisDict): storage_class = RedisHashDict - num_instances: int = 10 + num_instances: int = 10 # Supports multiple instances, since this stores items under hash keys picklable = True |