Add Redis implementation of CacheDB

- Add RedisCache class
- Add REDIS_HOST env var
This commit is contained in:
trueold89 2024-08-05 21:21:13 +03:00
parent 1e3ce3a55e
commit dd27264fd5
Signed by: trueold89
GPG Key ID: C122E85DD49E6B30
6 changed files with 104 additions and 2 deletions

View File

@ -7,4 +7,5 @@ frozenlist>=1.4.1
idna>=3.7 idna>=3.7
multidict>=6.0.5 multidict>=6.0.5
python-magic>=0.4.27 python-magic>=0.4.27
redis>=5.0.8
yarl>=1.9.4 yarl>=1.9.4

View File

@ -7,6 +7,11 @@ setup(
author="ORUDO", author="ORUDO",
author_email="root@orudo.ru", author_email="root@orudo.ru",
description="A simple Telegram bot that will allow you to upload torrent files / magnet links to a remote Torrent server (qBitTorrent, Transmission, etc.)", description="A simple Telegram bot that will allow you to upload torrent files / magnet links to a remote Torrent server (qBitTorrent, Transmission, etc.)",
install_requires=["aiohttp>=3.10.0", "aiofiles>=24.1.0", "aiofiles>=24.1.0"], install_requires=[
"aiohttp>=3.10.0",
"aiofiles>=24.1.0",
"aiofiles>=24.1.0",
"redis>=5.0.8",
],
packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"], packages=["tubot", "tubot.static", "tubot.torrent", "tubot.dirgetter", "db"],
) )

View File

@ -13,6 +13,9 @@ from aiofiles.ospath import isdir, isfile
from aiofiles.os import mkdir from aiofiles.os import mkdir
from aiofiles import open from aiofiles import open
from asyncio import sleep from asyncio import sleep
from redis import asyncio as aioredis
from json import loads as json_loads
from json import dumps as json_dumps
class PythonCache(CacheDB): class PythonCache(CacheDB):
@ -100,3 +103,87 @@ class PythonCache(CacheDB):
@property @property
async def get_dirs(self) -> dict: async def get_dirs(self) -> dict:
return self.dirs return self.dirs
class RedisCache(CacheDB):
"""
Redis implementation of Cache DataBase
"""
_ctype = CacheDBTypes.Redis
host: str
def __init__(self, redis_host: str) -> None:
super().__init__()
self.host = redis_host
async def __validate__(self) -> bool:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
return await redis.ping()
# Users
async def add_user(self, tg_id: int, name: str) -> None:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
user = User(tg_id, name)
json = json_dumps(user.dict)
await redis.set(str(tg_id), json)
async def user_state(self, tg_id: int) -> UserStates:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
json = await redis.get(str(tg_id))
user = json_loads(json)
return user["state"]
async def change_user_state(self, tg_id: int, status: UserStates) -> None:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
json = await redis.get(str(tg_id))
user = json_loads(json)
user["state"] = status
json = json_dumps(user)
await redis.set(str(tg_id), json)
async def auth_user(self, tg_id: int) -> None:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
json = await redis.get(str(tg_id))
user = json_loads(json)
user["auth"] = True
json = json_dumps(user)
await redis.set(str(tg_id), json)
async def is_user_auth(self, tg_id: int) -> bool:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
json = await redis.get(str(tg_id))
user = json_loads(json)
return bool(user["auth"])
# Dirs
async def cache_dirs(self, dirs: dict, expire: int) -> None:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
json = json_dumps(dirs)
await redis.set("dirs", json, ex=expire)
@property
async def get_dirs(self) -> dict:
async with aioredis.from_url(
f"redis://{self.host}", encoding="utf-8", decode_responses=True
) as redis:
resp = await redis.get("dirs")
if resp is None:
return {}
return json_loads(resp)

View File

@ -14,6 +14,7 @@ class CacheDBTypes(Enum):
""" """
PythonPKL = "python" PythonPKL = "python"
Redis = "redis"
class UserStates(Enum): class UserStates(Enum):

View File

@ -26,7 +26,6 @@ class IValidatable(ABC):
class ENV(object): class ENV(object):
_name: str | None = None _name: str | None = None
DEFAULT: str DEFAULT: str

View File

@ -15,3 +15,12 @@ class PN_CACHE(ENV):
_name = "PN_CACHE" _name = "PN_CACHE"
DEFAULT = "/etc/tubot" DEFAULT = "/etc/tubot"
class REDIS_HOST(ENV):
"""
Redis host adress
"""
_name = "REDIS_HOST"
DEFAULT = "localhost:6379"