aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--bot/utils/redis_cache.py28
-rw-r--r--tests/bot/utils/test_redis_cache.py4
2 files changed, 23 insertions, 9 deletions
diff --git a/bot/utils/redis_cache.py b/bot/utils/redis_cache.py
index 558ab33a7..fb9a534bd 100644
--- a/bot/utils/redis_cache.py
+++ b/bot/utils/redis_cache.py
@@ -1,6 +1,6 @@
from __future__ import annotations
-from typing import Any, AsyncIterator, Dict, Optional, Union
+from typing import Any, Dict, ItemsView, Optional, Union
from bot.bot import Bot
@@ -237,12 +237,26 @@ class RedisCache:
key = self._to_typestring(key)
return await self._redis.hexists(self._namespace, key)
- async def items(self) -> AsyncIterator:
- """Iterate all the items in the Redis cache."""
+ async def items(self) -> ItemsView:
+ """
+ Fetch all the key/value pairs in the cache.
+
+ Returns a normal ItemsView, like you would get from dict.items().
+
+ Keep in mind that these items are just a _copy_ of the data in the
+ RedisCache - any changes you make to them will not be reflected
+ into the RedisCache itself. If you want to change these, you need
+ to make a .set call.
+
+ Example:
+ items = await my_cache.items()
+ for key, value in items:
+ # Iterate like a normal dictionary
+ """
await self._validate_cache()
- data = await self._redis.hgetall(self._namespace) # Get all the keys
- for key, value in self._dict_from_typestring(data).items():
- yield key, value
+ return self._dict_from_typestring(
+ await self._redis.hgetall(self._namespace)
+ ).items()
async def length(self) -> int:
"""Return the number of items in the Redis cache."""
@@ -251,7 +265,7 @@ class RedisCache:
async def to_dict(self) -> Dict:
"""Convert to dict and return."""
- return {key: value async for key, value in self.items()}
+ return {key: value for key, value in await self.items()}
async def clear(self) -> None:
"""Deletes the entire hash from the Redis cache."""
diff --git a/tests/bot/utils/test_redis_cache.py b/tests/bot/utils/test_redis_cache.py
index 150195726..6e12002ed 100644
--- a/tests/bot/utils/test_redis_cache.py
+++ b/tests/bot/utils/test_redis_cache.py
@@ -88,7 +88,7 @@ class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
await self.redis.set(key, value)
# Consume the AsyncIterator into a regular list, easier to compare that way.
- redis_items = [item async for item in self.redis.items()]
+ redis_items = [item for item in await self.redis.items()]
# These sequences are probably in the same order now, but probably
# isn't good enough for tests. Let's not rely on .hgetall always
@@ -113,7 +113,7 @@ class RedisCacheTests(unittest.IsolatedAsyncioTestCase):
async def test_to_dict(self):
"""Test that the .to_dict method returns a workable dictionary copy."""
copy = await self.redis.to_dict()
- local_copy = {key: value async for key, value in self.redis.items()}
+ local_copy = {key: value for key, value in await self.redis.items()}
self.assertIs(type(copy), dict)
self.assertDictEqual(copy, local_copy)