koatty_cacheable 1.6.1 → 2.0.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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
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)
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))
12
+
5
13
  ### [1.6.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.0...v1.6.1) (2025-06-09)
6
14
 
7
15
  ## [1.6.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.5.0...v1.6.0) (2024-11-07)
package/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # koatty_cacheable
2
2
 
3
- Cacheable for koatty.
3
+ Cacheable plugin for Koatty.
4
4
 
5
- Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级别的缓存功能。
5
+ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别的缓存功能。
6
6
 
7
7
  ## 特性
8
8
 
@@ -13,6 +13,7 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级
13
13
  - 🔧 **多后端支持**: 支持 Memory 和 Redis 缓存后端
14
14
  - 🎯 **参数化缓存**: 支持基于方法参数的缓存键生成
15
15
  - 🛡️ **类型安全**: 完整的 TypeScript 支持
16
+ - 📦 **插件化设计**: 遵循 Koatty 插件标准,统一管理
16
17
 
17
18
  ## 安装
18
19
 
@@ -22,28 +23,48 @@ npm install koatty_cacheable
22
23
 
23
24
  ## 配置
24
25
 
25
- koatty 项目的 `db.ts` 配置文件中添加缓存配置:
26
+ ### 1. Generate Plugin Template
27
+
28
+ Use Koatty CLI to generate the plugin template:
29
+
30
+ ```bash
31
+ kt plugin Cacheable
32
+ ```
33
+
34
+ Create `src/plugin/Cacheable.ts`:
35
+
36
+ ```typescript
37
+ import { Plugin, IPlugin, App } from "koatty";
38
+ import { KoattyCache } from "koatty_cacheable";
39
+
40
+ @Plugin()
41
+ export class Cacheable implements IPlugin {
42
+ run(options: any, app: App) {
43
+ return KoattyCache(options, app);
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### 2. Configure Plugin
49
+
50
+ Update `src/config/plugin.ts`:
26
51
 
27
52
  ```typescript
28
53
  export default {
29
- // ... 其他配置
30
-
31
- "CacheStore": {
32
- type: "memory", // 缓存类型: "redis" 或 "memory",默认为 "memory"
33
- // Redis 配置 (当 type 为 "redis" 时)
34
- // key_prefix: "koatty",
35
- // host: '127.0.0.1',
36
- // port: 6379,
37
- // name: "",
38
- // username: "",
39
- // password: "",
40
- // db: 0,
41
- // timeout: 30,
42
- // pool_size: 10,
43
- // conn_timeout: 30
44
- },
45
-
46
- // ... 其他配置
54
+ list: ["Cacheable"], // Plugin loading order
55
+ config: {
56
+ 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
+ }
66
+ }
67
+ }
47
68
  };
48
69
  ```
49
70
 
@@ -53,13 +74,15 @@ export default {
53
74
 
54
75
  ```typescript
55
76
  import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
77
+ import { Component } from "koatty_container";
56
78
 
