Added support for all Redis versions (>=1.0.0) (#5)
Previously support was provided for Redis>=6.2.0 Reviewed-on: #5 Co-authored-by: Ivan Golikov <root@ivnglkv.me> Co-committed-by: Ivan Golikov <root@ivnglkv.me>
This commit is contained in:
parent
f8a67e5fbd
commit
6804360352
4 changed files with 78 additions and 3 deletions
|
@ -1,7 +1,10 @@
|
|||
from functools import lru_cache
|
||||
from uuid import uuid4
|
||||
|
||||
from cryptography.fernet import Fernet
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import ResponseError
|
||||
from redis.typing import ResponseT
|
||||
|
||||
from pssecret_server.models import Secret
|
||||
|
||||
|
@ -30,3 +33,35 @@ async def save_secret(data: Secret, redis: Redis) -> str:
|
|||
await redis.setex(new_key, 60 * 60 * 24, data.data)
|
||||
|
||||
return new_key
|
||||
|
||||
|
||||
@lru_cache
|
||||
async def _is_getdel_available(redis: Redis) -> bool:
|
||||
"""Checks the availability of GETDEL command on the Redis server instance
|
||||
|
||||
GETDEL is not available in Redis prior to version 6.2
|
||||
"""
|
||||
try:
|
||||
await redis.getdel("test:getdel:availability")
|
||||
except ResponseError:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
|
||||
async def getdel(redis: Redis, key: str) -> ResponseT:
|
||||
"""Gets the value of key and deletes the key
|
||||
|
||||
Depending on the capabilities of Redis server this function
|
||||
will either call GETDEL command, either first call GETSET with empty string
|
||||
and DEL right after that.
|
||||
"""
|
||||
result: ResponseT
|
||||
|
||||
if await _is_getdel_available(redis):
|
||||
result = await redis.getdel(key)
|
||||
else:
|
||||
result = await redis.getset(key, "")
|
||||
await redis.delete(key)
|
||||
|
||||
return result
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue