mm_cache 1.4.1 → 1.4.2
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 +259 -259
- package/compatibility_table.md +2 -2
- package/index.js +7 -35
- package/package.json +9 -9
- package/test.js +8 -8
package/README.md
CHANGED
|
@@ -1,260 +1,260 @@
|
|
|
1
|
-
# mm_cache
|
|
2
|
-
|
|
3
|
-
## 项目信息
|
|
4
|
-
|
|
5
|
-
- **当前版本**: 1.4.
|
|
6
|
-
- **更新日期**:
|
|
7
|
-
- **仓库地址**: [https://gitee.com/qiuwenwu/mm_cache](https://gitee.com/qiuwenwu/mm_cache)
|
|
8
|
-
- **Node.js版本要求**: >=14.0.0,推荐16.0.0及以上
|
|
9
|
-
|
|
10
|
-
## 简介
|
|
11
|
-
|
|
12
|
-
mm_cache是一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容。本模块作为上层抽象,整合了多种缓存实现,让开发者可以使用统一的方式操作不同的缓存系统。
|
|
13
|
-
|
|
14
|
-
## 特点
|
|
15
|
-
|
|
16
|
-
- **统一API接口**:为Redis、MongoDB、CacheBase、Memory提供完全一致的接口
|
|
17
|
-
- **无缝切换**:支持在不同缓存实现之间快速切换,无需修改业务代码
|
|
18
|
-
- **完整的方法支持**:包括核心缓存方法、原子操作、字符串操作、批量操作、过期时间操作、键操作、哈希表操作、数组/列表操作等
|
|
19
|
-
- **高兼容性**:所有缓存方式支持相同的参数格式和返回值类型
|
|
20
|
-
- **灵活配置**:支持多种配置方式,适应不同的应用场景
|
|
21
|
-
- **一致性保证**:确保不同缓存实现的行为一致性,特别是在原子操作和过期时间处理方面
|
|
22
|
-
|
|
23
|
-
## 安装
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
npm install mm_cache
|
|
27
|
-
```
|
|
28
|
-
|
|
29
|
-
## 使用方法
|
|
30
|
-
|
|
31
|
-
### 基础用法
|
|
32
|
-
|
|
33
|
-
```javascript
|
|
34
|
-
const { Cache } = require('mm_cache');
|
|
35
|
-
|
|
36
|
-
// 初始化Redis缓存
|
|
37
|
-
const redisCache = new Cache({
|
|
38
|
-
|
|
39
|
-
redis: {
|
|
40
|
-
host: '127.0.0.1',
|
|
41
|
-
port: 6379,
|
|
42
|
-
password: 'your_password',
|
|
43
|
-
db: 0
|
|
44
|
-
}
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// 初始化MongoDB缓存
|
|
48
|
-
const mongodbCache = new Cache({
|
|
49
|
-
|
|
50
|
-
mongodb: {
|
|
51
|
-
url: 'mongodb://localhost:27017',
|
|
52
|
-
database: 'cache_db',
|
|
53
|
-
collection: 'cache_collection'
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
// 初始化CacheBase缓存
|
|
58
|
-
const cacheBaseCache = new Cache({
|
|
59
|
-
|
|
60
|
-
cachebase: {
|
|
61
|
-
enabled: true,
|
|
62
|
-
cache_dir: './cache'
|
|
63
|
-
}
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
// 初始化Memory缓存
|
|
67
|
-
const memoryCache = new Cache({
|
|
68
|
-
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
// 使用统一的API操作不同的缓存
|
|
72
|
-
async function testCache(cache) {
|
|
73
|
-
// 设置缓存
|
|
74
|
-
await cache.set('key', 'value');
|
|
75
|
-
|
|
76
|
-
// 获取缓存
|
|
77
|
-
const value = await cache.get('key');
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
// 删除缓存
|
|
81
|
-
await cache.del('key');
|
|
82
|
-
|
|
83
|
-
// 检查缓存是否存在
|
|
84
|
-
const exists = await cache.has('key');
|
|
85
|
-
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// 使用Redis缓存
|
|
89
|
-
await testCache(redisCache);
|
|
90
|
-
|
|
91
|
-
// 使用MongoDB缓存(无需修改代码)
|
|
92
|
-
await testCache(mongodbCache);
|
|
93
|
-
|
|
94
|
-
// 使用CacheBase缓存(无需修改代码)
|
|
95
|
-
await testCache(cacheBaseCache);
|
|
96
|
-
|
|
97
|
-
// 使用Memory缓存(无需修改代码)
|
|
98
|
-
await testCache(memoryCache);
|
|
99
|
-
```
|
|
100
|
-
|
|
101
|
-
### 高级用法
|
|
102
|
-
|
|
103
|
-
#### 设置过期时间
|
|
104
|
-
|
|
105
|
-
```javascript
|
|
106
|
-
// 设置缓存并设置过期时间为60秒
|
|
107
|
-
await cache.set('key', 'value', 60);
|
|
108
|
-
|
|
109
|
-
// 为已存在的键设置过期时间
|
|
110
|
-
await cache.expire('key', 60);
|
|
111
|
-
|
|
112
|
-
// 移除过期时间
|
|
113
|
-
await cache.persist('key');
|
|
114
|
-
|
|
115
|
-
// 获取剩余过期时间
|
|
116
|
-
const ttl = await cache.ttl('key');
|
|
117
|
-
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
#### 原子操作
|
|
121
|
-
|
|
122
|
-
```javascript
|
|
123
|
-
// 原子递增
|
|
124
|
-
const newValue = await cache.incr('counter');
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
// 增加指定整数值
|
|
128
|
-
const result = await cache.incrby('counter', 10);
|
|
129
|
-
|
|
130
|
-
// 增加指定浮点数值
|
|
131
|
-
const floatResult = await cache.incrbyfloat('counter', 0.5);
|
|
132
|
-
|
|
133
|
-
// 减少指定整数值
|
|
134
|
-
const decrResult = await cache.decrby('counter', 5);
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
#### 哈希表操作
|
|
138
|
-
|
|
139
|
-
```javascript
|
|
140
|
-
// 设置哈希表字段
|
|
141
|
-
await cache.hset('user:1', 'name', '张三');
|
|
142
|
-
await cache.hset('user:1', 'age', 25);
|
|
143
|
-
|
|
144
|
-
// 获取哈希表字段
|
|
145
|
-
const name = await cache.hget('user:1', 'name');
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
// 获取所有哈希表字段
|
|
149
|
-
const user = await cache.hgetall('user:1');
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
// 批量设置哈希表字段
|
|
153
|
-
await cache.hmset('user:2', {
|
|
154
|
-
name: '李四',
|
|
155
|
-
age: 30,
|
|
156
|
-
email: 'lisi@example.com'
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
// 批量获取哈希表字段
|
|
160
|
-
const values = await cache.hmget('user:2', ['name', 'age']);
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
// 删除哈希表字段
|
|
164
|
-
await cache.hdel('user:1', 'age');
|
|
165
|
-
|
|
166
|
-
// 获取哈希表字段数量
|
|
167
|
-
const count = await cache.hlen('user:1');
|
|
168
|
-
|
|
169
|
-
```
|
|
170
|
-
|
|
171
|
-
#### 数组/列表操作
|
|
172
|
-
|
|
173
|
-
```javascript
|
|
174
|
-
// 设置列表
|
|
175
|
-
await cache.setForList('users', ['张三', '李四', '王五']);
|
|
176
|
-
|
|
177
|
-
// 向列表添加元素
|
|
178
|
-
await cache.addForList('users', '赵六');
|
|
179
|
-
|
|
180
|
-
// 检查元素是否在列表中
|
|
181
|
-
const exists = await cache.hasForList('users', '李四');
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
// 获取列表元素(支持分页)
|
|
185
|
-
const users = await cache.getForList('users', 0, 2);
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
// 清空列表
|
|
189
|
-
await cache.clearForList('users');
|
|
190
|
-
```
|
|
191
|
-
|
|
192
|
-
## 方法兼容性
|
|
193
|
-
|
|
194
|
-
mm_cache确保所有支持的缓存方式(Redis、MongoDB、CacheBase、Memory)都实现了相同的API接口,具体兼容性情况请参考项目中的`compatibility_table.md`文件。
|
|
195
|
-
|
|
196
|
-
## 配置说明
|
|
197
|
-
|
|
198
|
-
### 全局配置选项
|
|
199
|
-
|
|
200
|
-
```javascript
|
|
201
|
-
const cache = new Cache({
|
|
202
|
-
// 缓存方式: redis, mongodb, cachebase, memory
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
// Redis配置
|
|
206
|
-
redis: {
|
|
207
|
-
host: '127.0.0.1',
|
|
208
|
-
port: 6379,
|
|
209
|
-
password: 'your_password',
|
|
210
|
-
db: 0
|
|
211
|
-
},
|
|
212
|
-
|
|
213
|
-
// MongoDB配置
|
|
214
|
-
mongodb: {
|
|
215
|
-
url: 'mongodb://localhost:27017',
|
|
216
|
-
database: 'cache_db',
|
|
217
|
-
collection: 'cache_collection'
|
|
218
|
-
},
|
|
219
|
-
|
|
220
|
-
// CacheBase配置
|
|
221
|
-
cachebase: {
|
|
222
|
-
enabled: true,
|
|
223
|
-
cache_dir: './cache',
|
|
224
|
-
file_ext: '.cache.json',
|
|
225
|
-
memory_limit: 5000,
|
|
226
|
-
save_interval: 5000,
|
|
227
|
-
file_mode: 384
|
|
228
|
-
}
|
|
229
|
-
});
|
|
230
|
-
```
|
|
231
|
-
|
|
232
|
-
## 贡献指南
|
|
233
|
-
|
|
234
|
-
1. **Fork项目**:在Gitee上Fork本项目到您的账户
|
|
235
|
-
2. **创建分支**:在您的仓库中创建功能分支
|
|
236
|
-
3. **提交修改**:提交您的代码修改和详细的提交信息
|
|
237
|
-
4. **运行测试**:确保所有测试通过
|
|
238
|
-
5. **发起PR**:向原项目提交Pull Request,描述您的修改内容和目的
|
|
239
|
-
|
|
240
|
-
## 问题反馈
|
|
241
|
-
|
|
242
|
-
如有任何问题或建议,请通过以下方式联系我们:
|
|
243
|
-
|
|
244
|
-
- **Gitee Issues**: [https://gitee.com/qiuwenwu/mm_cache/issues](https://gitee.com/qiuwenwu/mm_cache/issues)
|
|
245
|
-
- **NPM**: [https://www.npmjs.com/package/mm_cache](https://www.npmjs.com/package/mm_cache)
|
|
246
|
-
|
|
247
|
-
## 鸣谢
|
|
248
|
-
|
|
249
|
-
感谢以下项目和工具对本模块的支持:
|
|
250
|
-
|
|
251
|
-
- [Node.js](https://nodejs.org/)
|
|
252
|
-
- [Redis](https://redis.io/)
|
|
253
|
-
- [MongoDB](https://www.mongodb.com/)
|
|
254
|
-
- [mm_cachebase](https://www.npmjs.com/package/mm_cachebase)
|
|
255
|
-
- [mm_mongodb](https://www.npmjs.com/package/mm_mongodb)
|
|
256
|
-
- [mm_redis](https://www.npmjs.com/package/mm_redis)
|
|
257
|
-
|
|
258
|
-
## 许可证
|
|
259
|
-
|
|
1
|
+
# mm_cache
|
|
2
|
+
|
|
3
|
+
## 项目信息
|
|
4
|
+
|
|
5
|
+
- **当前版本**: 1.4.2
|
|
6
|
+
- **更新日期**: 2025-11-17
|
|
7
|
+
- **仓库地址**: [https://gitee.com/qiuwenwu/mm_cache](https://gitee.com/qiuwenwu/mm_cache)
|
|
8
|
+
- **Node.js版本要求**: >=14.0.0,推荐16.0.0及以上
|
|
9
|
+
|
|
10
|
+
## 简介
|
|
11
|
+
|
|
12
|
+
mm_cache是一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容。本模块作为上层抽象,整合了多种缓存实现,让开发者可以使用统一的方式操作不同的缓存系统。
|
|
13
|
+
|
|
14
|
+
## 特点
|
|
15
|
+
|
|
16
|
+
- **统一API接口**:为Redis、MongoDB、CacheBase、Memory提供完全一致的接口
|
|
17
|
+
- **无缝切换**:支持在不同缓存实现之间快速切换,无需修改业务代码
|
|
18
|
+
- **完整的方法支持**:包括核心缓存方法、原子操作、字符串操作、批量操作、过期时间操作、键操作、哈希表操作、数组/列表操作等
|
|
19
|
+
- **高兼容性**:所有缓存方式支持相同的参数格式和返回值类型
|
|
20
|
+
- **灵活配置**:支持多种配置方式,适应不同的应用场景
|
|
21
|
+
- **一致性保证**:确保不同缓存实现的行为一致性,特别是在原子操作和过期时间处理方面
|
|
22
|
+
|
|
23
|
+
## 安装
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
npm install mm_cache
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## 使用方法
|
|
30
|
+
|
|
31
|
+
### 基础用法
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
const { Cache } = require('mm_cache');
|
|
35
|
+
|
|
36
|
+
// 初始化Redis缓存
|
|
37
|
+
const redisCache = new Cache({
|
|
38
|
+
way: 'redis',
|
|
39
|
+
redis: {
|
|
40
|
+
host: '127.0.0.1',
|
|
41
|
+
port: 6379,
|
|
42
|
+
password: 'your_password',
|
|
43
|
+
db: 0
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// 初始化MongoDB缓存
|
|
48
|
+
const mongodbCache = new Cache({
|
|
49
|
+
way: 'mongodb',
|
|
50
|
+
mongodb: {
|
|
51
|
+
url: 'mongodb://localhost:27017',
|
|
52
|
+
database: 'cache_db',
|
|
53
|
+
collection: 'cache_collection'
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
// 初始化CacheBase缓存
|
|
58
|
+
const cacheBaseCache = new Cache({
|
|
59
|
+
way: 'cachebase',
|
|
60
|
+
cachebase: {
|
|
61
|
+
enabled: true,
|
|
62
|
+
cache_dir: './cache'
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// 初始化Memory缓存
|
|
67
|
+
const memoryCache = new Cache({
|
|
68
|
+
way: 'memory'
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// 使用统一的API操作不同的缓存
|
|
72
|
+
async function testCache(cache) {
|
|
73
|
+
// 设置缓存
|
|
74
|
+
await cache.set('key', 'value');
|
|
75
|
+
|
|
76
|
+
// 获取缓存
|
|
77
|
+
const value = await cache.get('key');
|
|
78
|
+
$.log.debug(value); // 'value'
|
|
79
|
+
|
|
80
|
+
// 删除缓存
|
|
81
|
+
await cache.del('key');
|
|
82
|
+
|
|
83
|
+
// 检查缓存是否存在
|
|
84
|
+
const exists = await cache.has('key');
|
|
85
|
+
$.log.debug(exists); // false
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 使用Redis缓存
|
|
89
|
+
await testCache(redisCache);
|
|
90
|
+
|
|
91
|
+
// 使用MongoDB缓存(无需修改代码)
|
|
92
|
+
await testCache(mongodbCache);
|
|
93
|
+
|
|
94
|
+
// 使用CacheBase缓存(无需修改代码)
|
|
95
|
+
await testCache(cacheBaseCache);
|
|
96
|
+
|
|
97
|
+
// 使用Memory缓存(无需修改代码)
|
|
98
|
+
await testCache(memoryCache);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### 高级用法
|
|
102
|
+
|
|
103
|
+
#### 设置过期时间
|
|
104
|
+
|
|
105
|
+
```javascript
|
|
106
|
+
// 设置缓存并设置过期时间为60秒
|
|
107
|
+
await cache.set('key', 'value', 60);
|
|
108
|
+
|
|
109
|
+
// 为已存在的键设置过期时间
|
|
110
|
+
await cache.expire('key', 60);
|
|
111
|
+
|
|
112
|
+
// 移除过期时间
|
|
113
|
+
await cache.persist('key');
|
|
114
|
+
|
|
115
|
+
// 获取剩余过期时间
|
|
116
|
+
const ttl = await cache.ttl('key');
|
|
117
|
+
$.log.debug(ttl); // 剩余过期时间(秒),-1表示无过期时间,-2表示键不存在
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
#### 原子操作
|
|
121
|
+
|
|
122
|
+
```javascript
|
|
123
|
+
// 原子递增
|
|
124
|
+
const newValue = await cache.incr('counter');
|
|
125
|
+
$.log.debug(newValue); // 1, 2, 3...
|
|
126
|
+
|
|
127
|
+
// 增加指定整数值
|
|
128
|
+
const result = await cache.incrby('counter', 10);
|
|
129
|
+
|
|
130
|
+
// 增加指定浮点数值
|
|
131
|
+
const floatResult = await cache.incrbyfloat('counter', 0.5);
|
|
132
|
+
|
|
133
|
+
// 减少指定整数值
|
|
134
|
+
const decrResult = await cache.decrby('counter', 5);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
#### 哈希表操作
|
|
138
|
+
|
|
139
|
+
```javascript
|
|
140
|
+
// 设置哈希表字段
|
|
141
|
+
await cache.hset('user:1', 'name', '张三');
|
|
142
|
+
await cache.hset('user:1', 'age', 25);
|
|
143
|
+
|
|
144
|
+
// 获取哈希表字段
|
|
145
|
+
const name = await cache.hget('user:1', 'name');
|
|
146
|
+
$.log.debug(name); // '张三'
|
|
147
|
+
|
|
148
|
+
// 获取所有哈希表字段
|
|
149
|
+
const user = await cache.hgetall('user:1');
|
|
150
|
+
$.log.debug(user); // { name: '张三', age: '25' }
|
|
151
|
+
|
|
152
|
+
// 批量设置哈希表字段
|
|
153
|
+
await cache.hmset('user:2', {
|
|
154
|
+
name: '李四',
|
|
155
|
+
age: 30,
|
|
156
|
+
email: 'lisi@example.com'
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// 批量获取哈希表字段
|
|
160
|
+
const values = await cache.hmget('user:2', ['name', 'age']);
|
|
161
|
+
$.log.debug(values); // ['李四', '30']
|
|
162
|
+
|
|
163
|
+
// 删除哈希表字段
|
|
164
|
+
await cache.hdel('user:1', 'age');
|
|
165
|
+
|
|
166
|
+
// 获取哈希表字段数量
|
|
167
|
+
const count = await cache.hlen('user:1');
|
|
168
|
+
$.log.debug(count); // 1
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
#### 数组/列表操作
|
|
172
|
+
|
|
173
|
+
```javascript
|
|
174
|
+
// 设置列表
|
|
175
|
+
await cache.setForList('users', ['张三', '李四', '王五']);
|
|
176
|
+
|
|
177
|
+
// 向列表添加元素
|
|
178
|
+
await cache.addForList('users', '赵六');
|
|
179
|
+
|
|
180
|
+
// 检查元素是否在列表中
|
|
181
|
+
const exists = await cache.hasForList('users', '李四');
|
|
182
|
+
$.log.debug(exists); // true
|
|
183
|
+
|
|
184
|
+
// 获取列表元素(支持分页)
|
|
185
|
+
const users = await cache.getForList('users', 0, 2);
|
|
186
|
+
$.log.debug(users); // ['张三', '李四', '王五']
|
|
187
|
+
|
|
188
|
+
// 清空列表
|
|
189
|
+
await cache.clearForList('users');
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
## 方法兼容性
|
|
193
|
+
|
|
194
|
+
mm_cache确保所有支持的缓存方式(Redis、MongoDB、CacheBase、Memory)都实现了相同的API接口,具体兼容性情况请参考项目中的`compatibility_table.md`文件。
|
|
195
|
+
|
|
196
|
+
## 配置说明
|
|
197
|
+
|
|
198
|
+
### 全局配置选项
|
|
199
|
+
|
|
200
|
+
```javascript
|
|
201
|
+
const cache = new Cache({
|
|
202
|
+
// 缓存方式: redis, mongodb, cachebase, memory
|
|
203
|
+
way: 'redis',
|
|
204
|
+
|
|
205
|
+
// Redis配置
|
|
206
|
+
redis: {
|
|
207
|
+
host: '127.0.0.1',
|
|
208
|
+
port: 6379,
|
|
209
|
+
password: 'your_password',
|
|
210
|
+
db: 0
|
|
211
|
+
},
|
|
212
|
+
|
|
213
|
+
// MongoDB配置
|
|
214
|
+
mongodb: {
|
|
215
|
+
url: 'mongodb://localhost:27017',
|
|
216
|
+
database: 'cache_db',
|
|
217
|
+
collection: 'cache_collection'
|
|
218
|
+
},
|
|
219
|
+
|
|
220
|
+
// CacheBase配置
|
|
221
|
+
cachebase: {
|
|
222
|
+
enabled: true,
|
|
223
|
+
cache_dir: './cache',
|
|
224
|
+
file_ext: '.cache.json',
|
|
225
|
+
memory_limit: 5000,
|
|
226
|
+
save_interval: 5000,
|
|
227
|
+
file_mode: 384
|
|
228
|
+
}
|
|
229
|
+
});
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
## 贡献指南
|
|
233
|
+
|
|
234
|
+
1. **Fork项目**:在Gitee上Fork本项目到您的账户
|
|
235
|
+
2. **创建分支**:在您的仓库中创建功能分支
|
|
236
|
+
3. **提交修改**:提交您的代码修改和详细的提交信息
|
|
237
|
+
4. **运行测试**:确保所有测试通过
|
|
238
|
+
5. **发起PR**:向原项目提交Pull Request,描述您的修改内容和目的
|
|
239
|
+
|
|
240
|
+
## 问题反馈
|
|
241
|
+
|
|
242
|
+
如有任何问题或建议,请通过以下方式联系我们:
|
|
243
|
+
|
|
244
|
+
- **Gitee Issues**: [https://gitee.com/qiuwenwu/mm_cache/issues](https://gitee.com/qiuwenwu/mm_cache/issues)
|
|
245
|
+
- **NPM**: [https://www.npmjs.com/package/mm_cache](https://www.npmjs.com/package/mm_cache)
|
|
246
|
+
|
|
247
|
+
## 鸣谢
|
|
248
|
+
|
|
249
|
+
感谢以下项目和工具对本模块的支持:
|
|
250
|
+
|
|
251
|
+
- [Node.js](https://nodejs.org/)
|
|
252
|
+
- [Redis](https://redis.io/)
|
|
253
|
+
- [MongoDB](https://www.mongodb.com/)
|
|
254
|
+
- [mm_cachebase](https://www.npmjs.com/package/mm_cachebase)
|
|
255
|
+
- [mm_mongodb](https://www.npmjs.com/package/mm_mongodb)
|
|
256
|
+
- [mm_redis](https://www.npmjs.com/package/mm_redis)
|
|
257
|
+
|
|
258
|
+
## 许可证
|
|
259
|
+
|
|
260
260
|
本项目使用ISC许可证 - 详情请查看 [LICENSE](LICENSE) 文件
|
package/compatibility_table.md
CHANGED
|
@@ -42,8 +42,8 @@
|
|
|
42
42
|
|
|
43
43
|
| 方法名 | Redis | MongoDB | CacheBase | Memory | 状态说明 |
|
|
44
44
|
|-------|-------|---------|-----------|--------|----------|
|
|
45
|
-
| expire | ✅ | ✅ |
|
|
46
|
-
| persist | ✅ | ✅ |
|
|
45
|
+
| expire | ✅ | ✅ | ✅ | ✅ | 所有方式功能实现效果相同,参数类型和返回值一致 |
|
|
46
|
+
| persist | ✅ | ✅ | ✅ | ✅ | 所有方式均正常工作 |
|
|
47
47
|
|
|
48
48
|
## 键操作方法
|
|
49
49
|
|
package/index.js
CHANGED
|
@@ -258,7 +258,7 @@ Cache.prototype._safeCall = async function(method, args = []) {
|
|
|
258
258
|
return await this.db[method](...args);
|
|
259
259
|
}
|
|
260
260
|
|
|
261
|
-
//
|
|
261
|
+
// 降级处理:仅为确实不存在的方法提供内置实现
|
|
262
262
|
switch (method) {
|
|
263
263
|
case 'strlen': {
|
|
264
264
|
const key = args[0];
|
|
@@ -306,39 +306,6 @@ Cache.prototype._safeCall = async function(method, args = []) {
|
|
|
306
306
|
}
|
|
307
307
|
return 'string';
|
|
308
308
|
}
|
|
309
|
-
case 'incrby': {
|
|
310
|
-
const [key, value] = args;
|
|
311
|
-
// 降级为addInt方法
|
|
312
|
-
return await this.db.addInt(key, value);
|
|
313
|
-
}
|
|
314
|
-
case 'decrby': {
|
|
315
|
-
const [key, value] = args;
|
|
316
|
-
// 降级为addInt方法,使用负值
|
|
317
|
-
return await this.db.addInt(key, -value);
|
|
318
|
-
}
|
|
319
|
-
case 'incrbyfloat': {
|
|
320
|
-
const [key, value] = args;
|
|
321
|
-
// 降级为addFloat方法
|
|
322
|
-
return await this.db.addFloat(key, value);
|
|
323
|
-
}
|
|
324
|
-
case 'mget': {
|
|
325
|
-
// 直接使用参数数组,不做额外处理
|
|
326
|
-
try {
|
|
327
|
-
// 并行获取多个键的值
|
|
328
|
-
const promises = args.map(async (key) => {
|
|
329
|
-
try {
|
|
330
|
-
return await this.db.get(key);
|
|
331
|
-
} catch (error) {
|
|
332
|
-
$.log.error(`[Cache] [_safeCall] mget获取键[${key}]失败:`, error);
|
|
333
|
-
return null;
|
|
334
|
-
}
|
|
335
|
-
});
|
|
336
|
-
return await Promise.all(promises);
|
|
337
|
-
} catch (error) {
|
|
338
|
-
$.log.error(`[Cache] [_safeCall] mget操作失败:`, error);
|
|
339
|
-
return [];
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
309
|
default:
|
|
343
310
|
// 如果方法不存在且没有降级实现,返回null
|
|
344
311
|
$.log.warn(`[Cache] [_safeCall] 不支持的方法: ${method}`);
|
|
@@ -427,7 +394,12 @@ Cache.prototype.expire = async function(key, seconds) {
|
|
|
427
394
|
Cache.prototype.ttl = async function(key, seconds) {
|
|
428
395
|
// 添加作用域前缀
|
|
429
396
|
const scopeKey = `${this.scope}:${key}`;
|
|
430
|
-
|
|
397
|
+
// 仅在seconds参数存在时才传递给底层方法
|
|
398
|
+
if (seconds !== undefined) {
|
|
399
|
+
return await this._safeCall('ttl', [scopeKey, seconds]);
|
|
400
|
+
} else {
|
|
401
|
+
return await this._safeCall('ttl', [scopeKey]);
|
|
402
|
+
}
|
|
431
403
|
};
|
|
432
404
|
|
|
433
405
|
/**
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "mm_cache",
|
|
3
|
-
"version": "1.4.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.4.2",
|
|
4
|
+
"description": "一个统一的缓存操作框架,为多种缓存方式(Redis、MongoDB、CacheBase、Memory)提供统一的API接口,实现无缝切换和兼容",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"test": "node test.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://
|
|
11
|
+
"url": "git+https://gitee.com/qiuwenwu/mm_cache.git"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"cache",
|
|
@@ -26,12 +26,12 @@
|
|
|
26
26
|
"author": "邱文武",
|
|
27
27
|
"license": "ISC",
|
|
28
28
|
"bugs": {
|
|
29
|
-
"url": "https://
|
|
29
|
+
"url": "https://gitee.com/qiuwenwu/mm_cache/issues"
|
|
30
30
|
},
|
|
31
|
-
"homepage": "https://
|
|
31
|
+
"homepage": "https://gitee.com/qiuwenwu/mm_cache#readme",
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"mm_cachebase": "^1.8.
|
|
34
|
-
"mm_mongodb": "^1.
|
|
35
|
-
"mm_redis": "^
|
|
33
|
+
"mm_cachebase": "^1.8.9",
|
|
34
|
+
"mm_mongodb": "^1.5.1",
|
|
35
|
+
"mm_redis": "^2.0.3"
|
|
36
36
|
}
|
|
37
|
-
}
|
|
37
|
+
}
|
package/test.js
CHANGED
|
@@ -87,8 +87,8 @@ async function recordTestResult(cache, methodName, testFunction) {
|
|
|
87
87
|
// 对关键方法失败时输出详细信息
|
|
88
88
|
const criticalMethods = ['hdel', 'hmset', 'hmget', 'hlen', 'addForList', 'expire'];
|
|
89
89
|
if (criticalMethods.includes(methodName)) {
|
|
90
|
-
|
|
91
|
-
|
|
90
|
+
$.log.error(`[Cache] ${cacheWay}.${methodName} 测试失败: ${error.message}`);
|
|
91
|
+
$.log.error(error.stack);
|
|
92
92
|
}
|
|
93
93
|
|
|
94
94
|
// 检查是否是"不支持的方法"错误
|
|
@@ -448,19 +448,19 @@ function getUnsupportedReason(method) {
|
|
|
448
448
|
|
|
449
449
|
// 测试单个缓存方式的所有方法
|
|
450
450
|
async function testCacheWay(cacheWay) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
451
|
+
$.log.debug(`\n[Cache] ==============================`);
|
|
452
|
+
$.log.debug(`[Cache] 开始测试缓存方式: ${cacheWay}`);
|
|
453
|
+
$.log.debug(`[Cache] ==============================`);
|
|
454
454
|
|
|
455
455
|
// 直接使用指定的缓存方式创建Cache实例
|
|
456
456
|
const cache = new Cache(`test_${cacheWay}`, { way: cacheWay });
|
|
457
457
|
const connected = await cache.connect();
|
|
458
458
|
|
|
459
459
|
if (connected) {
|
|
460
|
-
|
|
461
|
-
|
|
460
|
+
$.log.debug(`[Cache] 成功连接到缓存方式: ${cacheWay}`);
|
|
461
|
+
$.log.debug(`[Cache] 当前实际缓存方式: ${cache.getCacheWay()}`);
|
|
462
462
|
} else {
|
|
463
|
-
|
|
463
|
+
$.log.debug(`[Cache] 无法连接到缓存方式: ${cacheWay}`);
|
|
464
464
|
return false;
|
|
465
465
|
}
|
|
466
466
|
|