79
+ @Component()
57
80
  export class UserService {
58
81
 
59
82
  // 自动缓存方法返回值
60
83
  @CacheAble("userCache", {
61
84
  params: ["id"], // 使用 id 参数作为缓存键的一部分
62
- timeout: 300 // 缓存过期时间(秒),默认 300 秒
85
+ timeout: 300 // 缓存过期时间(秒),默认使用插件配置的 cacheTimeout
63
86
  })
64
87
  async getUserById(id: string): Promise<User> {
65
88
  // 数据库查询逻辑
@@ -69,7 +92,7 @@ export class UserService {
69
92
  // 自动清除相关缓存
70
93
  @CacheEvict("userCache", {
71
94
  params: ["id"], // 使用 id 参数定位要清除的缓存
72
- delayedDoubleDeletion: true // 启用延迟双删策略,默认 true
95
+ delayedDoubleDeletion: true // 启用延迟双删策略,默认使用插件配置的 delayedDoubleDeletion
73
96
  })
74
97
  async updateUser(id: string, userData: Partial<User>): Promise<User> {
75
98
  // 更新用户数据
@@ -96,6 +119,9 @@ export class UserService {
96
119
  ### 高级用法
97
120
 
98
121
  ```typescript
122
+ import { Component } from "koatty_container";
123
+
124
+ @Component()
99
125
  export class ProductService {
100
126
 
101
127
  // 无参数缓存
@@ -107,7 +133,7 @@ export class ProductService {
107
133
  // 多参数缓存
108
134
  @CacheAble("productSearch", {
109
135
  params: ["category", "keyword"],
110
- timeout: 600
136
+ timeout: 600 // 覆盖插件配置的默认时间
111
137
  })
112
138
  async searchProducts(category: string, keyword: string, page: number = 1): Promise<Product[]> {
113
139
  return await this.productRepository.search(category, keyword, page);
@@ -116,7 +142,7 @@ export class ProductService {
116
142
  // 立即清除缓存(不使用延迟双删)
117
143
  @CacheEvict("productSearch", {
118
144
  params: ["category"],
119
- delayedDoubleDeletion: false
145
+ delayedDoubleDeletion: false // 覆盖插件配置的默认策略
120
146
  })
121
147
  async updateProductCategory(category: string, updates: any): Promise<void> {
122
148
  await this.productRepository.updateCategory(category, updates);
@@ -176,12 +202,33 @@ export class ProductService {
176
202
 
177
203
  这样可以避免在并发场景下出现脏数据。
178
204
 
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
+
179
227
  ## 注意事项
180
228
 
181
- 1. 装饰器只能用于 `SERVICE` 和 `COMPONENT` 类型的类
182
- 2. 被装饰的方法必须是异步方法(返回 Promise)
183
- 3. 缓存的数据会自动进行 JSON 序列化/反序列化
184
- 4. 如果缓存服务不可用,方法会正常执行,不会抛出错误
229
+ 1. 缓存的数据会自动进行 JSON 序列化/反序列化
230
+ 2. 如果缓存服务不可用,方法会正常执行,不会抛出错误
231
+ 3. 插件会在应用启动时自动注入缓存功能到所有使用装饰器的方法
185
232
 
186
233
  ## 许可证
187
234
 
package/dist/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # koatty_cacheable
2
2
 
3
- Cacheable for koatty.
3
+ Cacheable plugin for Koatty.
4
4
 
5
- Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级别的缓存功能。
5
+ Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别的缓存功能。
6
6
 
7
7
  ## 特性
8
8
 
@@ -13,6 +13,7 @@ Koatty框架的 CacheAble, CacheEvict 缓存装饰器支持库,提供方法级
13
13
  - 🔧 **多后端支持**: 支持 Memory 和 Redis 缓存后端
14
14
  - 🎯 **参数化缓存**: 支持基于方法参数的缓存键生成
15
15
  - 🛡️ **类型安全**: 完整的 TypeScript 支持
16
+ - 📦 **插件化设计**: 遵循 Koatty 插件标准,统一管理
16
17
 
17
18
  ## 安装
18
19
 
@@ -22,28 +23,48 @@ npm install koatty_cacheable
22
23
 
23
24
  ## 配置
24
25
 
25
- koatty 项目的 `db.ts` 配置文件中添加缓存配置:
26
+ ### 1. Generate Plugin Template
27
+
28
+ Use Koatty CLI to generate the plugin template:
29
+
30
+ ```bash
31
+ kt plugin Cacheable
32
+ ```
33
+
34
+ Create `src/plugin/Cacheable.ts`:
35
+
36
+ ```typescript
37
+ import { Plugin, IPlugin, App } from "koatty";
38
+ import { KoattyCache } from "koatty_cacheable";
39
+
40
+ @Plugin()
41
+ export class Cacheable implements IPlugin {
42
+ run(options: any, app: App) {
43
+ return KoattyCache(options, app);
44
+ }
45
+ }
46
+ ```
47
+
48
+ ### 2. Configure Plugin
49
+
50
+ Update `src/config/plugin.ts`:
26
51
 
27
52
  ```typescript
28
53
  export default {
29
- // ... 其他配置
30
-
31
- "CacheStore": {
32
- type: "memory", // 缓存类型: "redis" 或 "memory",默认为 "memory"
33
- // Redis 配置 (当 type 为 "redis" 时)
34
- // key_prefix: "koatty",
35
- // host: '127.0.0.1',
36
- // port: 6379,
37
- // name: "",
38
- // username: "",
39
- // password: "",
40
- // db: 0,
41
- // timeout: 30,
42
- // pool_size: 10,
43
- // conn_timeout: 30
44
- },
45
-
46
- // ... 其他配置
54
+ list: ["Cacheable"], // Plugin loading order
55
+ config: {
56
+ 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
+ }
66
+ }
67
+ }
47
68
  };
48
69
  ```
49
70
 
@@ -53,13 +74,15 @@ export default {
53
74
 
54
75
  ```typescript
55
76
  import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
77
+ import { Component } from "koatty_container";
56
78
 
79
+ @Component()
57
80
  export class UserService {
58
81
 
59
82
  // 自动缓存方法返回值
60
83
  @CacheAble("userCache", {
61
84
  params: ["id"], // 使用 id 参数作为缓存键的一部分
62
- timeout: 300 // 缓存过期时间(秒),默认 300 秒
85
+ timeout: 300 // 缓存过期时间(秒),默认使用插件配置的 cacheTimeout
63
86
  })
64
87
  async getUserById(id: string): Promise<User> {
65
88
  // 数据库查询逻辑
@@ -69,7 +92,7 @@ export class UserService {
69
92
  // 自动清除相关缓存
70
93
  @CacheEvict("userCache", {
71
94
  params: ["id"], // 使用 id 参数定位要清除的缓存
72
- delayedDoubleDeletion: true // 启用延迟双删策略,默认 true
95
+ delayedDoubleDeletion: true // 启用延迟双删策略,默认使用插件配置的 delayedDoubleDeletion
73
96
  })
74
97
  async updateUser(id: string, userData: Partial<User>): Promise<User> {
75
98
  // 更新用户数据
@@ -96,6 +119,9 @@ export class UserService {
96
119
  ### 高级用法
97
120
 
98
121
  ```typescript
122
+ import { Component } from "koatty_container";
123
+
124
+ @Component()
99
125
  export class ProductService {
100
126
 
101
127
  // 无参数缓存
@@ -107,7 +133,7 @@ export class ProductService {
107
133
  // 多参数缓存
108
134
  @CacheAble("productSearch", {
109
135
  params: ["category", "keyword"],
110
- timeout: 600
136
+ timeout: 600 // 覆盖插件配置的默认时间
111
137
  })
112
138
  async searchProducts(category: string, keyword: string, page: number = 1): Promise<Product[]> {
113
139
  return await this.productRepository.search(category, keyword, page);
@@ -116,7 +142,7 @@ export class ProductService {
116
142
  // 立即清除缓存(不使用延迟双删)
117
143
  @CacheEvict("productSearch", {
118
144
  params: ["category"],
119
- delayedDoubleDeletion: false
145
+ delayedDoubleDeletion: false // 覆盖插件配置的默认策略
120
146
  })
121
147
  async updateProductCategory(category: string, updates: any): Promise<void> {
122
148
  await this.productRepository.updateCategory(category, updates);
@@ -176,12 +202,33 @@ export class ProductService {
176
202
 
177
203
  这样可以避免在并发场景下出现脏数据。
178
204
 
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
+
179
227
  ## 注意事项
180
228
 
181
- 1. 装饰器只能用于 `SERVICE` 和 `COMPONENT` 类型的类
182
- 2. 被装饰的方法必须是异步方法(返回 Promise)
183
- 3. 缓存的数据会自动进行 JSON 序列化/反序列化
184
- 4. 如果缓存服务不可用,方法会正常执行,不会抛出错误
229
+ 1. 缓存的数据会自动进行 JSON 序列化/反序列化
230
+ 2. 如果缓存服务不可用,方法会正常执行,不会抛出错误
231
+ 3. 插件会在应用启动时自动注入缓存功能到所有使用装饰器的方法
185
232
 
186
233
  ## 许可证
187
234
 
package/dist/index.d.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  /*!
2
2
  * @Author: richen
3
- * @Date: 2025-06-09 13:04:42
3
+ * @Date: 2025-06-23 01:59:58
4
4
  * @License: BSD (3-Clause)
5
5
  * @Copyright (c) - <richenlin(at)gmail.com>
6
6
  * @HomePage: https://koatty.org/
7
7
  */
8
- import { Application } from 'koatty_container';
9
- import { CacheStore } from 'koatty_store';
8
+ import { Koatty } from 'koatty_core';
9
+
10
+ export declare const CACHE_METADATA_KEY = "CACHE_METADATA_KEY";
10
11
 
11
12
  /**
12
13
  * Decorate this method to support caching.
@@ -26,15 +27,15 @@ import { CacheStore } from 'koatty_store';
26
27
  * Use the 'id' parameters of the method as cache subkeys, the cache expiration time 30s
27
28
  * @returns {MethodDecorator}
28
29
  */
29
- export declare function CacheAble(cacheName: string, opt?: CacheAbleOpt): MethodDecorator;
30
+ export declare function CacheAble(cacheNameOrOpt?: string | CacheAbleOpt, opt?: CacheAbleOpt): MethodDecorator;
30
31
 
31
32
  /**
32
- * @description:
33
- * @return {*}
33
+ * @description: CacheAble decorator options
34
34
  */
35
35
  export declare interface CacheAbleOpt {
36
36
  params?: string[];
37
37
  timeout?: number;
38
+ cacheName?: string;
38
39
  }
39
40
 
40
41
  /**
@@ -53,35 +54,40 @@ export declare interface CacheAbleOpt {
53
54
  * and clear the cache after the method executed
54
55
  * @returns
55
56
  */
56
- export declare function CacheEvict(cacheName: string, opt?: CacheEvictOpt): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
57
+ export declare function CacheEvict(cacheNameOrOpt?: string | CacheEvictOpt, opt?: CacheEvictOpt): MethodDecorator;
57
58
 
58
59
  /**
59
- * @description:
60
- * @return {*}
60
+ * @description: CacheEvict decorator options
61
61
  */
62
62
  export declare interface CacheEvictOpt {
63
63
  params?: string[];
64
64
  delayedDoubleDeletion?: boolean;
65
+ cacheName?: string;
65
66
  }
66
67
 
67
- /**
68
- * Close cache store connection for cleanup (mainly for testing)
69
- */
70
- export declare function CloseCacheStore(): Promise<void>;
68
+ declare interface CacheOptions {
69
+ cacheTimeout?: number;
70
+ delayedDoubleDeletion?: boolean;
71
+ redisConfig?: RedisConfig;
72
+ }
71
73
 
72
- /**
73
- * get instances of storeCache
74
- *
75
- * @export
76
- * @param {Application} app
77
- * @returns {*} {CacheStore}
78
- */
79
- export declare function GetCacheStore(app?: Application): Promise<CacheStore>;
74
+ export declare enum DecoratorType {
75
+ CACHE_EVICT = "CACHE_EVICT",
76
+ CACHE_ABLE = "CACHE_ABLE"
77
+ }
80
78
 
81
79
  /**
82
- * initiation CacheStore connection and client.
83
- *
80
+ * @param options - The options for the scheduled job
81
+ * @param app - The Koatty application instance
84
82
  */
85
- export declare function InitCacheStore(): Promise<void>;
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
+ }
86
92
 
87
93
  export { }