ezKit 2.0.2__tar.gz → 2.0.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 2.0.2
3
+ Version: 2.0.4
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -51,7 +51,7 @@ class HTTPClient:
51
51
 
52
52
  info: str = f"request get {url}"
53
53
 
54
- logger.info(f"{info} | start")
54
+ logger.info(f"{info} [ start ]")
55
55
 
56
56
  try:
57
57
  response = self.session.get(url, timeout=self.timeout, **kwargs)
@@ -0,0 +1,159 @@
1
+ """Redis Library"""
2
+
3
+ import random
4
+
5
+ from os import environ
6
+ from typing import Any
7
+
8
+ import orjson
9
+ import redis
10
+
11
+ from loguru import logger
12
+
13
+ # --------------------------------------------------------------------------------------------------
14
+
15
+ DEBUG = environ.get("DEBUG")
16
+
17
+ # --------------------------------------------------------------------------------------------------
18
+
19
+
20
+ class RedisClient:
21
+ """Redis Client"""
22
+
23
+ def __init__(self, url: str, **kwargs):
24
+
25
+ self.pool = redis.ConnectionPool.from_url(
26
+ url,
27
+ **{
28
+ "max_connections": 20, # 生产环境建议设置
29
+ "retry_on_timeout": True, # 超时重试
30
+ "decode_responses": True, # 自动解码为字符串
31
+ **kwargs,
32
+ }
33
+ )
34
+ self.client = redis.Redis(connection_pool=self.pool)
35
+
36
+ # ---------- String ----------
37
+ def set(self, key: str, value: Any, ex: int | None = None, random_ttl: bool = False):
38
+
39
+ try:
40
+
41
+ if random_ttl and ex:
42
+ ex = ex + random.randint(1, 300)
43
+ if not isinstance(value, str):
44
+ # value = json.dumps(value)
45
+ # pylint: disable=E1101
46
+ value = orjson.dumps(value).decode()
47
+ # pylint: enable=E1101
48
+ return self.client.set(key, value, ex=ex)
49
+
50
+ except Exception as e:
51
+ if DEBUG:
52
+ logger.exception(e)
53
+ else:
54
+ logger.error(e)
55
+ return None
56
+
57
+ def get(self, key: str):
58
+ try:
59
+ return self.client.get(key)
60
+ except Exception as e:
61
+ if DEBUG:
62
+ logger.exception(e)
63
+ else:
64
+ logger.error(e)
65
+ return None
66
+
67
+ # ---------- Hash ----------
68
+ def hset(self, key: str, mapping: dict):
69
+ try:
70
+ return self.client.hset(key, mapping=mapping)
71
+ except Exception as e:
72
+ if DEBUG:
73
+ logger.exception(e)
74
+ else:
75
+ logger.error(e)
76
+ return None
77
+
78
+ def hgetall(self, key: str):
79
+ try:
80
+ return self.client.hgetall(key)
81
+ except Exception as e:
82
+ if DEBUG:
83
+ logger.exception(e)
84
+ else:
85
+ logger.error(e)
86
+ return None
87
+
88
+ # ---------- List ----------
89
+ def push(self, key: str, *values):
90
+ try:
91
+ return self.client.rpush(key, *values)
92
+ except Exception as e:
93
+ if DEBUG:
94
+ logger.exception(e)
95
+ else:
96
+ logger.error(e)
97
+ return None
98
+
99
+ def pop(self, key: str):
100
+ try:
101
+ return self.client.lpop(key)
102
+ except Exception as e:
103
+ if DEBUG:
104
+ logger.exception(e)
105
+ else:
106
+ logger.error(e)
107
+ return None
108
+
109
+ # ---------- Set ----------
110
+ def sadd(self, key: str, *values):
111
+ try:
112
+ return self.client.sadd(key, *values)
113
+ except Exception as e:
114
+ if DEBUG:
115
+ logger.exception(e)
116
+ else:
117
+ logger.error(e)
118
+ return None
119
+
120
+ def sismember(self, key: str, value):
121
+ try:
122
+ return self.client.sismember(key, value)
123
+ except Exception as e:
124
+ if DEBUG:
125
+ logger.exception(e)
126
+ else:
127
+ logger.error(e)
128
+ return None
129
+
130
+ # ---------- Atomic ops ----------
131
+ def incr(self, key: str, step: int = 1):
132
+ try:
133
+ return self.client.incrby(key, step)
134
+ except Exception as e:
135
+ if DEBUG:
136
+ logger.exception(e)
137
+ else:
138
+ logger.error(e)
139
+ return None
140
+
141
+ # ---------- Lock ----------
142
+ def lock(self, key: str, timeout=5):
143
+ try:
144
+ return self.client.lock(key, timeout=timeout)
145
+ except Exception as e:
146
+ if DEBUG:
147
+ logger.exception(e)
148
+ else:
149
+ logger.error(e)
150
+ return None
151
+
152
+ # ---------- Scan ----------
153
+ def scan(self, pattern: str):
154
+
155
+ # for key in self.client.scan_iter(pattern):
156
+ # yield key
157
+
158
+ # https://pylint.readthedocs.io/en/latest/user_guide/messages/refactor/use-yield-from.html
159
+ yield from self.client.scan_iter(pattern)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ezKit
3
- Version: 2.0.2
3
+ Version: 2.0.4
4
4
  Summary: Easy Kit
5
5
  Author: septvean
6
6
  Author-email: septvean@gmail.com
@@ -6,6 +6,7 @@ setup.py
6
6
  ezKit/__init__.py
7
7
  ezKit/database.py
8
8
  ezKit/http.py
9
+ ezKit/redix.py
9
10
  ezKit/utils.py
10
11
  ezKit.egg-info/PKG-INFO
11
12
  ezKit.egg-info/SOURCES.txt
@@ -4,7 +4,7 @@ from setuptools import find_packages, setup
4
4
 
5
5
  setup(
6
6
  name="ezKit",
7
- version="2.0.2",
7
+ version="2.0.4",
8
8
  author="septvean",
9
9
  author_email="septvean@gmail.com",
10
10
  description="Easy Kit",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes