koatty_cacheable 1.6.0 → 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/.rollup.config.js +59 -59
- package/CHANGELOG.md +47 -37
- package/LICENSE +29 -29
- package/README.md +235 -57
- package/dist/LICENSE +29 -29
- package/dist/README.md +235 -57
- package/dist/index.d.ts +32 -15
- package/dist/index.js +326 -132
- package/dist/index.mjs +325 -132
- package/dist/package.json +102 -98
- package/package.json +102 -98
package/.rollup.config.js
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* @Description:
|
|
3
|
-
* @Usage:
|
|
4
|
-
* @Author: richen
|
|
5
|
-
* @Date: 2021-12-17 10:20:44
|
|
6
|
-
* @LastEditTime: 2024-11-04 22:04:45
|
|
7
|
-
*/
|
|
8
|
-
import commonjs from '@rollup/plugin-commonjs';
|
|
9
|
-
import json from "@rollup/plugin-json";
|
|
10
|
-
import resolve from '@rollup/plugin-node-resolve';
|
|
11
|
-
import { builtinModules } from 'module';
|
|
12
|
-
import del from "rollup-plugin-delete";
|
|
13
|
-
import typescript from 'rollup-plugin-typescript2';
|
|
14
|
-
// import babel from '@rollup/plugin-babel';
|
|
15
|
-
const pkg = require('./package.json');
|
|
16
|
-
|
|
17
|
-
export default [
|
|
18
|
-
{
|
|
19
|
-
input: './src/index.ts',
|
|
20
|
-
output: [
|
|
21
|
-
{
|
|
22
|
-
format: 'cjs',
|
|
23
|
-
file: './dist/index.js',
|
|
24
|
-
banner: require('./scripts/copyright'),
|
|
25
|
-
},
|
|
26
|
-
{
|
|
27
|
-
format: 'es',
|
|
28
|
-
file: './dist/index.mjs',
|
|
29
|
-
banner: require('./scripts/copyright'),
|
|
30
|
-
},
|
|
31
|
-
],
|
|
32
|
-
plugins: [
|
|
33
|
-
del({ targets: ["dist/*", "temp/*", "docs/api"] }),
|
|
34
|
-
// babel({
|
|
35
|
-
// babelHelpers: "runtime",
|
|
36
|
-
// configFile: './babel.config.js',
|
|
37
|
-
// exclude: 'node_modules/**',
|
|
38
|
-
// }),
|
|
39
|
-
json(),
|
|
40
|
-
resolve({
|
|
41
|
-
preferBuiltins: true, // 优先选择内置模块
|
|
42
|
-
}),
|
|
43
|
-
commonjs(),
|
|
44
|
-
typescript({
|
|
45
|
-
tsconfigOverride: {
|
|
46
|
-
compilerOptions: {
|
|
47
|
-
declaration: false,
|
|
48
|
-
declarationMap: false,
|
|
49
|
-
module: "ESNext"
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
})
|
|
53
|
-
],
|
|
54
|
-
external: [
|
|
55
|
-
...builtinModules, // 排除 Node.js 内置模块
|
|
56
|
-
...Object.keys(pkg.dependencies || {}), // 排除 package.json 中的外部依赖
|
|
57
|
-
],
|
|
58
|
-
},
|
|
59
|
-
|
|
1
|
+
/*
|
|
2
|
+
* @Description:
|
|
3
|
+
* @Usage:
|
|
4
|
+
* @Author: richen
|
|
5
|
+
* @Date: 2021-12-17 10:20:44
|
|
6
|
+
* @LastEditTime: 2024-11-04 22:04:45
|
|
7
|
+
*/
|
|
8
|
+
import commonjs from '@rollup/plugin-commonjs';
|
|
9
|
+
import json from "@rollup/plugin-json";
|
|
10
|
+
import resolve from '@rollup/plugin-node-resolve';
|
|
11
|
+
import { builtinModules } from 'module';
|
|
12
|
+
import del from "rollup-plugin-delete";
|
|
13
|
+
import typescript from 'rollup-plugin-typescript2';
|
|
14
|
+
// import babel from '@rollup/plugin-babel';
|
|
15
|
+
const pkg = require('./package.json');
|
|
16
|
+
|
|
17
|
+
export default [
|
|
18
|
+
{
|
|
19
|
+
input: './src/index.ts',
|
|
20
|
+
output: [
|
|
21
|
+
{
|
|
22
|
+
format: 'cjs',
|
|
23
|
+
file: './dist/index.js',
|
|
24
|
+
banner: require('./scripts/copyright'),
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
format: 'es',
|
|
28
|
+
file: './dist/index.mjs',
|
|
29
|
+
banner: require('./scripts/copyright'),
|
|
30
|
+
},
|
|
31
|
+
],
|
|
32
|
+
plugins: [
|
|
33
|
+
del({ targets: ["dist/*", "temp/*", "docs/api"] }),
|
|
34
|
+
// babel({
|
|
35
|
+
// babelHelpers: "runtime",
|
|
36
|
+
// configFile: './babel.config.js',
|
|
37
|
+
// exclude: 'node_modules/**',
|
|
38
|
+
// }),
|
|
39
|
+
json(),
|
|
40
|
+
resolve({
|
|
41
|
+
preferBuiltins: true, // 优先选择内置模块
|
|
42
|
+
}),
|
|
43
|
+
commonjs(),
|
|
44
|
+
typescript({
|
|
45
|
+
tsconfigOverride: {
|
|
46
|
+
compilerOptions: {
|
|
47
|
+
declaration: false,
|
|
48
|
+
declarationMap: false,
|
|
49
|
+
module: "ESNext"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
],
|
|
54
|
+
external: [
|
|
55
|
+
...builtinModules, // 排除 Node.js 内置模块
|
|
56
|
+
...Object.keys(pkg.dependencies || {}), // 排除 package.json 中的外部依赖
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
|
|
60
60
|
]
|
package/CHANGELOG.md
CHANGED
|
@@ -2,42 +2,52 @@
|
|
|
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
|
+
|
|
13
|
+
### [1.6.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.6.0...v1.6.1) (2025-06-09)
|
|
14
|
+
|
|
5
15
|
## [1.6.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.5.0...v1.6.0) (2024-11-07)
|
|
6
16
|
|
|
7
|
-
## [1.5.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.3...v1.5.0) (2024-04-01)
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
### Features
|
|
11
|
-
|
|
12
|
-
* delayedDoubleDeletion ([532f735](https://github.com/thinkkoa/koatty_cacheable/commit/532f735818fc66fa144023cd24282efe350833ba))
|
|
13
|
-
|
|
14
|
-
### [1.4.3](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.2...v1.4.3) (2024-01-17)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
### Bug Fixes
|
|
18
|
-
|
|
19
|
-
* 缓存key拼装 ([5d0b924](https://github.com/thinkkoa/koatty_cacheable/commit/5d0b9248064d353046bd7287d3a3ee5229e88c23))
|
|
20
|
-
|
|
21
|
-
### [1.4.2](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.1...v1.4.2) (2023-12-20)
|
|
22
|
-
|
|
23
|
-
### [1.4.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.0...v1.4.1) (2023-08-04)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
### Bug Fixes
|
|
27
|
-
|
|
28
|
-
* default value ([38f2e57](https://github.com/thinkkoa/koatty_cacheable/commit/38f2e57d52d13482907ca9eac6006d1e841bdadd))
|
|
29
|
-
* 初始化缓存改为appReady执行 ([7e9b1ab](https://github.com/thinkkoa/koatty_cacheable/commit/7e9b1abe6acdbdf1b6ad34efa053cbaca1e7b6d7))
|
|
30
|
-
|
|
31
|
-
## [1.4.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.8...v1.4.0) (2023-02-18)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
### Bug Fixes
|
|
35
|
-
|
|
36
|
-
* cache subkey ([408e3c7](https://github.com/thinkkoa/koatty_cacheable/commit/408e3c709a4dfff6e7d224a22d26e58854a805ac))
|
|
37
|
-
* 启动时链接 ([e7ec709](https://github.com/thinkkoa/koatty_cacheable/commit/e7ec7094be106e4e48783c9b214b8369e8a18297))
|
|
38
|
-
* 支持引入变量做缓存key ([4ce81aa](https://github.com/thinkkoa/koatty_cacheable/commit/4ce81aaaf9610f4a62ddf43e3c4389dd003c2db8))
|
|
39
|
-
* 获取单例 ([0759972](https://github.com/thinkkoa/koatty_cacheable/commit/0759972adf7e86312f60f0bf77604bddf06ce0de))
|
|
40
|
-
|
|
41
|
-
### [1.3.8](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.8) (2023-01-13)
|
|
42
|
-
|
|
43
|
-
### [1.3.7](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.7) (2022-05-27)
|
|
17
|
+
## [1.5.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.3...v1.5.0) (2024-04-01)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
### Features
|
|
21
|
+
|
|
22
|
+
* delayedDoubleDeletion ([532f735](https://github.com/thinkkoa/koatty_cacheable/commit/532f735818fc66fa144023cd24282efe350833ba))
|
|
23
|
+
|
|
24
|
+
### [1.4.3](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.2...v1.4.3) (2024-01-17)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* 缓存key拼装 ([5d0b924](https://github.com/thinkkoa/koatty_cacheable/commit/5d0b9248064d353046bd7287d3a3ee5229e88c23))
|
|
30
|
+
|
|
31
|
+
### [1.4.2](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.1...v1.4.2) (2023-12-20)
|
|
32
|
+
|
|
33
|
+
### [1.4.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.0...v1.4.1) (2023-08-04)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
### Bug Fixes
|
|
37
|
+
|
|
38
|
+
* default value ([38f2e57](https://github.com/thinkkoa/koatty_cacheable/commit/38f2e57d52d13482907ca9eac6006d1e841bdadd))
|
|
39
|
+
* 初始化缓存改为appReady执行 ([7e9b1ab](https://github.com/thinkkoa/koatty_cacheable/commit/7e9b1abe6acdbdf1b6ad34efa053cbaca1e7b6d7))
|
|
40
|
+
|
|
41
|
+
## [1.4.0](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.8...v1.4.0) (2023-02-18)
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
### Bug Fixes
|
|
45
|
+
|
|
46
|
+
* cache subkey ([408e3c7](https://github.com/thinkkoa/koatty_cacheable/commit/408e3c709a4dfff6e7d224a22d26e58854a805ac))
|
|
47
|
+
* 启动时链接 ([e7ec709](https://github.com/thinkkoa/koatty_cacheable/commit/e7ec7094be106e4e48783c9b214b8369e8a18297))
|
|
48
|
+
* 支持引入变量做缓存key ([4ce81aa](https://github.com/thinkkoa/koatty_cacheable/commit/4ce81aaaf9610f4a62ddf43e3c4389dd003c2db8))
|
|
49
|
+
* 获取单例 ([0759972](https://github.com/thinkkoa/koatty_cacheable/commit/0759972adf7e86312f60f0bf77604bddf06ce0de))
|
|
50
|
+
|
|
51
|
+
### [1.3.8](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.8) (2023-01-13)
|
|
52
|
+
|
|
53
|
+
### [1.3.7](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.7) (2022-05-27)
|
package/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020, Koatty
|
|
4
|
-
All rights reserved.
|
|
5
|
-
|
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
|
7
|
-
modification, are permitted provided that the following conditions are met:
|
|
8
|
-
|
|
9
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
-
list of conditions and the following disclaimer.
|
|
11
|
-
|
|
12
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
and/or other materials provided with the distribution.
|
|
15
|
-
|
|
16
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
-
contributors may be used to endorse or promote products derived from
|
|
18
|
-
this software without specific prior written permission.
|
|
19
|
-
|
|
20
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Koatty
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
CHANGED
|
@@ -1,57 +1,235 @@
|
|
|
1
|
-
# koatty_cacheable
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
```
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
```
|
|
37
|
-
import {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
1
|
+
# koatty_cacheable
|
|
2
|
+
|
|
3
|
+
Cacheable plugin for Koatty.
|
|
4
|
+
|
|
5
|
+
Koatty框架的 CacheAble, CacheEvict 缓存装饰器插件,提供方法级别的缓存功能。
|
|
6
|
+
|
|
7
|
+
## 特性
|
|
8
|
+
|
|
9
|
+
- 🚀 **简单易用**: 通过装饰器轻松添加缓存功能
|
|
10
|
+
- 🔄 **自动缓存**: `@CacheAble` 装饰器自动缓存方法返回值
|
|
11
|
+
- 🗑️ **智能清除**: `@CacheEvict` 装饰器智能清除相关缓存
|
|
12
|
+
- ⚡ **延迟双删**: 支持延迟双删策略,解决缓存一致性问题
|
|
13
|
+
- 🔧 **多后端支持**: 支持 Memory 和 Redis 缓存后端
|
|
14
|
+
- 🎯 **参数化缓存**: 支持基于方法参数的缓存键生成
|
|
15
|
+
- 🛡️ **类型安全**: 完整的 TypeScript 支持
|
|
16
|
+
- 📦 **插件化设计**: 遵循 Koatty 插件标准,统一管理
|
|
17
|
+
|
|
18
|
+
## 安装
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
npm install koatty_cacheable
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## 配置
|
|
25
|
+
|
|
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`:
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
export default {
|
|
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
|
+
}
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 使用方法
|
|
72
|
+
|
|
73
|
+
### 基本用法
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
|
|
77
|
+
import { Component } from "koatty_container";
|
|
78
|
+
|
|
79
|
+
@Component()
|
|
80
|
+
export class UserService {
|
|
81
|
+
|
|
82
|
+
// 自动缓存方法返回值
|
|
83
|
+
@CacheAble("userCache", {
|
|
84
|
+
params: ["id"], // 使用 id 参数作为缓存键的一部分
|
|
85
|
+
timeout: 300 // 缓存过期时间(秒),默认使用插件配置的 cacheTimeout
|
|
86
|
+
})
|
|
87
|
+
async getUserById(id: string): Promise<User> {
|
|
88
|
+
// 数据库查询逻辑
|
|
89
|
+
return await this.userRepository.findById(id);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 自动清除相关缓存
|
|
93
|
+
@CacheEvict("userCache", {
|
|
94
|
+
params: ["id"], // 使用 id 参数定位要清除的缓存
|
|
95
|
+
delayedDoubleDeletion: true // 启用延迟双删策略,默认使用插件配置的 delayedDoubleDeletion
|
|
96
|
+
})
|
|
97
|
+
async updateUser(id: string, userData: Partial<User>): Promise<User> {
|
|
98
|
+
// 更新用户数据
|
|
99
|
+
const updatedUser = await this.userRepository.update(id, userData);
|
|
100
|
+
return updatedUser;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// 手动操作缓存
|
|
104
|
+
async customCacheOperation() {
|
|
105
|
+
const store = await GetCacheStore(this.app);
|
|
106
|
+
|
|
107
|
+
// 设置缓存
|
|
108
|
+
await store.set("custom:key", "value", 60);
|
|
109
|
+
|
|
110
|
+
// 获取缓存
|
|
111
|
+
const value = await store.get("custom:key");
|
|
112
|
+
|
|
113
|
+
// 删除缓存
|
|
114
|
+
await store.del("custom:key");
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### 高级用法
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
import { Component } from "koatty_container";
|
|
123
|
+
|
|
124
|
+
@Component()
|
|
125
|
+
export class ProductService {
|
|
126
|
+
|
|
127
|
+
// 无参数缓存
|
|
128
|
+
@CacheAble("productStats")
|
|
129
|
+
async getProductStats(): Promise<ProductStats> {
|
|
130
|
+
return await this.calculateStats();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// 多参数缓存
|
|
134
|
+
@CacheAble("productSearch", {
|
|
135
|
+
params: ["category", "keyword"],
|
|
136
|
+
timeout: 600 // 覆盖插件配置的默认时间
|
|
137
|
+
})
|
|
138
|
+
async searchProducts(category: string, keyword: string, page: number = 1): Promise<Product[]> {
|
|
139
|
+
return await this.productRepository.search(category, keyword, page);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// 立即清除缓存(不使用延迟双删)
|
|
143
|
+
@CacheEvict("productSearch", {
|
|
144
|
+
params: ["category"],
|
|
145
|
+
delayedDoubleDeletion: false // 覆盖插件配置的默认策略
|
|
146
|
+
})
|
|
147
|
+
async updateProductCategory(category: string, updates: any): Promise<void> {
|
|
148
|
+
await this.productRepository.updateCategory(category, updates);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## API 文档
|
|
154
|
+
|
|
155
|
+
### @CacheAble(cacheName, options?)
|
|
156
|
+
|
|
157
|
+
自动缓存装饰器,缓存方法的返回值。
|
|
158
|
+
|
|
159
|
+
**参数:**
|
|
160
|
+
- `cacheName: string` - 缓存名称
|
|
161
|
+
- `options?: CacheAbleOpt` - 缓存选项
|
|
162
|
+
- `params?: string[]` - 用作缓存键的参数名数组
|
|
163
|
+
- `timeout?: number` - 缓存过期时间(秒),默认 300
|
|
164
|
+
|
|
165
|
+
### @CacheEvict(cacheName, options?)
|
|
166
|
+
|
|
167
|
+
自动清除缓存装饰器,在方法执行后清除相关缓存。
|
|
168
|
+
|
|
169
|
+
**参数:**
|
|
170
|
+
- `cacheName: string` - 要清除的缓存名称
|
|
171
|
+
- `options?: CacheEvictOpt` - 清除选项
|
|
172
|
+
- `params?: string[]` - 用于定位缓存的参数名数组
|
|
173
|
+
- `delayedDoubleDeletion?: boolean` - 是否启用延迟双删策略,默认 true
|
|
174
|
+
|
|
175
|
+
### GetCacheStore(app?)
|
|
176
|
+
|
|
177
|
+
获取缓存存储实例。
|
|
178
|
+
|
|
179
|
+
**参数:**
|
|
180
|
+
- `app?: Application` - Koatty 应用实例
|
|
181
|
+
|
|
182
|
+
**返回:** `Promise<CacheStore>`
|
|
183
|
+
|
|
184
|
+
## 缓存键生成规则
|
|
185
|
+
|
|
186
|
+
缓存键按以下格式生成:
|
|
187
|
+
```
|
|
188
|
+
{cacheName}:{paramName1}:{paramValue1}:{paramName2}:{paramValue2}...
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
例如:
|
|
192
|
+
- `@CacheAble("user", {params: ["id"]})` + `getUserById("123")` → `user:id:123`
|
|
193
|
+
- 当缓存键长度超过 128 字符时,会自动使用 murmur hash 进行压缩
|
|
194
|
+
|
|
195
|
+
## 延迟双删策略
|
|
196
|
+
|
|
197
|
+
延迟双删是一种解决缓存一致性问题的策略:
|
|
198
|
+
|
|
199
|
+
1. 立即删除缓存
|
|
200
|
+
2. 执行数据更新操作
|
|
201
|
+
3. 延迟 5 秒后再次删除缓存
|
|
202
|
+
|
|
203
|
+
这样可以避免在并发场景下出现脏数据。
|
|
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
|
+
|
|
227
|
+
## 注意事项
|
|
228
|
+
|
|
229
|
+
1. 缓存的数据会自动进行 JSON 序列化/反序列化
|
|
230
|
+
2. 如果缓存服务不可用,方法会正常执行,不会抛出错误
|
|
231
|
+
3. 插件会在应用启动时自动注入缓存功能到所有使用装饰器的方法
|
|
232
|
+
|
|
233
|
+
## 许可证
|
|
234
|
+
|
|
235
|
+
BSD-3-Clause
|
package/dist/LICENSE
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
BSD 3-Clause License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020, Koatty
|
|
4
|
-
All rights reserved.
|
|
5
|
-
|
|
6
|
-
Redistribution and use in source and binary forms, with or without
|
|
7
|
-
modification, are permitted provided that the following conditions are met:
|
|
8
|
-
|
|
9
|
-
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
-
list of conditions and the following disclaimer.
|
|
11
|
-
|
|
12
|
-
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
-
this list of conditions and the following disclaimer in the documentation
|
|
14
|
-
and/or other materials provided with the distribution.
|
|
15
|
-
|
|
16
|
-
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
-
contributors may be used to endorse or promote products derived from
|
|
18
|
-
this software without specific prior written permission.
|
|
19
|
-
|
|
20
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
-
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
-
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
-
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
-
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
-
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
-
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
-
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
-
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
1
|
+
BSD 3-Clause License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020, Koatty
|
|
4
|
+
All rights reserved.
|
|
5
|
+
|
|
6
|
+
Redistribution and use in source and binary forms, with or without
|
|
7
|
+
modification, are permitted provided that the following conditions are met:
|
|
8
|
+
|
|
9
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
|
10
|
+
list of conditions and the following disclaimer.
|
|
11
|
+
|
|
12
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
13
|
+
this list of conditions and the following disclaimer in the documentation
|
|
14
|
+
and/or other materials provided with the distribution.
|
|
15
|
+
|
|
16
|
+
3. Neither the name of the copyright holder nor the names of its
|
|
17
|
+
contributors may be used to endorse or promote products derived from
|
|
18
|
+
this software without specific prior written permission.
|
|
19
|
+
|
|
20
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
21
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
22
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
23
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
24
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
25
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
26
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
27
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
28
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
29
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|