koatty_cacheable 1.4.1 → 1.4.3
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 +9 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +20 -45
- package/dist/index.mjs +20 -45
- package/dist/package.json +6 -4
- package/package.json +6 -4
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
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
|
+
### [1.4.3](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.2...v1.4.3) (2024-01-17)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Bug Fixes
|
|
9
|
+
|
|
10
|
+
* 缓存key拼装 ([5d0b924](https://github.com/thinkkoa/koatty_cacheable/commit/5d0b9248064d353046bd7287d3a3ee5229e88c23))
|
|
11
|
+
|
|
12
|
+
### [1.4.2](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.1...v1.4.2) (2023-12-20)
|
|
13
|
+
|
|
5
14
|
### [1.4.1](https://github.com/thinkkoa/koatty_cacheable/compare/v1.4.0...v1.4.1) (2023-08-04)
|
|
6
15
|
|
|
7
16
|
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date:
|
|
3
|
+
* @Date: 2024-01-17 22:08:01
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -15,11 +15,11 @@ var koatty_container = require('koatty_container');
|
|
|
15
15
|
/*
|
|
16
16
|
* @Author: richen
|
|
17
17
|
* @Date: 2020-07-06 19:53:43
|
|
18
|
-
* @LastEditTime:
|
|
18
|
+
* @LastEditTime: 2024-01-17 21:54:53
|
|
19
19
|
* @Description:
|
|
20
20
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
21
21
|
*/
|
|
22
|
-
const
|
|
22
|
+
const longKey = 128;
|
|
23
23
|
// storeCache
|
|
24
24
|
const storeCache = {
|
|
25
25
|
store: null
|
|
@@ -78,7 +78,7 @@ async function InitCacheStore() {
|
|
|
78
78
|
*/
|
|
79
79
|
function CacheAble(cacheName, opt = {
|
|
80
80
|
params: [],
|
|
81
|
-
timeout:
|
|
81
|
+
timeout: 300,
|
|
82
82
|
}) {
|
|
83
83
|
return (target, methodName, descriptor) => {
|
|
84
84
|
const componentType = koatty_container.IOCContainer.getType(target);
|
|
@@ -89,11 +89,11 @@ function CacheAble(cacheName, opt = {
|
|
|
89
89
|
opt = {
|
|
90
90
|
...{
|
|
91
91
|
params: [],
|
|
92
|
-
timeout:
|
|
92
|
+
timeout: 300,
|
|
93
93
|
}, ...opt
|
|
94
94
|
};
|
|
95
95
|
// 获取定义的参数位置
|
|
96
|
-
const paramIndexes = getParamIndex(opt.params
|
|
96
|
+
const paramIndexes = getParamIndex(opt.params);
|
|
97
97
|
descriptor = {
|
|
98
98
|
configurable,
|
|
99
99
|
enumerable,
|
|
@@ -107,35 +107,31 @@ function CacheAble(cacheName, opt = {
|
|
|
107
107
|
});
|
|
108
108
|
if (cacheFlag) {
|
|
109
109
|
// tslint:disable-next-line: one-variable-per-declaration
|
|
110
|
-
let key =
|
|
110
|
+
let key = cacheName;
|
|
111
111
|
if (props && props.length > 0) {
|
|
112
112
|
for (const item of paramIndexes) {
|
|
113
113
|
if (props[item] !== undefined) {
|
|
114
114
|
const value = koatty_lib.Helper.toString(props[item]);
|
|
115
|
-
key += `:${value}`;
|
|
115
|
+
key += `:${opt.params[item]}:${value}`;
|
|
116
116
|
}
|
|
117
117
|
}
|
|
118
118
|
// 防止key超长
|
|
119
|
-
if (key.length >
|
|
119
|
+
if (key.length > longKey) {
|
|
120
120
|
key = koatty_lib.Helper.murmurHash(key);
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
-
let res = await store.get(
|
|
123
|
+
let res = await store.get(key).catch(() => null);
|
|
124
124
|
if (!koatty_lib.Helper.isEmpty(res)) {
|
|
125
125
|
return JSON.parse(res);
|
|
126
126
|
}
|
|
127
127
|
// tslint:disable-next-line: no-invalid-this
|
|
128
128
|
res = await value.apply(this, props);
|
|
129
129
|
// prevent cache penetration
|
|
130
|
-
if (koatty_lib.Helper.
|
|
130
|
+
if (koatty_lib.Helper.isTrueEmpty(res)) {
|
|
131
131
|
res = "";
|
|
132
|
-
opt.timeout = 5;
|
|
133
|
-
}
|
|
134
|
-
if (!opt.timeout) {
|
|
135
|
-
opt.timeout = 3600;
|
|
136
132
|
}
|
|
137
133
|
// async set store
|
|
138
|
-
store.set(
|
|
134
|
+
store.set(key, JSON.stringify(res), opt.timeout).catch(() => null);
|
|
139
135
|
return res;
|
|
140
136
|
}
|
|
141
137
|
else {
|
|
@@ -178,7 +174,7 @@ function CacheEvict(cacheName, opt = {
|
|
|
178
174
|
eventTime: "Before",
|
|
179
175
|
}, ...opt
|
|
180
176
|
};
|
|
181
|
-
const paramIndexes = getParamIndex(opt.params
|
|
177
|
+
const paramIndexes = getParamIndex(opt.params);
|
|
182
178
|
descriptor = {
|
|
183
179
|
configurable,
|
|
184
180
|
enumerable,
|
|
@@ -191,28 +187,28 @@ function CacheEvict(cacheName, opt = {
|
|
|
191
187
|
return null;
|
|
192
188
|
});
|
|
193
189
|
if (cacheFlag) {
|
|
194
|
-
let key =
|
|
190
|
+
let key = cacheName;
|
|
195
191
|
if (props && props.length > 0) {
|
|
196
192
|
for (const item of paramIndexes) {
|
|
197
193
|
if (props[item] !== undefined) {
|
|
198
194
|
const value = koatty_lib.Helper.toString(props[item]);
|
|
199
|
-
key += `:${value}`;
|
|
195
|
+
key += `:${opt.params[item]}:${value}`;
|
|
200
196
|
}
|
|
201
197
|
}
|
|
202
198
|
// 防止key超长
|
|
203
|
-
if (key.length >
|
|
199
|
+
if (key.length > longKey) {
|
|
204
200
|
key = koatty_lib.Helper.murmurHash(key);
|
|
205
201
|
}
|
|
206
202
|
}
|
|
207
203
|
if (opt.eventTime === "Before") {
|
|
208
|
-
await store.del(
|
|
204
|
+
await store.del(key).catch(() => null);
|
|
209
205
|
// tslint:disable-next-line: no-invalid-this
|
|
210
206
|
return value.apply(this, props);
|
|
211
207
|
}
|
|
212
208
|
else {
|
|
213
209
|
// tslint:disable-next-line: no-invalid-this
|
|
214
210
|
const res = await value.apply(this, props);
|
|
215
|
-
store.del(
|
|
211
|
+
store.del(key).catch(() => null);
|
|
216
212
|
return res;
|
|
217
213
|
}
|
|
218
214
|
}
|
|
@@ -229,31 +225,10 @@ function CacheEvict(cacheName, opt = {
|
|
|
229
225
|
}
|
|
230
226
|
/**
|
|
231
227
|
* @description:
|
|
232
|
-
* @param {
|
|
233
|
-
* @return {*}
|
|
234
|
-
*/
|
|
235
|
-
function getArgs(func) {
|
|
236
|
-
// 首先匹配函数括弧里的参数
|
|
237
|
-
const args = func.toString().match(/.*?\(([^)]*)\)/);
|
|
238
|
-
if (args.length > 1) {
|
|
239
|
-
// 分解参数成数组
|
|
240
|
-
return args[1].split(",").map(function (a) {
|
|
241
|
-
// 去空格和内联注释
|
|
242
|
-
return a.replace(/\/\*.*\*\//, "").trim();
|
|
243
|
-
}).filter(function (ae) {
|
|
244
|
-
// 确保没有undefineds
|
|
245
|
-
return ae;
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
return [];
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* @description:
|
|
252
|
-
* @param {string} params
|
|
253
|
-
* @param {string} args
|
|
228
|
+
* @param {string[]} params
|
|
254
229
|
* @return {*}
|
|
255
230
|
*/
|
|
256
|
-
function getParamIndex(params
|
|
231
|
+
function getParamIndex(params) {
|
|
257
232
|
const res = [];
|
|
258
233
|
for (let i = 0; i < params.length; i++) {
|
|
259
234
|
if (params.includes(params[i])) {
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*!
|
|
2
2
|
* @Author: richen
|
|
3
|
-
* @Date:
|
|
3
|
+
* @Date: 2024-01-17 22:08:01
|
|
4
4
|
* @License: BSD (3-Clause)
|
|
5
5
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
6
6
|
* @HomePage: https://koatty.org/
|
|
@@ -13,11 +13,11 @@ import { IOCContainer } from 'koatty_container';
|
|
|
13
13
|
/*
|
|
14
14
|
* @Author: richen
|
|
15
15
|
* @Date: 2020-07-06 19:53:43
|
|
16
|
-
* @LastEditTime:
|
|
16
|
+
* @LastEditTime: 2024-01-17 21:54:53
|
|
17
17
|
* @Description:
|
|
18
18
|
* @Copyright (c) - <richenlin(at)gmail.com>
|
|
19
19
|
*/
|
|
20
|
-
const
|
|
20
|
+
const longKey = 128;
|
|
21
21
|
// storeCache
|
|
22
22
|
const storeCache = {
|
|
23
23
|
store: null
|
|
@@ -76,7 +76,7 @@ async function InitCacheStore() {
|
|
|
76
76
|
*/
|
|
77
77
|
function CacheAble(cacheName, opt = {
|
|
78
78
|
params: [],
|
|
79
|
-
timeout:
|
|
79
|
+
timeout: 300,
|
|
80
80
|
}) {
|
|
81
81
|
return (target, methodName, descriptor) => {
|
|
82
82
|
const componentType = IOCContainer.getType(target);
|
|
@@ -87,11 +87,11 @@ function CacheAble(cacheName, opt = {
|
|
|
87
87
|
opt = {
|
|
88
88
|
...{
|
|
89
89
|
params: [],
|
|
90
|
-
timeout:
|
|
90
|
+
timeout: 300,
|
|
91
91
|
}, ...opt
|
|
92
92
|
};
|
|
93
93
|
// 获取定义的参数位置
|
|
94
|
-
const paramIndexes = getParamIndex(opt.params
|
|
94
|
+
const paramIndexes = getParamIndex(opt.params);
|
|
95
95
|
descriptor = {
|
|
96
96
|
configurable,
|
|
97
97
|
enumerable,
|
|
@@ -105,35 +105,31 @@ function CacheAble(cacheName, opt = {
|
|
|
105
105
|
});
|
|
106
106
|
if (cacheFlag) {
|
|
107
107
|
// tslint:disable-next-line: one-variable-per-declaration
|
|
108
|
-
let key =
|
|
108
|
+
let key = cacheName;
|
|
109
109
|
if (props && props.length > 0) {
|
|
110
110
|
for (const item of paramIndexes) {
|
|
111
111
|
if (props[item] !== undefined) {
|
|
112
112
|
const value = Helper.toString(props[item]);
|
|
113
|
-
key += `:${value}`;
|
|
113
|
+
key += `:${opt.params[item]}:${value}`;
|
|
114
114
|
}
|
|
115
115
|
}
|
|
116
116
|
// 防止key超长
|
|
117
|
-
if (key.length >
|
|
117
|
+
if (key.length > longKey) {
|
|
118
118
|
key = Helper.murmurHash(key);
|
|
119
119
|
}
|
|
120
120
|
}
|
|
121
|
-
let res = await store.get(
|
|
121
|
+
let res = await store.get(key).catch(() => null);
|
|
122
122
|
if (!Helper.isEmpty(res)) {
|
|
123
123
|
return JSON.parse(res);
|
|
124
124
|
}
|
|
125
125
|
// tslint:disable-next-line: no-invalid-this
|
|
126
126
|
res = await value.apply(this, props);
|
|
127
127
|
// prevent cache penetration
|
|
128
|
-
if (Helper.
|
|
128
|
+
if (Helper.isTrueEmpty(res)) {
|
|
129
129
|
res = "";
|
|
130
|
-
opt.timeout = 5;
|
|
131
|
-
}
|
|
132
|
-
if (!opt.timeout) {
|
|
133
|
-
opt.timeout = 3600;
|
|
134
130
|
}
|
|
135
131
|
// async set store
|
|
136
|
-
store.set(
|
|
132
|
+
store.set(key, JSON.stringify(res), opt.timeout).catch(() => null);
|
|
137
133
|
return res;
|
|
138
134
|
}
|
|
139
135
|
else {
|
|
@@ -176,7 +172,7 @@ function CacheEvict(cacheName, opt = {
|
|
|
176
172
|
eventTime: "Before",
|
|
177
173
|
}, ...opt
|
|
178
174
|
};
|
|
179
|
-
const paramIndexes = getParamIndex(opt.params
|
|
175
|
+
const paramIndexes = getParamIndex(opt.params);
|
|
180
176
|
descriptor = {
|
|
181
177
|
configurable,
|
|
182
178
|
enumerable,
|
|
@@ -189,28 +185,28 @@ function CacheEvict(cacheName, opt = {
|
|
|
189
185
|
return null;
|
|
190
186
|
});
|
|
191
187
|
if (cacheFlag) {
|
|
192
|
-
let key =
|
|
188
|
+
let key = cacheName;
|
|
193
189
|
if (props && props.length > 0) {
|
|
194
190
|
for (const item of paramIndexes) {
|
|
195
191
|
if (props[item] !== undefined) {
|
|
196
192
|
const value = Helper.toString(props[item]);
|
|
197
|
-
key += `:${value}`;
|
|
193
|
+
key += `:${opt.params[item]}:${value}`;
|
|
198
194
|
}
|
|
199
195
|
}
|
|
200
196
|
// 防止key超长
|
|
201
|
-
if (key.length >
|
|
197
|
+
if (key.length > longKey) {
|
|
202
198
|
key = Helper.murmurHash(key);
|
|
203
199
|
}
|
|
204
200
|
}
|
|
205
201
|
if (opt.eventTime === "Before") {
|
|
206
|
-
await store.del(
|
|
202
|
+
await store.del(key).catch(() => null);
|
|
207
203
|
// tslint:disable-next-line: no-invalid-this
|
|
208
204
|
return value.apply(this, props);
|
|
209
205
|
}
|
|
210
206
|
else {
|
|
211
207
|
// tslint:disable-next-line: no-invalid-this
|
|
212
208
|
const res = await value.apply(this, props);
|
|
213
|
-
store.del(
|
|
209
|
+
store.del(key).catch(() => null);
|
|
214
210
|
return res;
|
|
215
211
|
}
|
|
216
212
|
}
|
|
@@ -227,31 +223,10 @@ function CacheEvict(cacheName, opt = {
|
|
|
227
223
|
}
|
|
228
224
|
/**
|
|
229
225
|
* @description:
|
|
230
|
-
* @param {
|
|
231
|
-
* @return {*}
|
|
232
|
-
*/
|
|
233
|
-
function getArgs(func) {
|
|
234
|
-
// 首先匹配函数括弧里的参数
|
|
235
|
-
const args = func.toString().match(/.*?\(([^)]*)\)/);
|
|
236
|
-
if (args.length > 1) {
|
|
237
|
-
// 分解参数成数组
|
|
238
|
-
return args[1].split(",").map(function (a) {
|
|
239
|
-
// 去空格和内联注释
|
|
240
|
-
return a.replace(/\/\*.*\*\//, "").trim();
|
|
241
|
-
}).filter(function (ae) {
|
|
242
|
-
// 确保没有undefineds
|
|
243
|
-
return ae;
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
return [];
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* @description:
|
|
250
|
-
* @param {string} params
|
|
251
|
-
* @param {string} args
|
|
226
|
+
* @param {string[]} params
|
|
252
227
|
* @return {*}
|
|
253
228
|
*/
|
|
254
|
-
function getParamIndex(params
|
|
229
|
+
function getParamIndex(params) {
|
|
255
230
|
const res = [];
|
|
256
231
|
for (let i = 0; i < params.length; i++) {
|
|
257
232
|
if (params.includes(params[i])) {
|
package/dist/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koatty_cacheable",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Cacheable for koatty.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
|
|
10
10
|
"build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
|
|
11
11
|
"eslint": "eslint --ext .ts,.js ./",
|
|
12
|
+
"lock": "npm i --package-lock-only",
|
|
12
13
|
"prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
|
|
13
14
|
"prerelease": "npm test && npm run build",
|
|
14
15
|
"release": "standard-version",
|
|
@@ -58,6 +59,7 @@
|
|
|
58
59
|
"@rollup/plugin-json": "^6.x.x",
|
|
59
60
|
"@types/jest": "^29.x.x",
|
|
60
61
|
"@types/koa": "^2.x.x",
|
|
62
|
+
"@types/lodash": "^4.x.x",
|
|
61
63
|
"@types/node": "^18.x.x",
|
|
62
64
|
"@typescript-eslint/eslint-plugin": "^5.x.x",
|
|
63
65
|
"@typescript-eslint/parser": "^5.x.x",
|
|
@@ -74,18 +76,18 @@
|
|
|
74
76
|
"standard-version": "^9.x.x",
|
|
75
77
|
"ts-jest": "^29.x.x",
|
|
76
78
|
"ts-node": "^10.x.x",
|
|
79
|
+
"tslib": "^2.x.x",
|
|
77
80
|
"typescript": "^4.x.x"
|
|
78
81
|
},
|
|
79
82
|
"dependencies": {
|
|
80
83
|
"koatty_container": "^1.x.x",
|
|
81
84
|
"koatty_lib": "^1.x.x",
|
|
82
85
|
"koatty_logger": "^2.x.x",
|
|
83
|
-
"koatty_store": "^1.
|
|
86
|
+
"koatty_store": "^1.6.2"
|
|
84
87
|
},
|
|
85
88
|
"peerDependencies": {
|
|
86
89
|
"koatty_container": "^1.x.x",
|
|
87
90
|
"koatty_lib": "^1.x.x",
|
|
88
|
-
"koatty_logger": "^2.x.x"
|
|
89
|
-
"koatty_store": "^1.x.x"
|
|
91
|
+
"koatty_logger": "^2.x.x"
|
|
90
92
|
}
|
|
91
93
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koatty_cacheable",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Cacheable for koatty.",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "npm run build:js && npm run build:dts && npm run build:doc && npm run build:cp",
|
|
@@ -9,6 +9,7 @@
|
|
|
9
9
|
"build:doc": "del-cli --force docs/api && npx api-documenter markdown --input temp --output docs/api",
|
|
10
10
|
"build:dts": "del-cli --force temp && npx tsc && npx api-extractor run --local --verbose",
|
|
11
11
|
"eslint": "eslint --ext .ts,.js ./",
|
|
12
|
+
"lock": "npm i --package-lock-only",
|
|
12
13
|
"prepublishOnly": "npm test && npm run build && git push --follow-tags origin",
|
|
13
14
|
"prerelease": "npm test && npm run build",
|
|
14
15
|
"release": "standard-version",
|
|
@@ -58,6 +59,7 @@
|
|
|
58
59
|
"@rollup/plugin-json": "^6.x.x",
|
|
59
60
|
"@types/jest": "^29.x.x",
|
|
60
61
|
"@types/koa": "^2.x.x",
|
|
62
|
+
"@types/lodash": "^4.x.x",
|
|
61
63
|
"@types/node": "^18.x.x",
|
|
62
64
|
"@typescript-eslint/eslint-plugin": "^5.x.x",
|
|
63
65
|
"@typescript-eslint/parser": "^5.x.x",
|
|
@@ -74,18 +76,18 @@
|
|
|
74
76
|
"standard-version": "^9.x.x",
|
|
75
77
|
"ts-jest": "^29.x.x",
|
|
76
78
|
"ts-node": "^10.x.x",
|
|
79
|
+
"tslib": "^2.x.x",
|
|
77
80
|
"typescript": "^4.x.x"
|
|
78
81
|
},
|
|
79
82
|
"dependencies": {
|
|
80
83
|
"koatty_container": "^1.x.x",
|
|
81
84
|
"koatty_lib": "^1.x.x",
|
|
82
85
|
"koatty_logger": "^2.x.x",
|
|
83
|
-
"koatty_store": "^1.
|
|
86
|
+
"koatty_store": "^1.6.2"
|
|
84
87
|
},
|
|
85
88
|
"peerDependencies": {
|
|
86
89
|
"koatty_container": "^1.x.x",
|
|
87
90
|
"koatty_lib": "^1.x.x",
|
|
88
|
-
"koatty_logger": "^2.x.x"
|
|
89
|
-
"koatty_store": "^1.x.x"
|
|
91
|
+
"koatty_logger": "^2.x.x"
|
|
90
92
|
}
|
|
91
93
|
}
|