mm_cachebase 1.4.5 → 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/LICENSE +21 -0
- package/README.md +291 -91
- package/compatibility_table.md +63 -0
- package/index.js +1215 -535
- package/package.json +41 -35
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023 邱文武
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
# mm_cachebase
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
一个轻量级的本地缓存类库,提供了与Redis兼容的缓存操作接口,支持过期时间设置、哈希表操作、数值操作、字符串操作等功能,以及完整的事件通知系统。
|
|
4
|
+
|
|
5
|
+
## 特点
|
|
6
|
+
|
|
7
|
+
- **Redis兼容API**:实现了与npm redis模块相似的接口,便于无缝切换
|
|
8
|
+
- **轻量级设计**:纯JavaScript实现,无外部依赖
|
|
9
|
+
- **完整的事件系统**:支持前置/后置/错误事件,便于日志记录和访问控制
|
|
10
|
+
- **过期时间管理**:支持设置和查询键的过期时间
|
|
11
|
+
- **丰富的数据类型支持**:支持字符串、哈希表等Redis常见数据类型
|
|
4
12
|
|
|
5
13
|
## 安装
|
|
6
14
|
|
|
@@ -38,9 +46,7 @@ async function example() {
|
|
|
38
46
|
const cache = new CacheBase();
|
|
39
47
|
```
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
- `list`: 存储缓存数据的数组
|
|
43
|
-
- `func`: 到期通知函数(可选)
|
|
49
|
+
创建一个新的缓存实例。缓存实例基于EventEmitter,支持完整的事件系统。
|
|
44
50
|
|
|
45
51
|
### 基本操作
|
|
46
52
|
|
|
@@ -51,14 +57,14 @@ await cache.set(key, value, maxAge)
|
|
|
51
57
|
- `key`: 缓存键名
|
|
52
58
|
- `value`: 缓存的值
|
|
53
59
|
- `maxAge`: 过期时间(秒),-1表示永不过期
|
|
54
|
-
- 返回:
|
|
60
|
+
- 返回: 'OK'
|
|
55
61
|
|
|
56
62
|
#### 获取缓存
|
|
57
63
|
```javascript
|
|
58
64
|
await cache.get(key)
|
|
59
65
|
```
|
|
60
66
|
- `key`: 缓存键名
|
|
61
|
-
- 返回: 缓存值,不存在则返回
|
|
67
|
+
- 返回: 缓存值,不存在则返回null
|
|
62
68
|
|
|
63
69
|
#### 删除缓存
|
|
64
70
|
```javascript
|
|
@@ -67,120 +73,291 @@ await cache.del(key)
|
|
|
67
73
|
- `key`: 缓存键名
|
|
68
74
|
- 返回: 成功返回1,失败返回0
|
|
69
75
|
|
|
70
|
-
####
|
|
76
|
+
#### 设置并获取旧值
|
|
71
77
|
```javascript
|
|
72
|
-
await cache.
|
|
78
|
+
await cache.getset(key, value, maxAge)
|
|
73
79
|
```
|
|
74
|
-
-
|
|
75
|
-
- 返回:
|
|
80
|
+
- 先获取旧值,然后设置新值
|
|
81
|
+
- 返回: 旧值,不存在则返回null
|
|
82
|
+
|
|
83
|
+
#### 批量设置缓存
|
|
84
|
+
```javascript
|
|
85
|
+
await cache.mset(keys, values, maxAge)
|
|
86
|
+
```
|
|
87
|
+
- `keys`: 键名数组
|
|
88
|
+
- `values`: 值数组
|
|
89
|
+
- 返回: 'OK'
|
|
90
|
+
|
|
91
|
+
#### 批量获取缓存
|
|
92
|
+
```javascript
|
|
93
|
+
await cache.mget(...keys)
|
|
94
|
+
```
|
|
95
|
+
- `keys`: 一个或多个键名
|
|
96
|
+
- 返回: 值数组
|
|
76
97
|
|
|
77
98
|
#### 判断键是否存在
|
|
78
99
|
```javascript
|
|
79
|
-
await cache.
|
|
100
|
+
await cache.exists(...keys)
|
|
80
101
|
```
|
|
81
|
-
-
|
|
102
|
+
- `keys`: 一个或多个键名
|
|
103
|
+
- 返回: 存在的键数量
|
|
82
104
|
|
|
83
105
|
### 过期时间操作
|
|
84
106
|
|
|
85
|
-
####
|
|
107
|
+
#### 设置过期时间
|
|
86
108
|
```javascript
|
|
87
|
-
await cache.
|
|
109
|
+
await cache.expire(key, maxAge)
|
|
88
110
|
```
|
|
89
|
-
- `maxAge`:
|
|
90
|
-
- 返回:
|
|
111
|
+
- `maxAge`: 过期时间(秒)
|
|
112
|
+
- 返回: 成功返回1,失败返回0
|
|
113
|
+
|
|
114
|
+
#### 查询剩余过期时间
|
|
115
|
+
```javascript
|
|
116
|
+
await cache.ttl(key)
|
|
117
|
+
```
|
|
118
|
+
- 返回: 剩余秒数,不存在或永不过期返回-1
|
|
119
|
+
|
|
120
|
+
#### 移除过期时间
|
|
121
|
+
```javascript
|
|
122
|
+
await cache.persist(key)
|
|
123
|
+
```
|
|
124
|
+
- 返回: 成功返回1,失败返回0
|
|
91
125
|
|
|
92
126
|
### 数值操作
|
|
93
127
|
|
|
94
128
|
#### 增加整数
|
|
95
129
|
```javascript
|
|
96
|
-
await cache.
|
|
130
|
+
await cache.incr(key)
|
|
97
131
|
```
|
|
98
|
-
-
|
|
99
|
-
- 返回:
|
|
132
|
+
- 将键的值加1
|
|
133
|
+
- 返回: 增加后的值
|
|
134
|
+
|
|
135
|
+
#### 增加指定整数
|
|
136
|
+
```javascript
|
|
137
|
+
await cache.incrby(key, increment)
|
|
138
|
+
```
|
|
139
|
+
- `increment`: 要增加的整数
|
|
140
|
+
- 返回: 增加后的值
|
|
141
|
+
|
|
142
|
+
#### 减少整数
|
|
143
|
+
```javascript
|
|
144
|
+
await cache.decr(key)
|
|
145
|
+
```
|
|
146
|
+
- 将键的值减1
|
|
147
|
+
- 返回: 减少后的值
|
|
148
|
+
|
|
149
|
+
#### 减少指定整数
|
|
150
|
+
```javascript
|
|
151
|
+
await cache.decrby(key, decrement)
|
|
152
|
+
```
|
|
153
|
+
- `decrement`: 要减少的整数
|
|
154
|
+
- 返回: 减少后的值
|
|
100
155
|
|
|
101
156
|
#### 增加浮点数
|
|
102
157
|
```javascript
|
|
103
|
-
await cache.
|
|
158
|
+
await cache.incrbyfloat(key, increment)
|
|
104
159
|
```
|
|
105
|
-
-
|
|
160
|
+
- `increment`: 要增加的浮点数
|
|
161
|
+
- 返回: 增加后的值
|
|
106
162
|
|
|
107
163
|
### 字符串操作
|
|
108
164
|
|
|
109
165
|
#### 追加字符串
|
|
110
166
|
```javascript
|
|
111
|
-
await cache.
|
|
167
|
+
await cache.append(key, value)
|
|
112
168
|
```
|
|
113
169
|
- 在已有字符串后追加新字符串
|
|
114
|
-
- 返回:
|
|
170
|
+
- 返回: 追加后的字符串长度
|
|
171
|
+
|
|
172
|
+
#### 获取字符串长度
|
|
173
|
+
```javascript
|
|
174
|
+
await cache.strlen(key)
|
|
175
|
+
```
|
|
176
|
+
- 返回: 字符串长度
|
|
177
|
+
|
|
178
|
+
### 哈希表操作
|
|
115
179
|
|
|
116
|
-
####
|
|
180
|
+
#### 设置哈希表字段
|
|
117
181
|
```javascript
|
|
118
|
-
await cache.
|
|
182
|
+
await cache.hset(key, field, value)
|
|
119
183
|
```
|
|
120
|
-
-
|
|
121
|
-
-
|
|
184
|
+
- `key`: 哈希表的键名
|
|
185
|
+
- `field`: 字段名
|
|
186
|
+
- `value`: 字段值
|
|
187
|
+
- 返回: 成功返回1,失败返回0
|
|
188
|
+
|
|
189
|
+
#### 获取哈希表字段
|
|
190
|
+
```javascript
|
|
191
|
+
await cache.hget(key, field)
|
|
192
|
+
```
|
|
193
|
+
- 返回: 字段值,不存在则返回null
|
|
194
|
+
|
|
195
|
+
#### 获取哈希表所有字段和值
|
|
196
|
+
```javascript
|
|
197
|
+
await cache.hgetall(key)
|
|
198
|
+
```
|
|
199
|
+
- 返回: 包含所有字段和值的对象
|
|
122
200
|
|
|
123
|
-
####
|
|
201
|
+
#### 删除哈希表字段
|
|
124
202
|
```javascript
|
|
125
|
-
await cache.
|
|
203
|
+
await cache.hdel(key, field)
|
|
126
204
|
```
|
|
127
|
-
-
|
|
205
|
+
- 返回: 成功返回1,失败返回0
|
|
128
206
|
|
|
129
|
-
###
|
|
207
|
+
### 键操作
|
|
130
208
|
|
|
131
|
-
####
|
|
209
|
+
#### 获取所有键名
|
|
132
210
|
```javascript
|
|
133
|
-
await cache.
|
|
211
|
+
await cache.keys(pattern)
|
|
134
212
|
```
|
|
135
|
-
-
|
|
213
|
+
- `pattern`: 支持*通配符的匹配模式
|
|
214
|
+
- 返回: 匹配的键名数组
|
|
136
215
|
|
|
137
|
-
####
|
|
216
|
+
#### 获取键数量
|
|
138
217
|
```javascript
|
|
139
|
-
await cache.
|
|
218
|
+
await cache.dbsize()
|
|
140
219
|
```
|
|
141
|
-
-
|
|
142
|
-
- 返回: 成功返回1,失败返回-1
|
|
220
|
+
- 返回: 缓存中的键数量
|
|
143
221
|
|
|
144
|
-
####
|
|
222
|
+
#### 清空缓存
|
|
145
223
|
```javascript
|
|
146
|
-
await cache.
|
|
224
|
+
await cache.flushdb()
|
|
147
225
|
```
|
|
148
|
-
-
|
|
226
|
+
- 清空所有缓存
|
|
227
|
+
- 返回: 'OK'
|
|
149
228
|
|
|
150
|
-
####
|
|
229
|
+
#### 销毁缓存实例
|
|
151
230
|
```javascript
|
|
152
|
-
await cache.
|
|
231
|
+
await cache.dispose()
|
|
153
232
|
```
|
|
154
|
-
-
|
|
233
|
+
- 清理所有资源,移除所有监听器
|
|
234
|
+
|
|
235
|
+
### 兼容方法(保持向后兼容)
|
|
155
236
|
|
|
156
|
-
####
|
|
237
|
+
#### 添加缓存(仅当键不存在时)
|
|
157
238
|
```javascript
|
|
158
|
-
await cache.
|
|
239
|
+
await cache.add(key, value, maxAge)
|
|
159
240
|
```
|
|
160
|
-
-
|
|
241
|
+
- 返回: 添加成功返回值,已存在返回0
|
|
161
242
|
|
|
162
|
-
|
|
243
|
+
#### 检查键是否存在
|
|
244
|
+
```javascript
|
|
245
|
+
await cache.has(key)
|
|
246
|
+
```
|
|
247
|
+
- 返回: 存在返回1,不存在返回0
|
|
163
248
|
|
|
164
|
-
####
|
|
249
|
+
#### 增加浮点数(兼容方法)
|
|
165
250
|
```javascript
|
|
166
|
-
await cache.
|
|
251
|
+
await cache.addFloat(key, num)
|
|
167
252
|
```
|
|
168
|
-
- `
|
|
169
|
-
-
|
|
253
|
+
- `num`: 要增加的浮点数
|
|
254
|
+
- 返回: 计算后的结果
|
|
170
255
|
|
|
171
|
-
####
|
|
256
|
+
#### 追加字符串(兼容方法)
|
|
172
257
|
```javascript
|
|
173
|
-
await cache.
|
|
258
|
+
await cache.addStr(key, str)
|
|
174
259
|
```
|
|
175
|
-
-
|
|
176
|
-
- 返回: 匹配的键名数组
|
|
260
|
+
- 返回: 追加后的完整字符串
|
|
177
261
|
|
|
178
|
-
####
|
|
262
|
+
#### 清空匹配缓存(兼容方法)
|
|
179
263
|
```javascript
|
|
180
264
|
await cache.clear(pattern)
|
|
181
265
|
```
|
|
182
|
-
- `pattern`:
|
|
183
|
-
|
|
266
|
+
- `pattern`: 可选,支持*通配符
|
|
267
|
+
|
|
268
|
+
## 事件系统
|
|
269
|
+
|
|
270
|
+
CacheBase 提供了完整的事件系统,允许你监听各种缓存操作的前后事件,以便进行日志记录、数据验证或操作取消等功能。
|
|
271
|
+
|
|
272
|
+
### 事件监听方法
|
|
273
|
+
|
|
274
|
+
```javascript
|
|
275
|
+
// 监听事件
|
|
276
|
+
cache.on('event_name', callback);
|
|
277
|
+
|
|
278
|
+
// 监听一次性事件
|
|
279
|
+
cache.once('event_name', callback);
|
|
280
|
+
|
|
281
|
+
// 移除事件监听
|
|
282
|
+
cache.off('event_name', callback);
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
### 事件参数格式
|
|
286
|
+
|
|
287
|
+
所有事件的回调函数都会接收一个标准化的事件数据对象,包含以下属性:
|
|
288
|
+
|
|
289
|
+
- `key`: 操作的缓存键名(如果适用)
|
|
290
|
+
- `value`: 操作的值,可以是基本类型或包含多个属性的对象
|
|
291
|
+
- `result`: 操作的结果
|
|
292
|
+
- `error`: 错误信息(仅错误事件)
|
|
293
|
+
- `cancel`: 用于取消操作的标志(仅前置事件)
|
|
294
|
+
- `ttl`: 过期时间(秒)(仅过期时间相关操作)
|
|
295
|
+
|
|
296
|
+
### 支持的事件类型
|
|
297
|
+
|
|
298
|
+
每个缓存操作都有对应的前置事件(`_before`)、后置事件(`_after`)和错误事件(`_error`)。
|
|
299
|
+
|
|
300
|
+
#### 基本操作事件
|
|
301
|
+
- `set_before`, `set_after`, `set_error`
|
|
302
|
+
- `get_before`, `get_after`, `get_error`
|
|
303
|
+
- `del_before`, `del_after`, `del_error`
|
|
304
|
+
- `exists_before`, `exists_after`, `exists_error`
|
|
305
|
+
- `mset_before`, `mset_after`, `mset_error`
|
|
306
|
+
- `mget_before`, `mget_after`, `mget_error`
|
|
307
|
+
|
|
308
|
+
#### 过期时间操作事件
|
|
309
|
+
- `expire_before`, `expire_after`, `expire_error`
|
|
310
|
+
- `ttl_before`, `ttl_after`, `ttl_error`
|
|
311
|
+
|
|
312
|
+
#### 数值操作事件
|
|
313
|
+
- `incr_before`, `incr_after`, `incr_error`
|
|
314
|
+
- `decr_before`, `decr_after`, `decr_error`
|
|
315
|
+
- `incrby_before`, `incrby_after`, `incrby_error`
|
|
316
|
+
- `decrby_before`, `decrby_after`, `decrby_error`
|
|
317
|
+
- `incrbyfloat_before`, `incrbyfloat_after`, `incrbyfloat_error`
|
|
318
|
+
|
|
319
|
+
#### 字符串操作事件
|
|
320
|
+
- `append_before`, `append_after`, `append_error`
|
|
321
|
+
- `strlen_before`, `strlen_after`, `strlen_error`
|
|
322
|
+
|
|
323
|
+
#### 哈希表操作事件
|
|
324
|
+
- `hset_before`, `hset_after`, `hset_error`
|
|
325
|
+
- `hget_before`, `hget_after`, `hget_error`
|
|
326
|
+
- `hgetall_before`, `hgetall_after`, `hgetall_error`
|
|
327
|
+
- `hdel_before`, `hdel_after`, `hdel_error`
|
|
328
|
+
|
|
329
|
+
#### 键操作事件
|
|
330
|
+
- `keys_before`, `keys_after`, `keys_error`
|
|
331
|
+
- `dbsize_before`, `dbsize_after`, `dbsize_error`
|
|
332
|
+
- `flushdb_before`, `flushdb_after`, `flushdb_error`
|
|
333
|
+
- `dispose_before`, `dispose_after`, `dispose_error`
|
|
334
|
+
|
|
335
|
+
### 事件使用示例
|
|
336
|
+
|
|
337
|
+
```javascript
|
|
338
|
+
const cache = new CacheBase();
|
|
339
|
+
|
|
340
|
+
// 监听设置操作的前置事件
|
|
341
|
+
cache.on('set_before', (data) => {
|
|
342
|
+
console.log('即将设置键:', data.key, '值:', data.value);
|
|
343
|
+
|
|
344
|
+
// 数据验证示例
|
|
345
|
+
if (typeof data.value === 'string' && data.value.length > 100) {
|
|
346
|
+
console.log('值长度过长,取消设置');
|
|
347
|
+
data.cancel = true; // 取消操作
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
|
|
351
|
+
// 监听获取操作的后置事件
|
|
352
|
+
cache.on('get_after', (data) => {
|
|
353
|
+
console.log(`获取键 ${data.key} 的结果: ${data.result}`);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
// 错误事件监听
|
|
357
|
+
cache.on('get_error', (data) => {
|
|
358
|
+
console.error(`获取键 ${data.key} 时出错: ${data.error.message}`);
|
|
359
|
+
});
|
|
360
|
+
```
|
|
184
361
|
|
|
185
362
|
## 使用示例
|
|
186
363
|
|
|
@@ -197,7 +374,7 @@ async function example() {
|
|
|
197
374
|
console.log(user); // 输出: 张三
|
|
198
375
|
|
|
199
376
|
// 检查是否存在
|
|
200
|
-
const exists = await cache.
|
|
377
|
+
const exists = await cache.exists("user");
|
|
201
378
|
console.log(exists); // 输出: 1
|
|
202
379
|
|
|
203
380
|
// 删除缓存
|
|
@@ -205,53 +382,72 @@ async function example() {
|
|
|
205
382
|
}
|
|
206
383
|
```
|
|
207
384
|
|
|
208
|
-
###
|
|
385
|
+
### 哈希表操作示例
|
|
209
386
|
```javascript
|
|
210
|
-
async function
|
|
211
|
-
//
|
|
212
|
-
await cache.
|
|
387
|
+
async function hashExample() {
|
|
388
|
+
// 设置哈希表字段
|
|
389
|
+
await cache.hset("user:123", "name", "张三");
|
|
390
|
+
await cache.hset("user:123", "age", 30);
|
|
391
|
+
await cache.hset("user:123", "email", "zhangsan@example.com");
|
|
213
392
|
|
|
214
|
-
//
|
|
215
|
-
await cache.
|
|
393
|
+
// 获取哈希表字段
|
|
394
|
+
const name = await cache.hget("user:123", "name");
|
|
395
|
+
console.log(name); // 输出: 张三
|
|
216
396
|
|
|
217
|
-
//
|
|
218
|
-
const
|
|
219
|
-
console.log(
|
|
397
|
+
// 获取所有字段和值
|
|
398
|
+
const user = await cache.hgetall("user:123");
|
|
399
|
+
console.log(user); // 输出: { name: "张三", age: 30, email: "zhangsan@example.com" }
|
|
220
400
|
|
|
221
|
-
//
|
|
222
|
-
await cache.
|
|
401
|
+
// 删除字段
|
|
402
|
+
await cache.hdel("user:123", "email");
|
|
223
403
|
}
|
|
224
404
|
```
|
|
225
405
|
|
|
226
|
-
###
|
|
406
|
+
### 数值操作示例
|
|
227
407
|
```javascript
|
|
228
|
-
async function
|
|
229
|
-
//
|
|
230
|
-
await cache.set("
|
|
408
|
+
async function numberExample() {
|
|
409
|
+
// 设置初始值
|
|
410
|
+
await cache.set("counter", 10);
|
|
411
|
+
|
|
412
|
+
// 增加1
|
|
413
|
+
const incr = await cache.incr("counter");
|
|
414
|
+
console.log("增加后:", incr); // 输出: 11
|
|
231
415
|
|
|
232
|
-
//
|
|
233
|
-
const
|
|
234
|
-
console.log(
|
|
416
|
+
// 增加指定值
|
|
417
|
+
const incrby = await cache.incrby("counter", 5);
|
|
418
|
+
console.log("增加5后:", incrby); // 输出: 16
|
|
235
419
|
|
|
236
|
-
//
|
|
237
|
-
await cache.
|
|
420
|
+
// 减少1
|
|
421
|
+
const decr = await cache.decr("counter");
|
|
422
|
+
console.log("减少后:", decr); // 输出: 15
|
|
238
423
|
|
|
239
|
-
//
|
|
240
|
-
await cache.
|
|
424
|
+
// 浮点数操作
|
|
425
|
+
await cache.set("price", 99.99);
|
|
426
|
+
const incrFloat = await cache.incrbyfloat("price", 0.01);
|
|
427
|
+
console.log("浮点数增加后:", incrFloat); // 输出: 100
|
|
241
428
|
}
|
|
242
429
|
```
|
|
243
430
|
|
|
244
|
-
###
|
|
431
|
+
### 批量操作示例
|
|
245
432
|
```javascript
|
|
246
433
|
async function batchExample() {
|
|
247
|
-
//
|
|
248
|
-
|
|
434
|
+
// 批量设置
|
|
435
|
+
await cache.mset(["key1", "key2", "key3"], ["value1", "value2", "value3"]);
|
|
436
|
+
|
|
437
|
+
// 批量获取
|
|
438
|
+
const values = await cache.mget("key1", "key2", "key3");
|
|
439
|
+
console.log("批量获取:", values); // 输出: ["value1", "value2", "value3"]
|
|
249
440
|
|
|
250
|
-
//
|
|
251
|
-
await cache.
|
|
441
|
+
// 检查多个键是否存在
|
|
442
|
+
const existsCount = await cache.exists("key1", "key2", "nonexistent");
|
|
443
|
+
console.log("存在的键数量:", existsCount); // 输出: 2
|
|
444
|
+
|
|
445
|
+
// 获取键数量
|
|
446
|
+
const count = await cache.dbsize();
|
|
447
|
+
console.log("键总数:", count);
|
|
252
448
|
|
|
253
449
|
// 清空所有缓存
|
|
254
|
-
await cache.
|
|
450
|
+
await cache.flushdb();
|
|
255
451
|
}
|
|
256
452
|
```
|
|
257
453
|
|
|
@@ -259,6 +455,10 @@ async function batchExample() {
|
|
|
259
455
|
|
|
260
456
|
1. 所有操作方法都是异步的,需要使用async/await或Promise处理
|
|
261
457
|
2. 过期时间单位为秒,-1表示永不过期
|
|
262
|
-
3.
|
|
263
|
-
4.
|
|
264
|
-
5.
|
|
458
|
+
3. 缓存数据存储在内存中,重启后数据会丢失
|
|
459
|
+
4. 事件回调函数中的 `cancel` 标志只在前置事件中有效,后置事件中修改不会产生效果
|
|
460
|
+
5. 所有事件参数都遵循统一的格式,包含key、value、result、error和cancel等字段
|
|
461
|
+
|
|
462
|
+
## 许可证
|
|
463
|
+
|
|
464
|
+
MIT
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
```markdown
|
|
2
|
+
# 三种缓存方式方法兼容性对比表格
|
|
3
|
+
|
|
4
|
+
## 核心缓存方法
|
|
5
|
+
|
|
6
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
7
|
+
|-------|-------|---------|-----------|----------|
|
|
8
|
+
| set | ✅ | ✅ | ✅ | 完全兼容 |
|
|
9
|
+
| get | ✅ | ✅ | ✅ | 完全兼容 |
|
|
10
|
+
| del | ✅ | ✅ | ✅ | 完全兼容 |
|
|
11
|
+
| has | ❌ | ✅ | ✅ | Redis无此方法,CacheBase保持兼容 |
|
|
12
|
+
| clear | ❌ | ✅ | ✅ | Redis无此方法,CacheBase保持兼容 |
|
|
13
|
+
| keys | ✅ | ✅ | ✅ | 完全兼容 |
|
|
14
|
+
| ttl | ✅ | ❌ | ✅ | Redis原生支持,CacheBase模拟实现 |
|
|
15
|
+
| add | ❌ | ✅ | ✅ | Redis无此方法,CacheBase保持兼容 |
|
|
16
|
+
| addInt | ❌ | ✅ | ✅ | Redis无此方法,CacheBase保持兼容 |
|
|
17
|
+
| addStr | ❌ | ✅ | ✅ | Redis无此方法,CacheBase保持兼容 |
|
|
18
|
+
| sort | ✅ | ❌ | ✅ | Redis原生支持,CacheBase模拟实现 |
|
|
19
|
+
|
|
20
|
+
## Redis兼容方法 - 哈希表操作
|
|
21
|
+
|
|
22
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
23
|
+
|-------|-------|---------|-----------|----------|
|
|
24
|
+
| hset | ✅ | ✅ | ✅ | 模拟实现,通过set对象方式 |
|
|
25
|
+
| hget | ✅ | ✅ | ✅ | 模拟实现,通过get对象方式 |
|
|
26
|
+
| hgetall | ✅ | ✅ | ✅ | 模拟实现,返回完整对象 |
|
|
27
|
+
| hdel | ✅ | ✅ | ✅ | 模拟实现,删除对象属性 |
|
|
28
|
+
| hexists | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
29
|
+
| hkeys | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
30
|
+
| hvals | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
31
|
+
|
|
32
|
+
## Redis兼容方法 - 集合操作
|
|
33
|
+
|
|
34
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
35
|
+
|-------|-------|---------|-----------|----------|
|
|
36
|
+
| sadd | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
37
|
+
| srem | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
38
|
+
| sismember | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
39
|
+
| smembers | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
40
|
+
| scard | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
41
|
+
|
|
42
|
+
## Redis兼容方法 - 有序集合操作
|
|
43
|
+
|
|
44
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
45
|
+
|-------|-------|---------|-----------|----------|
|
|
46
|
+
| zadd | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
47
|
+
| zrem | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
48
|
+
| zrange | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
49
|
+
| zscore | ✅ | ❌ | ❌ | Redis原生支持,CacheBase未实现 |
|
|
50
|
+
|
|
51
|
+
## Redis兼容方法 - 其他常用命令
|
|
52
|
+
|
|
53
|
+
| 方法名 | Redis | MongoDB | CacheBase | 状态说明 |
|
|
54
|
+
|-------|-------|---------|-----------|----------|
|
|
55
|
+
| expire | ✅ | ❌ | ✅ | Redis原生支持,CacheBase通过ttl模拟 |
|
|
56
|
+
| incr | ✅ | ⚠️ | ✅ | Redis原生支持,MongoDB返回undefined,CacheBase通过addInt模拟 |
|
|
57
|
+
| decr | ✅ | ⚠️ | ✅ | Redis原生支持,MongoDB返回undefined,CacheBase通过addInt模拟 |
|
|
58
|
+
| exists | ✅ | ✅ | ✅ | 兼容,Redis原生支持 |
|
|
59
|
+
| mget | ✅ | ⚠️ | ✅ | Redis原生支持,CacheBase实现兼容 |
|
|
60
|
+
| mset | ✅ | ✅ | ✅ | Redis原生支持,CacheBase实现兼容 |
|
|
61
|
+
|
|
62
|
+
注:✅ 完全兼容/功能正常 | ⚠️ 部分兼容/有异常 | ❌ 不兼容/未实现
|
|
63
|
+
```
|