-
Notifications
You must be signed in to change notification settings - Fork 9
/
database.py
81 lines (70 loc) · 2.58 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import asyncio
from redis.asyncio import Redis
from config import Database
from typing import List, Union
class RedisClient:
def __init__(self, host: str, port: int, password: str):
self.db = Redis(
host=host,
port=port,
password=password,
decode_responses=True,
)
def s_l(self, text: str) -> List[str]:
return text.split(" ")
def l_s(self, lst: List[str]) -> str:
return " ".join(lst).strip()
def ensure_str(self, value: Union[str, int]) -> str:
if isinstance(value, (str, int)):
return str(value)
else:
raise ValueError("Invalid input type: value should be str or int")
async def is_inserted(self, var: Union[str, int], id: Union[str, int]) -> bool:
try:
var_str = self.ensure_str(var)
id_str = self.ensure_str(id)
users = await self.fetch_all(var_str)
return id_str in users
except Exception as e:
print(f"Error in is_inserted: {e}")
return False
async def insert(self, var: Union[str, int], id: Union[str, int]) -> bool:
try:
var_str = self.ensure_str(var)
id_str = self.ensure_str(id)
users = await self.fetch_all(var_str)
if id_str not in users:
users.append(id_str)
await self.db.set(var_str, self.l_s(users))
return True
except Exception as e:
print(f"Error in insert: {e}")
return False
async def fetch_all(self, var: str) -> List[str]:
if not isinstance(var, str):
raise ValueError("Invalid input type: 'var' should be str")
try:
users = await self.db.get(var)
return [] if users is None or users == "" else self.s_l(users)
except Exception as e:
print(f"Error in fetch_all: {e}")
return []
async def delete(self, var: Union[str, int], id: Union[str, int]) -> bool:
try:
var_str = self.ensure_str(var)
id_str = self.ensure_str(id)
users = await self.fetch_all(var_str)
if id_str in users:
users.remove(id_str)
await self.db.set(var_str, self.l_s(users))
return True
except Exception as e:
print(f"Error in delete: {e}")
return False
# Configuration
REDIS_URI = Database.REDIS_URI.split(":")
host = REDIS_URI[0]
port = int(REDIS_URI[1])
password = Database.REDIS_PASSWORD
# db Instance
db = RedisClient(host, port, password)