Skip to content

Commit

Permalink
remove aioredis
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcarbs committed Sep 16, 2024
1 parent 4b37a4e commit d72f5f5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
2 changes: 1 addition & 1 deletion contrib-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ mypy-zope==1.0.5
black==19.10b0
isort==4.3.21
emcache==1.3.0
pymemcache==4.0.0
pymemcache==4.0.0
45 changes: 26 additions & 19 deletions guillotina/contrib/redis/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@
class watch(metrics.watch):
def __init__(self, operation: str):
super().__init__(
counter=REDIS_OPS,
histogram=REDIS_OPS_PROCESSING_TIME,
labels={"type": operation},
counter=REDIS_OPS, histogram=REDIS_OPS_PROCESSING_TIME, labels={"type": operation}
)


except ImportError:
watch = metrics.watch # type: ignore

Expand Down Expand Up @@ -88,6 +87,9 @@ def pool(self):
return self._pool

async def info(self):
if self._pool is None:
raise NoRedisConfigured()

return await self._pool.execute_command(b"COMMAND", b"INFO", "get")

# VALUE API
Expand Down Expand Up @@ -117,32 +119,37 @@ async def delete(self, key: str):
with watch("delete"):
await self._pool.delete(key)

async def expire(self, key: str, expire: int):
async def delete_all(self, keys: List[str]):
if self._pool is None:
raise NoRedisConfigured()
await self._pool.expire(key, expire)

async def keys_startswith(self, key: str):
with watch("delete_many"):
for key in keys:
try:
with watch("delete"):
await self._pool.execute_command(b"DEL", key)
logger.debug("Deleted cache keys {}".format(keys))
except Exception:
logger.warning("Error deleting cache keys {}".format(keys), exc_info=True)

async def flushall(self, *, async_op: Optional[bool] = False):
if self._pool is None:
raise NoRedisConfigured()
return await self._pool.keys(f"{key}*")
ops = [b"FLUSHDB"]
if async_op:
ops.append(b"ASYNC")
with watch("flush"):
await self._pool.execute_command(*ops)

async def delete_all(self, keys: List[str]):
async def expire(self, key: str, expire: int):
if self._pool is None:
raise NoRedisConfigured()
for key in keys:
try:
with watch("delete_many"):
await self._pool.delete(key)
logger.debug("Deleted cache keys {}".format(keys))
except Exception:
logger.warning("Error deleting cache keys {}".format(keys), exc_info=True)
await self._pool.expire(key, expire)

async def flushall(self, *, async_op: bool = False):
async def keys_startswith(self, key: str):
if self._pool is None:
raise NoRedisConfigured()
with watch("flush"):
await self._pool.flushdb(asynchronous=async_op)
return await self._pool.keys(f"{key}*")

# PUBSUB API

Expand Down Expand Up @@ -179,4 +186,4 @@ async def _listener(self, p: PubSub):
while True:
message = await p.get_message(ignore_subscribe_messages=True, timeout=1)
if message is not None:
yield message["data"]
yield message["data"]
4 changes: 2 additions & 2 deletions guillotina/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class TestRedisMetrics:
async def test_set_redis_metric(self, metrics_registry):
driver = RedisDriver()
driver._pool = AsyncMock()
driver._pool.execute.return_value = b"OK"
driver._pool.set.return_value = b"OK"
await driver.set("foo", "bar")

assert (
Expand Down Expand Up @@ -59,7 +59,7 @@ async def test_get_redis_metric(self, metrics_registry):
async def test_get_miss_redis_metric(self, metrics_registry):
driver = RedisDriver()
driver._pool = AsyncMock()
driver._pool.execute.return_value = None
driver._pool.get.return_value = None
await driver.get("foo")
assert (
metrics_registry.get_sample_value(
Expand Down

0 comments on commit d72f5f5

Please sign in to comment.