mm_cachebase 1.6.0 → 1.7.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 CHANGED
@@ -9,6 +9,7 @@
9
9
  - **完整的事件系统**:支持前置/后置/错误事件,便于日志记录和访问控制
10
10
  - **过期时间管理**:支持设置和查询键的过期时间
11
11
  - **丰富的数据类型支持**:支持字符串、哈希表等Redis常见数据类型
12
+ - **多文件持久化**:支持数据持久化到文件,通过scope参数实现多实例数据隔离
12
13
 
13
14
  ## 安装
14
15
 
@@ -43,11 +44,19 @@ async function example() {
43
44
  ### 构造函数
44
45
 
45
46
  ```javascript
46
- const cache = new CacheBase();
47
+ const cache = new CacheBase(scope, config);
47
48
  ```
48
49
 
49
50
  创建一个新的缓存实例。缓存实例基于EventEmitter,支持完整的事件系统。
50
51
 
52
+ - `scope`: 缓存作用域名称(默认'sys'),用于多文件持久化时的数据隔离
53
+ - `config`: 配置选项对象,支持以下属性:
54
+ - `dir`: 缓存文件存储目录(默认'./cache/')
55
+ - `expire`: 默认过期时间(秒)(默认0,表示永不过期)
56
+ - `max`: 最大缓存键数量(默认0,表示无限制)
57
+ - `persist`: 是否启用持久化(默认false)
58
+ - `saveInterval`: 定时保存间隔(毫秒)(默认60000)
59
+
51
60
  ### 基本操作
52
61
 
53
62
  #### 设置缓存
@@ -232,6 +241,22 @@ await cache.dispose()
232
241
  ```
233
242
  - 清理所有资源,移除所有监听器
234
243
 
244
+ ### 持久化操作
245
+
246
+ #### 连接并启动定时保存
247
+ ```javascript
248
+ await cache.connect()
249
+ ```
250
+ - 启用持久化时启动定时保存功能
251
+ - 返回: 连接成功返回true,失败返回false
252
+
253
+ #### 手动保存数据
254
+ ```javascript
255
+ await cache.save()
256
+ ```
257
+ - 手动将缓存数据保存到文件
258
+ - 返回: 保存成功返回true,失败返回false
259
+
235
260
  ### 兼容方法(保持向后兼容)
236
261
 
237
262
  #### 添加缓存(仅当键不存在时)
@@ -417,6 +442,49 @@ async function numberExample() {
417
442
  }
418
443
  ```
419
444
 
445
+ ### 多文件持久化示例
446
+ ```javascript
447
+ const CacheBase = require('mm_cachebase');
448
+
449
+ async function persistenceExample() {
450
+ // 创建启用持久化的缓存实例,指定不同的scope
451
+ const userCache = new CacheBase('users', {
452
+ persist: true,
453
+ saveInterval: 30000, // 30秒保存一次
454
+ dir: './test_cache/' // 自定义缓存目录
455
+ });
456
+
457
+ const productCache = new CacheBase('products', {
458
+ persist: true,
459
+ saveInterval: 30000,
460
+ dir: './test_cache/'
461
+ });
462
+
463
+ // 启动定时保存
464
+ await userCache.connect();
465
+ await productCache.connect();
466
+
467
+ // 向不同scope添加数据
468
+ await userCache.set('user1', { name: '张三', age: 30 }, 3600);
469
+ await productCache.set('product1', { id: 1, name: '商品A', price: 99.99 }, 3600);
470
+
471
+ // 数据会分别保存到 users.cache.json 和 products.cache.json
472
+ console.log('数据已保存,不同scope的数据完全隔离');
473
+
474
+ // 手动保存(可选)
475
+ await userCache.save();
476
+
477
+ // 应用重启后,可通过相同的scope加载对应的数据
478
+ const loadedUserCache = new CacheBase('users', { persist: true, dir: './test_cache/' });
479
+ const loadedUser = await loadedUserCache.get('user1');
480
+ console.log('加载的用户数据:', loadedUser);
481
+
482
+ // 销毁实例时,会自动保存数据
483
+ await userCache.dispose();
484
+ await productCache.dispose();
485
+ }
486
+ ```
487
+
420
488
  ### 批量操作示例
421
489
  ```javascript
422
490
  async function batchExample() {
@@ -444,9 +512,11 @@ async function batchExample() {
444
512
 
445
513
  1. 所有操作方法都是异步的,需要使用async/await或Promise处理
446
514
  2. 过期时间单位为秒,-1表示永不过期
447
- 3. 缓存数据存储在内存中,重启后数据会丢失
448
- 4. 事件回调函数中的 `cancel` 标志只在前置事件中有效,后置事件中修改不会产生效果
449
- 5. 所有事件参数都遵循统一的格式,包含key、value、result、error和cancel等字段
515
+ 3. 缓存数据默认存储在内存中,重启后数据会丢失
516
+ 4. 启用持久化时,数据将保存到`{scope}.cache.json`文件,不同scope的数据存储在不同文件中,实现数据隔离
517
+ 5. 缓存目录会在第一次使用时自动创建,无需手动创建
518
+ 6. 事件回调函数中的 `cancel` 标志只在前置事件中有效,后置事件中修改不会产生效果
519
+ 7. 所有事件参数都遵循统一的格式,包含key、value、result、error和cancel等字段
450
520
 
451
521
  ## 许可证
452
522