-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredis.py
More file actions
58 lines (43 loc) · 1.6 KB
/
redis.py
File metadata and controls
58 lines (43 loc) · 1.6 KB
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
"""
──────────────────────
OBJECTIVE INTERACTIVE™
──────────────────────
DEVELOPED BY: rop_e1013
A module that handles Redis database usage and utilites.
"""
import redis
from typing import Optional, Any
from enum import IntEnum
# == TYPE ALIASES == #
_Key = str | int | bytes
class BaseName(IntEnum):
testdata = 0
userdata = 1
itemdata = 2
reptdata = 3
class MemoryStore(redis.Redis):
IP: str = "localhost"
def __init__(self, name: BaseName) -> None:
super().__init__(self.IP, 6379, int(name))
def __getitem__(self, __key: _Key) -> int | str:
value: bytes = self.get(__key)
if value is None:
raise KeyError(__key)
if value.isdigit:
return int(value)
else:
return value.decode()
def __setitem__(self, __key: _Key, __value: Any) -> None:
if isinstance(__value, dict): return self.mset(__value)
if isinstance(__value, list): return self.lpush(__key, *__value)
return self.set(__key, __value)
def __delitem__(self, __key: str) -> None:
self.delete(__key)
def keys(self, pattern: str = "*", **kwargs) -> list[bytes]:
return super().keys(pattern, **kwargs)
def hgetall(self, name: _Key) -> dict[bytes, bytes]:
return super().hgetall(name)
def lindex(self, name: _Key, index: int) -> Optional[bytes]:
return super().lindex(name, index)
def lrange(self, name: _Key, start: int, end: int) -> list[bytes]:
return super().lrange(name, start, end)