mm_redis 1.4.2 → 1.5.0
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.
- package/README.md +298 -0
- package/index.js +2021 -382
- package/package.json +33 -33
- package/test.js +83 -87
- package/index_new.js +0 -585
- package/test2.js +0 -22
package/README.md
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# mm_redis
|
|
2
|
+
|
|
3
|
+
mm_redis是一个高效的Redis客户端封装库,提供了全面的Redis功能支持,包括连接池管理、数据类型操作、错误处理和批量操作优化。
|
|
4
|
+
|
|
5
|
+
## 版本信息
|
|
6
|
+
|
|
7
|
+
- **当前版本**: 1.6.0
|
|
8
|
+
- **更新日期**: 2024-01-10
|
|
9
|
+
- **作者**: 邱文武
|
|
10
|
+
|
|
11
|
+
## 功能特性
|
|
12
|
+
|
|
13
|
+
- 完整的连接池管理,优化并发性能
|
|
14
|
+
- 支持Pipeline批量操作,提升执行效率
|
|
15
|
+
- 支持所有主要Redis数据类型:String、List、Hash等
|
|
16
|
+
- 自动重连机制,使用指数退避策略
|
|
17
|
+
- 全面的错误处理和日志记录
|
|
18
|
+
- 自动JSON序列化/反序列化
|
|
19
|
+
- 键前缀管理,支持多业务隔离
|
|
20
|
+
- 自动连接管理,无需手动维护连接状态
|
|
21
|
+
|
|
22
|
+
## 安装
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install mm_redis --save
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## 基本配置
|
|
29
|
+
|
|
30
|
+
```javascript
|
|
31
|
+
const { Redis } = require('mm_redis');
|
|
32
|
+
|
|
33
|
+
// 创建Redis实例
|
|
34
|
+
const redis = new Redis('default');
|
|
35
|
+
|
|
36
|
+
// 配置Redis连接
|
|
37
|
+
redis.setConfig({
|
|
38
|
+
// 服务器地址
|
|
39
|
+
"host": "localhost",
|
|
40
|
+
// 端口号
|
|
41
|
+
"port": 6379,
|
|
42
|
+
// 密码
|
|
43
|
+
"password": "",
|
|
44
|
+
// 数据库
|
|
45
|
+
"database": 0,
|
|
46
|
+
// 前缀
|
|
47
|
+
"prefix": "app:",
|
|
48
|
+
// 连接超时时间(ms)
|
|
49
|
+
"connect_timeout": 10000,
|
|
50
|
+
// 重试次数
|
|
51
|
+
"retry_count": 3,
|
|
52
|
+
// 重试间隔(ms)
|
|
53
|
+
"retry_delay": 1000,
|
|
54
|
+
// 连接池配置
|
|
55
|
+
"pool": {
|
|
56
|
+
"max_clients": 10, // 最大连接数
|
|
57
|
+
"min_idle": 2, // 最小空闲连接数
|
|
58
|
+
"max_idle": 5, // 最大空闲连接数
|
|
59
|
+
"idle_timeout": 30000 // 空闲超时时间(ms)
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## 使用示例
|
|
65
|
+
|
|
66
|
+
### 基本操作
|
|
67
|
+
|
|
68
|
+
```javascript
|
|
69
|
+
// 连接Redis
|
|
70
|
+
await redis.open();
|
|
71
|
+
|
|
72
|
+
// 设置缓存
|
|
73
|
+
await redis.set('user:1', { name: '张三', age: 25 }, 3600);
|
|
74
|
+
|
|
75
|
+
// 获取缓存
|
|
76
|
+
const user = await redis.get('user:1');
|
|
77
|
+
$.log.debug('[Redis] 获取用户信息:', user);
|
|
78
|
+
|
|
79
|
+
// 判断键是否存在
|
|
80
|
+
const exists = await redis.has('user:1');
|
|
81
|
+
|
|
82
|
+
// 删除缓存
|
|
83
|
+
await redis.del('user:1');
|
|
84
|
+
|
|
85
|
+
// 关闭连接
|
|
86
|
+
await redis.close();
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### 列表操作
|
|
90
|
+
|
|
91
|
+
```javascript
|
|
92
|
+
// 设置列表
|
|
93
|
+
await redis.list_set('users', ['user1', 'user2', 'user3']);
|
|
94
|
+
|
|
95
|
+
// 添加元素到列表
|
|
96
|
+
await redis.list_push('users', 'user4');
|
|
97
|
+
|
|
98
|
+
// 获取列表
|
|
99
|
+
const users = await redis.list_get('users');
|
|
100
|
+
$.log.debug('[Redis] 用户列表:', users);
|
|
101
|
+
|
|
102
|
+
// 获取列表的部分元素
|
|
103
|
+
const partialUsers = await redis.list_get('users', 0, 2);
|
|
104
|
+
|
|
105
|
+
// 清空列表
|
|
106
|
+
await redis.list_clear('users');
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Hash操作
|
|
110
|
+
|
|
111
|
+
```javascript
|
|
112
|
+
// 设置哈希字段
|
|
113
|
+
await redis.hset('user:profile:1', 'name', '张三');
|
|
114
|
+
await redis.hset('user:profile:1', 'age', 25);
|
|
115
|
+
|
|
116
|
+
// 批量设置哈希字段
|
|
117
|
+
await redis.hmset('user:profile:1', {
|
|
118
|
+
'email': 'zhangsan@example.com',
|
|
119
|
+
'phone': '13800138000'
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
// 获取哈希字段
|
|
123
|
+
const name = await redis.hget('user:profile:1', 'name');
|
|
124
|
+
|
|
125
|
+
// 获取哈希的所有字段和值
|
|
126
|
+
const profile = await redis.hgetall('user:profile:1');
|
|
127
|
+
$.log.debug('[Redis] 用户资料:', profile);
|
|
128
|
+
|
|
129
|
+
// 获取哈希的所有字段名
|
|
130
|
+
const fields = await redis.hkeys('user:profile:1');
|
|
131
|
+
|
|
132
|
+
// 获取哈希的所有字段值
|
|
133
|
+
const values = await redis.hvals('user:profile:1');
|
|
134
|
+
|
|
135
|
+
// 判断哈希字段是否存在
|
|
136
|
+
const hasEmail = await redis.hexists('user:profile:1', 'email');
|
|
137
|
+
|
|
138
|
+
// 获取哈希字段数量
|
|
139
|
+
const fieldCount = await redis.hlen('user:profile:1');
|
|
140
|
+
|
|
141
|
+
// 删除哈希字段
|
|
142
|
+
await redis.hdel('user:profile:1', ['email', 'phone']);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 批量操作
|
|
146
|
+
|
|
147
|
+
```javascript
|
|
148
|
+
// 批量获取
|
|
149
|
+
const data = await redis.mget(['user:1', 'user:2', 'user:3']);
|
|
150
|
+
$.log.debug('[Redis] 批量获取结果:', data);
|
|
151
|
+
|
|
152
|
+
// 批量设置
|
|
153
|
+
await redis.mset({
|
|
154
|
+
'user:4': { name: '李四', age: 30 },
|
|
155
|
+
'user:5': { name: '王五', age: 35 }
|
|
156
|
+
}, 3600);
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### 使用Pipeline优化批量操作
|
|
160
|
+
|
|
161
|
+
```javascript
|
|
162
|
+
// 创建Pipeline
|
|
163
|
+
const pipeline = redis.pipeline();
|
|
164
|
+
|
|
165
|
+
// 添加多个命令
|
|
166
|
+
pipeline.set('counter:1', 100);
|
|
167
|
+
pipeline.ttl('counter:1', 3600);
|
|
168
|
+
pipeline.hset('stats:today', 'visits', 500);
|
|
169
|
+
pipeline.hset('stats:today', 'users', 100);
|
|
170
|
+
|
|
171
|
+
// 执行所有命令
|
|
172
|
+
const results = await pipeline.exec();
|
|
173
|
+
$.log.debug('[Redis] Pipeline执行结果:', results);
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
### 发布订阅
|
|
177
|
+
|
|
178
|
+
```javascript
|
|
179
|
+
// 订阅频道
|
|
180
|
+
await redis.subscribe('notifications', async (message) => {
|
|
181
|
+
$.log.debug('[Redis] 收到通知:', message);
|
|
182
|
+
// 处理接收到的消息
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
// 发布消息
|
|
186
|
+
const receivedCount = await redis.publish('notifications', JSON.stringify({
|
|
187
|
+
type: 'info',
|
|
188
|
+
content: '系统更新成功'
|
|
189
|
+
}));
|
|
190
|
+
|
|
191
|
+
$.log.debug(`[Redis] 消息已发布,${receivedCount}个客户端接收`);
|
|
192
|
+
|
|
193
|
+
// 取消订阅
|
|
194
|
+
await redis.unsubscribe('notifications');
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## API文档
|
|
198
|
+
|
|
199
|
+
### 连接管理
|
|
200
|
+
|
|
201
|
+
- `setConfig(config)`: 设置Redis配置
|
|
202
|
+
- `open()`: 连接Redis
|
|
203
|
+
- `close()`: 关闭连接
|
|
204
|
+
- `isConnected()`: 检查连接状态
|
|
205
|
+
|
|
206
|
+
### 字符串操作
|
|
207
|
+
|
|
208
|
+
- `set(key, value, seconds)`: 设置缓存
|
|
209
|
+
- `get(key)`: 获取缓存
|
|
210
|
+
- `has(key)`: 判断键是否存在
|
|
211
|
+
- `del(key)`: 删除缓存
|
|
212
|
+
- `add(key, value, seconds)`: 仅当键不存在时设置缓存
|
|
213
|
+
- `ttl(key, seconds)`: 设置或获取过期时间
|
|
214
|
+
- `addInt(key, num)`: 增加整数值
|
|
215
|
+
- `addFloat(key, num)`: 增加浮点数值
|
|
216
|
+
- `addStr(key, str)`: 追加字符串
|
|
217
|
+
- `getrange(key, start, end)`: 获取字符串子串
|
|
218
|
+
- `setrange(key, index, value)`: 设置字符串子串
|
|
219
|
+
|
|
220
|
+
### 列表操作
|
|
221
|
+
|
|
222
|
+
- `list_set(key, value, seconds)`: 设置列表
|
|
223
|
+
- `list_push(key, value)`: 添加元素到列表
|
|
224
|
+
- `list_get(key, start, end)`: 获取列表
|
|
225
|
+
- `list_has(key, value)`: 判断列表中是否存在元素
|
|
226
|
+
- `list_clear(key, value)`: 清空列表或重置列表
|
|
227
|
+
|
|
228
|
+
### Hash操作
|
|
229
|
+
|
|
230
|
+
- `hset(key, field, value)`: 设置哈希字段
|
|
231
|
+
- `hmset(key, obj)`: 批量设置哈希字段
|
|
232
|
+
- `hget(key, field)`: 获取哈希字段值
|
|
233
|
+
- `hgetall(key)`: 获取哈希的所有字段和值
|
|
234
|
+
- `hkeys(key)`: 获取哈希的所有字段名
|
|
235
|
+
- `hvals(key)`: 获取哈希的所有字段值
|
|
236
|
+
- `hdel(key, fields)`: 删除哈希字段
|
|
237
|
+
- `hexists(key, field)`: 判断哈希字段是否存在
|
|
238
|
+
- `hlen(key)`: 获取哈希字段数量
|
|
239
|
+
|
|
240
|
+
### 批量操作
|
|
241
|
+
|
|
242
|
+
- `mget(keys)`: 批量获取缓存
|
|
243
|
+
- `mset(obj, seconds)`: 批量设置缓存
|
|
244
|
+
- `pipeline()`: 创建Pipeline对象进行批量操作优化
|
|
245
|
+
|
|
246
|
+
### 高级操作
|
|
247
|
+
|
|
248
|
+
- `subscribe(channel, func)`: 订阅频道
|
|
249
|
+
- `unsubscribe(channel)`: 取消订阅
|
|
250
|
+
- `publish(channel, message)`: 发布消息
|
|
251
|
+
- `clear(key)`: 清空缓存或当前数据库
|
|
252
|
+
- `sort(key, options)`: 排序
|
|
253
|
+
- `keys(pattern)`: 获取所有键名
|
|
254
|
+
|
|
255
|
+
## 连接池管理
|
|
256
|
+
|
|
257
|
+
本模块内置了高效的连接池管理,可自动处理连接的创建、回收和复用。连接池配置可在初始化时设置:
|
|
258
|
+
|
|
259
|
+
```javascript
|
|
260
|
+
{
|
|
261
|
+
"pool": {
|
|
262
|
+
"max_clients": 10, // 最大连接数
|
|
263
|
+
"min_idle": 2, // 最小空闲连接数
|
|
264
|
+
"max_idle": 5, // 最大空闲连接数
|
|
265
|
+
"idle_timeout": 30000 // 空闲超时时间(ms)
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
## 错误处理
|
|
271
|
+
|
|
272
|
+
所有方法都包含了完善的错误处理机制,并提供详细的日志输出。在发生错误时,会自动记录错误信息,并在可能的情况下提供回退方案。
|
|
273
|
+
|
|
274
|
+
## 最佳实践
|
|
275
|
+
|
|
276
|
+
1. **使用连接池**: 对于高并发应用,连接池配置至关重要,合理设置最大连接数和空闲连接数
|
|
277
|
+
2. **使用Pipeline**: 对于批量操作,使用Pipeline可显著提升性能
|
|
278
|
+
3. **合理设置过期时间**: 避免Redis中数据无限增长
|
|
279
|
+
4. **使用键前缀**: 通过键前缀进行业务隔离,避免键冲突
|
|
280
|
+
5. **及时关闭连接**: 在应用结束时调用close()方法关闭所有连接
|
|
281
|
+
|
|
282
|
+
## 版本更新
|
|
283
|
+
|
|
284
|
+
### 1.6.0 (2024-01-10)
|
|
285
|
+
- 添加完整的连接池管理功能
|
|
286
|
+
- 新增Pipeline批量操作优化
|
|
287
|
+
- 增加Hash数据类型支持
|
|
288
|
+
- 增强错误处理和日志记录
|
|
289
|
+
- 优化重连机制,实现指数退避策略
|
|
290
|
+
|
|
291
|
+
### 1.5.0
|
|
292
|
+
- 优化连接管理
|
|
293
|
+
- 完善错误处理
|
|
294
|
+
- 提升代码稳定性
|
|
295
|
+
|
|
296
|
+
### 1.0.0
|
|
297
|
+
- 初始版本发布
|
|
298
|
+
- 基础Redis功能支持
|