-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathredis_db.py
89 lines (75 loc) · 2.27 KB
/
redis_db.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
82
83
84
85
86
87
88
89
import logging
import os
import sys
import threading
import typing
from typing import Any
from redis import Redis as r
from config import HOST, PASSWORD, PORT
log = logging.getLogger("telethon")
class Redis(r):
def __init__(
self,
host: str = None,
port: int = None,
password: str = None,
logger=log,
encoding: str = "utf-8",
decode_responses=True,
**kwargs,
):
if ":" in host:
data = host.split(":")
host = data[0]
port = int(data[1])
if host.startswith("http"):
logger.error("Your REDIS_URI should not start with http!")
sys.exit()
elif not host or not port:
logger.error("Port Number not found")
sys.exit()
kwargs["host"] = host
if password and len(password) > 1:
kwargs["password"] = password
kwargs["port"] = port
kwargs["encoding"] = encoding
kwargs["decode_responses"] = decode_responses
# kwargs['client_name'] = client_name
# kwargs['username'] = username
try:
super().__init__(**kwargs)
except Exception as e:
logger.exception(f"Error while connecting to redis: {e}")
sys.exit()
self.logger = logger
self._cache = {}
threading.Thread(target=self.re_cache).start()
def re_cache(self):
key = self.keys()
for keys in key:
self._cache[keys] = self.get(keys)
self.logger.info("Cached {} keys".format(len(self._cache)))
def get_key(self, key: Any):
if key in self._cache:
return self._cache[key]
else:
data = self.get(key)
self._cache[key] = data
return data
def del_key(self, key: Any):
if key in self._cache:
del self._cache[key]
return self.delete(key)
def set_key(self, key: Any = None, value: Any = None):
self._cache[key] = value
return self.set(key, value)
db = Redis(
host=HOST,
port=PORT,
password=PASSWORD if len(PASSWORD) > 1 else None,
decode_responses=True,
)
log.info(f"Starting redis on {HOST}:{PORT}")
if not db.ping():
log.error(f"Redis is not available on {HOST}:{PORT}")
exit(1)