koatty_cacheable 2.0.0 → 2.0.1

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/CHANGELOG.md CHANGED
@@ -2,13 +2,9 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
- ## [2.0.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.1...v2.0.0) (2025-06-22)
5
+ ### [2.0.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.1...v2.0.1) (2025-11-02)
6
6
 
7
-
8
- ### Features
9
-
10
- * enhance cache decorators to support both global and decorator-specific options ([1e70a64](https://github.com/thinkkoa/koatty_cacheable/commit/1e70a64b099e44c8f89180d501899c7e90d83264))
11
- * implement CacheAble and CacheEvict decorators with unified injection system ([621c02c](https://github.com/thinkkoa/koatty_cacheable/commit/621c02c89596ccca61619ccd603e1868b38b6764))
7
+ ## [2.0.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.1...v2.0.0) (2025-11-02)
12
8
 
13
9
  ### [1.6.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.0...v1.6.1) (2025-06-09)
14
10
 
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # koatty_cacheable
2
2
 
3
- Cacheable plugin for Koatty.
3
+ Cacheable for koatty.
4
4
 
5
- Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别的缓存功能。
5
+ Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级别的缓存功能。
6
6
 
7
7
  ## 特性
8
8
 
@@ -13,7 +13,6 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别
13
13
  - 🔧 **多后端支持**: 支持 Memory 和 Redis 缓存后端
14
14
  - 🎯 **参数化缓存**: 支持基于方法参数的缓存键生成
15
15
  - 🛡️ **类型安全**: 完整的 TypeScript 支持
16
- - 📦 **插件化设计**: 遵循 Koatty 插件标准,统一管理
17
16
 
18
17
  ## 安装
19
18
 
@@ -21,68 +20,74 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别
21
20
  npm install koatty_cacheable
22
21
  ```
23
22
 
24
- ## 配置
23
+ ## 快速开始
25
24
 
26
- ### 1. Generate Plugin Template
25
+ ### 1. 生成插件模板
27
26
 
28
- Use Koatty CLI to generate the plugin template:
27
+ 使用 Koatty CLI 生成插件模板:
29
28
 
30
29
  ```bash
31
30
  kt plugin Cacheable
32
31
  ```
33
32
 
34
- Create `src/plugin/Cacheable.ts`:
33
+ 创建 `src/plugin/Cacheable.ts`:
35
34
 
36
35
  ```typescript
37
36
  import { Plugin, IPlugin, App } from "koatty";
38
- import { KoattyCache } from "koatty_cacheable";
37
+ import { KoattyCached } from "koatty_cacheable";
39
38
 
40
39
  @Plugin()
41
40
  export class Cacheable implements IPlugin {
42
41
  run(options: any, app: App) {
43
- return KoattyCache(options, app);
42
+ return KoattyCached(options, app);
44
43
  }
45
44
  }
46
45
  ```
47
46
 
48
- ### 2. Configure Plugin
47
+ ### 2. 配置插件
49
48
 
50
- Update `src/config/plugin.ts`:
49
+ 更新 `src/config/plugin.ts`:
51
50
 
52
51
  ```typescript
53
52
  export default {
54
- list: ["Cacheable"], // Plugin loading order
53
+ list: ["Cacheable"], // 插件加载顺序
55
54
  config: {
56
55
  Cacheable: {
57
- cacheTimeout: 300, // 默认缓存过期时间(秒)
58
- delayedDoubleDeletion: true, // 默认启用延迟双删策略
59
- redisConfig: {
60
- host: "127.0.0.1",
61
- port: 6379,
62
- password: "",
63
- db: 0,
64
- keyPrefix: "koatty:cache:"
65
- }
56
+ type: "memory", // 缓存类型: "redis" 或 "memory",默认为 "memory"
57
+ db: 0,
58
+ timeout: 30,
59
+ // Redis 配置 (当 type 为 "redis" 时)
60
+ // key_prefix: "koatty",
61
+ // host: '127.0.0.1',
62
+ // port: 6379,
63
+ // name: "",
64
+ // username: "",
65
+ // password: "",
66
+ // pool_size: 10,
67
+ // conn_timeout: 30
66
68
  }
67
69
  }
68
70
  };
69
71
  ```
70
72
 
73
+ **注意事项**:
74
+ - 插件会在应用启动时自动初始化缓存
75
+ - 必须在插件配置中提供正确的缓存配置
76
+ - 如果缓存未正确初始化,装饰器方法会直接执行而不进行缓存(优雅降级)
77
+
71
78
  ## 使用方法
72
79
 
73
80
  ### 基本用法
74
81
 
75
82
  ```typescript
76
83
  import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
77
- import { Component } from "koatty_container";
78
84
 
79
- @Component()
80
85
  export class UserService {
81
86
 
82
87
  // 自动缓存方法返回值
83
88
  @CacheAble("userCache", {
84
89
  params: ["id"], // 使用 id 参数作为缓存键的一部分
85
- timeout: 300 // 缓存过期时间(秒),默认使用插件配置的 cacheTimeout
90
+ timeout: 300 // 缓存过期时间(秒),默认 300 秒
86
91
  })
87
92
  async getUserById(id: string): Promise<User> {
88
93
  // 数据库查询逻辑
@@ -92,7 +97,7 @@ export class UserService {
92
97
  // 自动清除相关缓存
93
98
  @CacheEvict("userCache", {
94
99
  params: ["id"], // 使用 id 参数定位要清除的缓存
95
- delayedDoubleDeletion: true // 启用延迟双删策略,默认使用插件配置的 delayedDoubleDeletion
100
+ delayedDoubleDeletion: true // 启用延迟双删策略,默认 true
96
101
  })
97
102
  async updateUser(id: string, userData: Partial<User>): Promise<User> {
98
103
  // 更新用户数据
@@ -119,9 +124,6 @@ export class UserService {
119
124
  ### 高级用法
120
125
 
121
126
  ```typescript
122
- import { Component } from "koatty_container";
123
-
124
- @Component()
125
127
  export class ProductService {
126
128
 
127
129
  // 无参数缓存
@@ -133,7 +135,7 @@ export class ProductService {
133
135
  // 多参数缓存
134
136
  @CacheAble("productSearch", {
135
137
  params: ["category", "keyword"],
136
- timeout: 600 // 覆盖插件配置的默认时间
138
+ timeout: 600
137
139
  })
138
140
  async searchProducts(category: string, keyword: string, page: number = 1): Promise<Product[]> {
139
141
  return await this.productRepository.search(category, keyword, page);
@@ -142,7 +144,7 @@ export class ProductService {
142
144
  // 立即清除缓存(不使用延迟双删)
143
145
  @CacheEvict("productSearch", {
144
146
  params: ["category"],
145
- delayedDoubleDeletion: false // 覆盖插件配置的默认策略
147
+ delayedDoubleDeletion: false
146
148
  })
147
149
  async updateProductCategory(category: string, updates: any): Promise<void> {
148
150
  await this.productRepository.updateCategory(category, updates);
@@ -202,33 +204,14 @@ export class ProductService {
202
204
 
203
205
  这样可以避免在并发场景下出现脏数据。
204
206
 
205
- ## 配置优先级
206
-
207
- 配置项的优先级从高到低:
208
-
209
- 1. **装饰器配置**: 直接在 `@CacheAble` 或 `@CacheEvict` 中指定的选项
210
- 2. **插件配置**: 在 `src/config/plugin.ts` 中配置的 `Cacheable` 插件选项
211
- 3. **默认值**: 系统内置的默认配置
212
-
213
- 例如:
214
- ```typescript
215
- // 插件配置
216
- Cacheable: {
217
- cacheTimeout: 300,
218
- delayedDoubleDeletion: true
219
- }
220
-
221
- // 装饰器配置会覆盖插件配置
222
- @CacheAble("user", {
223
- timeout: 600 // 使用 600 秒而不是插件配置的 300 秒
224
- })
225
- ```
226
-
227
207
  ## 注意事项
228
208
 
229
- 1. 缓存的数据会自动进行 JSON 序列化/反序列化
230
- 2. 如果缓存服务不可用,方法会正常执行,不会抛出错误
231
- 3. 插件会在应用启动时自动注入缓存功能到所有使用装饰器的方法
209
+ 1. **初始化顺序**: 必须先调用 `KoattyCached()` 初始化缓存,然后再使用装饰器。建议在应用启动时(如 `init()` 方法中)进行初始化
210
+ 2. 装饰器只能用于 `SERVICE` 和 `COMPONENT` 类型的类
211
+ 3. 被装饰的方法必须是异步方法(返回 Promise)
212
+ 4. 缓存的数据会自动进行 JSON 序列化/反序列化
213
+ 5. 如果缓存服务不可用,方法会正常执行,不会抛出错误(优雅降级)
214
+ 6. 缓存键长度超过 128 字符时会自动使用 murmur hash 进行压缩
232
215
 
233
216
  ## 许可证
234
217
 
package/dist/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # koatty_cacheable
2
2
 
3
- Cacheable plugin for Koatty.
3
+ Cacheable for koatty.
4
4
 
5
- Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别的缓存功能。
5
+ Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级别的缓存功能。
6
6
 
7
7
  ## 特性
8
8
 
@@ -13,7 +13,6 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别
13
13
  - 🔧 **多后端支持**: 支持 Memory 和 Redis 缓存后端
14
14
  - 🎯 **参数化缓存**: 支持基于方法参数的缓存键生成
15
15
  - 🛡️ **类型安全**: 完整的 TypeScript 支持
16
- - 📦 **插件化设计**: 遵循 Koatty 插件标准,统一管理
17
16
 
18
17
  ## 安装
19
18
 
@@ -21,68 +20,74 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别
21
20
  npm install koatty_cacheable
22
21
  ```
23
22
 
24
- ## 配置
23
+ ## 快速开始
25
24
 
26
- ### 1. Generate Plugin Template
25
+ ### 1. 生成插件模板
27
26
 
28
- Use Koatty CLI to generate the plugin template:
27
+ 使用 Koatty CLI 生成插件模板:
29
28
 
30
29
  ```bash
31
30
  kt plugin Cacheable
32
31
  ```
33
32
 
34
- Create `src/plugin/Cacheable.ts`:
33
+ 创建 `src/plugin/Cacheable.ts`:
35
34
 
36
35
  ```typescript
37
36
  import { Plugin, IPlugin, App } from "koatty";
38
- import { KoattyCache } from "koatty_cacheable";
37
+ import { KoattyCached } from "koatty_cacheable";
39
38
 
40
39
  @Plugin()
41
40
  export class Cacheable implements IPlugin {
42
41
  run(options: any, app: App) {
43
- return KoattyCache(options, app);
42
+ return KoattyCached(options, app);
44
43
  }
45
44
  }
46
45
  ```
47
46
 
48
- ### 2. Configure Plugin
47
+ ### 2. 配置插件
49
48
 
50
- Update `src/config/plugin.ts`:
49
+ 更新 `src/config/plugin.ts`:
51
50
 
52
51
  ```typescript
53
52
  export default {
54
- list: ["Cacheable"], // Plugin loading order
53
+ list: ["Cacheable"], // 插件加载顺序
55
54
  config: {
56
55
  Cacheable: {
57
- cacheTimeout: 300, // 默认缓存过期时间(秒)
58
- delayedDoubleDeletion: true, // 默认启用延迟双删策略
59
- redisConfig: {
60
- host: "127.0.0.1",
61
- port: 6379,
62
- password: "",
63
- db: 0,
64
- keyPrefix: "koatty:cache:"
65
- }
56
+ type: "memory", // 缓存类型: "redis" 或 "memory",默认为 "memory"
57
+ db: 0,
58
+ timeout: 30,
59
+ // Redis 配置 (当 type 为 "redis" 时)
60
+ // key_prefix: "koatty",
61
+ // host: '127.0.0.1',
62
+ // port: 6379,
63
+ // name: "",
64
+ // username: "",
65
+ // password: "",
66
+ // pool_size: 10,
67
+ // conn_timeout: 30
66
68
  }
67
69
  }
68
70
  };
69
71
  ```
70
72
 
73
+ **注意事项**:
74
+ - 插件会在应用启动时自动初始化缓存
75
+ - 必须在插件配置中提供正确的缓存配置
76
+ - 如果缓存未正确初始化,装饰器方法会直接执行而不进行缓存(优雅降级)
77
+
71
78
  ## 使用方法
72
79
 
73
80
  ### 基本用法
74
81
 
75
82
  ```typescript
76
83
  import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
77
- import { Component } from "koatty_container";
78
84
 
79
- @Component()
80
85
  export class UserService {
81
86
 
82
87
  // 自动缓存方法返回值
83
88
  @CacheAble("userCache", {
84
89
  params: ["id"], // 使用 id 参数作为缓存键的一部分
85
- timeout: 300 // 缓存过期时间(秒),默认使用插件配置的 cacheTimeout
90
+ timeout: 300 // 缓存过期时间(秒),默认 300 秒
86
91
  })
87
92
  async getUserById(id: string): Promise<User> {
88
93
  // 数据库查询逻辑
@@ -92,7 +97,7 @@ export class UserService {
92
97
  // 自动清除相关缓存
93
98
  @CacheEvict("userCache", {
94
99
  params: ["id"], // 使用 id 参数定位要清除的缓存
95
- delayedDoubleDeletion: true // 启用延迟双删策略,默认使用插件配置的 delayedDoubleDeletion
100
+ delayedDoubleDeletion: true // 启用延迟双删策略,默认 true
96
101
  })
97
102
  async updateUser(id: string, userData: Partial<User>): Promise<User> {
98
103
  // 更新用户数据
@@ -119,9 +124,6 @@ export class UserService {
119
124
  ### 高级用法
120
125
 
121
126
  ```typescript
122
- import { Component } from "koatty_container";
123
-
124
- @Component()
125
127
  export class ProductService {
126
128
 
127
129
  // 无参数缓存
@@ -133,7 +135,7 @@ export class ProductService {
133
135
  // 多参数缓存
134
136
  @CacheAble("productSearch", {
135
137
  params: ["category", "keyword"],
136
- timeout: 600 // 覆盖插件配置的默认时间
138
+ timeout: 600
137
139
  })
138
140
  async searchProducts(category: string, keyword: string, page: number = 1): Promise<Product[]> {
139
141
  return await this.productRepository.search(category, keyword, page);
@@ -142,7 +144,7 @@ export class ProductService {
142
144
  // 立即清除缓存(不使用延迟双删)
143
145
  @CacheEvict("productSearch", {
144
146
  params: ["category"],
145
- delayedDoubleDeletion: false // 覆盖插件配置的默认策略
147
+ delayedDoubleDeletion: false
146
148
  })
147
149
  async updateProductCategory(category: string, updates: any): Promise<void> {
148
150
  await this.productRepository.updateCategory(category, updates);
@@ -202,33 +204,14 @@ export class ProductService {
202
204
 
203
205
  这样可以避免在并发场景下出现脏数据。
204
206
 
205
- ## 配置优先级
206
-
207
- 配置项的优先级从高到低:
208
-
209
- 1. **装饰器配置**: 直接在 `@CacheAble` 或 `@CacheEvict` 中指定的选项
210
- 2. **插件配置**: 在 `src/config/plugin.ts` 中配置的 `Cacheable` 插件选项
211
- 3. **默认值**: 系统内置的默认配置
212
-
213
- 例如:
214
- ```typescript
215
- // 插件配置
216
- Cacheable: {
217
- cacheTimeout: 300,
218
- delayedDoubleDeletion: true
219
- }
220
-
221
- // 装饰器配置会覆盖插件配置
222
- @CacheAble("user", {
223
- timeout: 600 // 使用 600 秒而不是插件配置的 300 秒
224
- })
225
- ```
226
-
227
207
  ## 注意事项
228
208
 
229
- 1. 缓存的数据会自动进行 JSON 序列化/反序列化
230
- 2. 如果缓存服务不可用,方法会正常执行,不会抛出错误
231
- 3. 插件会在应用启动时自动注入缓存功能到所有使用装饰器的方法
209
+ 1. **初始化顺序**: 必须先调用 `KoattyCached()` 初始化缓存,然后再使用装饰器。建议在应用启动时(如 `init()` 方法中)进行初始化
210
+ 2. 装饰器只能用于 `SERVICE` 和 `COMPONENT` 类型的类
211
+ 3. 被装饰的方法必须是异步方法(返回 Promise)
212
+ 4. 缓存的数据会自动进行 JSON 序列化/反序列化
213
+ 5. 如果缓存服务不可用,方法会正常执行,不会抛出错误(优雅降级)
214
+ 6. 缓存键长度超过 128 字符时会自动使用 murmur hash 进行压缩
232
215
 
233
216
  ## 许可证
234
217
 
package/dist/index.d.ts CHANGED
@@ -1,13 +1,13 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-06-23 01:59:58
3
+ * @Date: 2025-11-02 09:20:03
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
+ import { CacheStore } from 'koatty_store';
8
9
  import { Koatty } from 'koatty_core';
9
-
10
- export declare const CACHE_METADATA_KEY = "CACHE_METADATA_KEY";
10
+ import { StoreOptions } from 'koatty_store';
11
11
 
12
12
  /**
13
13
  * Decorate this method to support caching.
@@ -27,15 +27,15 @@ export declare const CACHE_METADATA_KEY = "CACHE_METADATA_KEY";
27
27
  * Use the 'id' parameters of the method as cache subkeys, the cache expiration time 30s
28
28
  * @returns {MethodDecorator}
29
29
  */
30
- export declare function CacheAble(cacheNameOrOpt?: string | CacheAbleOpt, opt?: CacheAbleOpt): MethodDecorator;
30
+ export declare function CacheAble(cacheName: string, opt?: CacheAbleOpt): MethodDecorator;
31
31
 
32
32
  /**
33
- * @description: CacheAble decorator options
33
+ * @description:
34
+ * @return {*}
34
35
  */
35
36
  export declare interface CacheAbleOpt {
36
37
  params?: string[];
37
38
  timeout?: number;
38
- cacheName?: string;
39
39
  }
40
40
 
41
41
  /**
@@ -54,40 +54,36 @@ export declare interface CacheAbleOpt {
54
54
  * and clear the cache after the method executed
55
55
  * @returns
56
56
  */
57
- export declare function CacheEvict(cacheNameOrOpt?: string | CacheEvictOpt, opt?: CacheEvictOpt): MethodDecorator;
57
+ export declare function CacheEvict(cacheName: string, opt?: CacheEvictOpt): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
58
58
 
59
59
  /**
60
- * @description: CacheEvict decorator options
60
+ * @description:
61
+ * @return {*}
61
62
  */
62
63
  export declare interface CacheEvictOpt {
63
64
  params?: string[];
64
65
  delayedDoubleDeletion?: boolean;
65
- cacheName?: string;
66
+ delayTime?: number;
66
67
  }
67
68
 
68
- declare interface CacheOptions {
69
- cacheTimeout?: number;
70
- delayedDoubleDeletion?: boolean;
71
- redisConfig?: RedisConfig;
72
- }
69
+ /**
70
+ * Close cache store connection for cleanup (mainly for testing)
71
+ */
72
+ export declare function CloseCacheStore(): Promise<void>;
73
73
 
74
- export declare enum DecoratorType {
75
- CACHE_EVICT = "CACHE_EVICT",
76
- CACHE_ABLE = "CACHE_ABLE"
77
- }
74
+ /**
75
+ * get instances of storeCache
76
+ *
77
+ * @export
78
+ * @param {StoreOptions} options
79
+ * @returns {*} {CacheStore}
80
+ */
81
+ export declare function GetCacheStore(options?: StoreOptions): Promise<CacheStore>;
78
82
 
79
83
  /**
80
- * @param options - The options for the scheduled job
84
+ * @param options - The options for the cached options
81
85
  * @param app - The Koatty application instance
82
86
  */
83
- export declare function KoattyCache(options: CacheOptions, app: Koatty): Promise<void>;
84
-
85
- declare interface RedisConfig {
86
- host?: string;
87
- port?: number;
88
- password?: string;
89
- db?: number;
90
- keyPrefix?: string;
91
- }
87
+ export declare function KoattyCached(options: StoreOptions, app: Koatty): Promise<void>;
92
88
 
93
89
  export { }