koatty_cacheable 1.3.6 → 1.3.8

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.
@@ -0,0 +1,63 @@
1
+ /*
2
+ * @Description:
3
+ * @Usage:
4
+ * @Author: richen
5
+ * @Date: 2021-12-17 10:20:44
6
+ * @LastEditTime: 2021-12-18 11:58:46
7
+ */
8
+ import json from "@rollup/plugin-json";
9
+ import typescript from 'rollup-plugin-typescript2';
10
+ // import babel from '@rollup/plugin-babel';
11
+
12
+ export default [
13
+ {
14
+ input: './src/index.ts',
15
+ output: [{
16
+ format: 'cjs',
17
+ file: './dist/index.js',
18
+ banner: require('./scripts/copyright')
19
+ }],
20
+ plugins: [
21
+ // babel({
22
+ // babelHelpers: "runtime",
23
+ // configFile: './babel.config.js',
24
+ // exclude: 'node_modules/**',
25
+ // }),
26
+ json(),
27
+ typescript({
28
+ tsconfigOverride: {
29
+ compilerOptions: {
30
+ declaration: false,
31
+ declarationMap: false,
32
+ module: "ESNext"
33
+ }
34
+ }
35
+ })
36
+ ]
37
+ },
38
+ {
39
+ input: './src/index.ts',
40
+ output: [{
41
+ format: 'es',
42
+ file: './dist/index.mjs',
43
+ banner: require('./scripts/copyright')
44
+ }],
45
+ plugins: [
46
+ // babel({
47
+ // babelHelpers: "runtime",
48
+ // configFile: './babel.config.js',
49
+ // exclude: 'node_modules/**',
50
+ // }),
51
+ json(),
52
+ typescript({
53
+ tsconfigOverride: {
54
+ compilerOptions: {
55
+ declaration: false,
56
+ declarationMap: false,
57
+ module: "ESNext"
58
+ }
59
+ }
60
+ })
61
+ ]
62
+ }
63
+ ]
package/CHANGELOG.md CHANGED
@@ -2,8 +2,6 @@
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.3.6](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.4...v1.3.6) (2021-12-02)
5
+ ### [1.3.8](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.8) (2023-01-13)
6
6
 
7
- ### [1.3.4](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.2...v1.3.4) (2021-11-23)
8
-
9
- ### [1.3.2](https://github.com/thinkkoa/koatty_cacheable/compare/v1.2.12...v1.3.2) (2021-11-20)
7
+ ### [1.3.7](https://github.com/thinkkoa/koatty_cacheable/compare/v1.3.6...v1.3.7) (2022-05-27)
package/dist/LICENSE ADDED
@@ -0,0 +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.
package/dist/README.md ADDED
@@ -0,0 +1,57 @@
1
+ # koatty_cacheable
2
+ Cacheable for koatty.
3
+
4
+ Koatty框架的 CacheAble, Cacheable, CacheEvict 支持库
5
+
6
+
7
+ # Usage
8
+
9
+ db.ts in koatty project:
10
+
11
+ ```js
12
+ export default {
13
+ ...
14
+
15
+ "CacheStore": {
16
+ type: "memory", // redis or memory, memory is default
17
+ // key_prefix: "koatty",
18
+ // host: '127.0.0.1',
19
+ // port: 6379,
20
+ // name: "",
21
+ // username: "",
22
+ // password: "",
23
+ // db: 0,
24
+ // timeout: 30,
25
+ // pool_size: 10,
26
+ // conn_timeout: 30
27
+ },
28
+
29
+ ...
30
+ };
31
+
32
+ ```
33
+
34
+ used in service:
35
+
36
+ ```js
37
+ import { CacheAble, CacheEvict, GetCacheStore } from "koatty_cacheable";
38
+
39
+ export class TestService {
40
+
41
+ @CacheAble("testCache") // auto cached
42
+ getTest(){
43
+ //todo
44
+ }
45
+
46
+ @CacheEvict("testCache") // auto clear cache
47
+ setTest(){
48
+ //todo
49
+ }
50
+
51
+ test(){
52
+ const store = GetCacheStore(this.app);
53
+ store.set(key, value);
54
+ }
55
+ }
56
+
57
+ ```
package/dist/index.d.ts CHANGED
@@ -1,34 +1,47 @@
1
- import { CacheStore } from "koatty_store";
2
- import { Application } from 'koatty_container';
3
- /**
4
- * get instances of cacheStore
5
- *
6
- * @export
7
- * @param {Application} app
8
- * @returns {*} {CacheStore}
9
- */
10
- export declare function GetCacheStore(app: Application): Promise<CacheStore>;
11
- /**
12
- * Decorate this method to support caching. Redis server config from db.ts.
13
- * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
14
- * the results can be obtained directly from the cache without the need to execute the method again.
15
- *
16
- * @export
17
- * @param {string} cacheName cache name
18
- * @param {number} [timeout=3600] cache timeout
19
- * @returns {MethodDecorator}
20
- */
21
- export declare function CacheAble(cacheName: string, timeout?: number): MethodDecorator;
22
- /**
23
- *
24
- */
25
- export declare type eventTimes = "Before" | "After";
26
- /**
27
- * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
28
- *
29
- * @export
30
- * @param {string} cacheName cacheName cache name
31
- * @param {eventTimes} [eventTime="Before"]
32
- * @returns
33
- */
34
- export declare function CacheEvict(cacheName: string, eventTime?: eventTimes): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
1
+ /*!
2
+ * @Author: richen
3
+ * @Date: 2023-01-13 14:17:21
4
+ * @License: BSD (3-Clause)
5
+ * @Copyright (c) - <richenlin(at)gmail.com>
6
+ * @HomePage: https://koatty.org/
7
+ */
8
+ import { Application } from 'koatty_container';
9
+ import { CacheStore } from 'koatty_store';
10
+
11
+ /**
12
+ * Decorate this method to support caching. Redis server config from db.ts.
13
+ * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
14
+ * the results can be obtained directly from the cache without the need to execute the method again.
15
+ *
16
+ * @export
17
+ * @param {string} cacheName cache name
18
+ * @param {number} [timeout=3600] cache timeout
19
+ * @returns {MethodDecorator}
20
+ */
21
+ export declare function CacheAble(cacheName: string, timeout?: number): MethodDecorator;
22
+
23
+ /**
24
+ * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
25
+ *
26
+ * @export
27
+ * @param {string} cacheName cacheName cache name
28
+ * @param {eventTimes} [eventTime="Before"]
29
+ * @returns
30
+ */
31
+ export declare function CacheEvict(cacheName: string, eventTime?: eventTimes): (target: any, methodName: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
32
+
33
+ /**
34
+ *
35
+ */
36
+ export declare type eventTimes = "Before" | "After";
37
+
38
+ /**
39
+ * get instances of cacheStore
40
+ *
41
+ * @export
42
+ * @param {Application} app
43
+ * @returns {*} {CacheStore}
44
+ */
45
+ export declare function GetCacheStore(app: Application): Promise<CacheStore>;
46
+
47
+ export { }
package/dist/index.js CHANGED
@@ -1,171 +1,196 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CacheEvict = exports.CacheAble = exports.GetCacheStore = void 0;
4
- const tslib_1 = require("tslib");
5
- /*
6
- * @Author: richen
7
- * @Date: 2020-07-06 19:53:43
8
- * @LastEditTime: 2021-12-02 16:20:52
9
- * @Description:
10
- * @Copyright (c) - <richenlin(at)gmail.com>
11
- */
12
- const helper = (0, tslib_1.__importStar)(require("koatty_lib"));
13
- const koatty_logger_1 = require("koatty_logger");
14
- const koatty_store_1 = require("koatty_store");
15
- const koatty_container_1 = require("koatty_container");
16
- // cacheStore
17
- const cacheStore = {
18
- store: null
19
- };
20
- /**
21
- * get instances of cacheStore
22
- *
23
- * @export
24
- * @param {Application} app
25
- * @returns {*} {CacheStore}
26
- */
27
- async function GetCacheStore(app) {
28
- var _a;
29
- if (cacheStore.store && cacheStore.store.getConnection) {
30
- return cacheStore.store;
31
- }
32
- const opt = (_a = app.config("CacheStore", "db")) !== null && _a !== void 0 ? _a : {};
33
- if (helper.isEmpty(opt)) {
34
- koatty_logger_1.DefaultLogger.Warn(`Missing CacheStore server configuration. Please write a configuration item with the key name 'CacheStore' in the db.ts file.`);
35
- }
36
- cacheStore.store = koatty_store_1.Store.getInstance(opt);
37
- if (!helper.isFunction(cacheStore.store.getConnection)) {
38
- throw Error(`CacheStore connection failed. `);
39
- }
40
- return cacheStore.store;
41
- }
42
- exports.GetCacheStore = GetCacheStore;
43
- /**
44
- * initiation CacheStore connection and client.
45
- *
46
- */
47
- async function InitCacheStore() {
48
- const app = koatty_container_1.IOCContainer.getApp();
49
- app && app.once("appStart", async function () {
50
- await GetCacheStore(app);
1
+ /*!
2
+ * @Author: richen
3
+ * @Date: 2023-01-13 14:17:07
4
+ * @License: BSD (3-Clause)
5
+ * @Copyright (c) - <richenlin(at)gmail.com>
6
+ * @HomePage: https://koatty.org/
7
+ */
8
+ 'use strict';
9
+
10
+ var helper = require('koatty_lib');
11
+ var koatty_logger = require('koatty_logger');
12
+ var koatty_store = require('koatty_store');
13
+ var koatty_container = require('koatty_container');
14
+
15
+ function _interopNamespaceDefault(e) {
16
+ var n = Object.create(null);
17
+ if (e) {
18
+ Object.keys(e).forEach(function (k) {
19
+ if (k !== 'default') {
20
+ var d = Object.getOwnPropertyDescriptor(e, k);
21
+ Object.defineProperty(n, k, d.get ? d : {
22
+ enumerable: true,
23
+ get: function () { return e[k]; }
24
+ });
25
+ }
51
26
  });
27
+ }
28
+ n.default = e;
29
+ return Object.freeze(n);
52
30
  }
53
- /**
54
- * Decorate this method to support caching. Redis server config from db.ts.
55
- * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
56
- * the results can be obtained directly from the cache without the need to execute the method again.
57
- *
58
- * @export
59
- * @param {string} cacheName cache name
60
- * @param {number} [timeout=3600] cache timeout
61
- * @returns {MethodDecorator}
62
- */
63
- function CacheAble(cacheName, timeout = 3600) {
64
- return (target, methodName, descriptor) => {
65
- const componentType = koatty_container_1.IOCContainer.getType(target);
66
- if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
67
- throw Error("This decorator only used in the service、component class.");
68
- }
69
- let identifier = koatty_container_1.IOCContainer.getIdentifier(target);
70
- identifier = identifier || (target.constructor ? (target.constructor.name || "") : "");
71
- const { value, configurable, enumerable } = descriptor;
72
- descriptor = {
73
- configurable,
74
- enumerable,
75
- writable: true,
76
- async value(...props) {
77
- let cacheFlag = true;
78
- const store = await GetCacheStore(this.app).catch(() => {
79
- cacheFlag = false;
80
- koatty_logger_1.DefaultLogger.Error("Get cache store instance failed.");
81
- return null;
82
- });
83
- if (cacheFlag) {
84
- // tslint:disable-next-line: one-variable-per-declaration
85
- let key = "", res;
86
- if (props && props.length > 0) {
87
- key = `${identifier}:${methodName}:${helper.murmurHash(JSON.stringify(props))}`;
88
- }
89
- else {
90
- key = `${identifier}:${methodName}`;
91
- }
92
- res = await store.hget(cacheName, key).catch(() => null);
93
- if (!helper.isEmpty(res)) {
94
- return JSON.parse(res);
95
- }
96
- // tslint:disable-next-line: no-invalid-this
97
- res = await value.apply(this, props);
98
- // prevent cache penetration
99
- if (helper.isEmpty(res)) {
100
- res = "";
101
- timeout = 60;
102
- }
103
- // async set store
104
- store.hset(cacheName, key, JSON.stringify(res), timeout).catch(() => null);
105
- return res;
106
- }
107
- else {
108
- // tslint:disable-next-line: no-invalid-this
109
- return value.apply(this, props);
110
- }
111
- }
112
- };
113
- // bind app_ready hook event
114
- InitCacheStore();
115
- return descriptor;
116
- };
31
+
32
+ var helper__namespace = /*#__PURE__*/_interopNamespaceDefault(helper);
33
+
34
+ /*
35
+ * @Author: richen
36
+ * @Date: 2020-07-06 19:53:43
37
+ * @LastEditTime: 2023-01-13 14:16:02
38
+ * @Description:
39
+ * @Copyright (c) - <richenlin(at)gmail.com>
40
+ */
41
+ // cacheStore
42
+ const cacheStore = {
43
+ store: null
44
+ };
45
+ /**
46
+ * get instances of cacheStore
47
+ *
48
+ * @export
49
+ * @param {Application} app
50
+ * @returns {*} {CacheStore}
51
+ */
52
+ async function GetCacheStore(app) {
53
+ var _a;
54
+ if (cacheStore.store && cacheStore.store.getConnection) {
55
+ return cacheStore.store;
56
+ }
57
+ const opt = (_a = app.config("CacheStore", "db")) !== null && _a !== void 0 ? _a : {};
58
+ if (helper__namespace.isEmpty(opt)) {
59
+ koatty_logger.DefaultLogger.Warn(`Missing CacheStore server configuration. Please write a configuration item with the key name 'CacheStore' in the db.ts file.`);
60
+ }
61
+ cacheStore.store = koatty_store.Store.getInstance(opt);
62
+ if (!helper__namespace.isFunction(cacheStore.store.getConnection)) {
63
+ throw Error(`CacheStore connection failed. `);
64
+ }
65
+ return cacheStore.store;
66
+ }
67
+ /**
68
+ * initiation CacheStore connection and client.
69
+ *
70
+ */
71
+ async function InitCacheStore() {
72
+ const app = koatty_container.IOCContainer.getApp();
73
+ app && app.once("appStart", async function () {
74
+ await GetCacheStore(app);
75
+ });
76
+ }
77
+ /**
78
+ * Decorate this method to support caching. Redis server config from db.ts.
79
+ * The cache method returns a value to ensure that the next time the method is executed with the same parameters,
80
+ * the results can be obtained directly from the cache without the need to execute the method again.
81
+ *
82
+ * @export
83
+ * @param {string} cacheName cache name
84
+ * @param {number} [timeout=3600] cache timeout
85
+ * @returns {MethodDecorator}
86
+ */
87
+ function CacheAble(cacheName, timeout = 3600) {
88
+ return (target, methodName, descriptor) => {
89
+ const componentType = koatty_container.IOCContainer.getType(target);
90
+ if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
91
+ throw Error("This decorator only used in the service、component class.");
92
+ }
93
+ let identifier = koatty_container.IOCContainer.getIdentifier(target);
94
+ identifier = identifier || (target.constructor ? (target.constructor.name || "") : "");
95
+ const { value, configurable, enumerable } = descriptor;
96
+ descriptor = {
97
+ configurable,
98
+ enumerable,
99
+ writable: true,
100
+ async value(...props) {
101
+ let cacheFlag = true;
102
+ const store = await GetCacheStore(this.app).catch(() => {
103
+ cacheFlag = false;
104
+ koatty_logger.DefaultLogger.Error("Get cache store instance failed.");
105
+ return null;
106
+ });
107
+ if (cacheFlag) {
108
+ // tslint:disable-next-line: one-variable-per-declaration
109
+ let key = "", res;
110
+ if (props && props.length > 0) {
111
+ key = `${identifier}:${methodName}:${helper__namespace.murmurHash(JSON.stringify(props))}`;
112
+ }
113
+ else {
114
+ key = `${identifier}:${methodName}`;
115
+ }
116
+ res = await store.hget(cacheName, key).catch(() => null);
117
+ if (!helper__namespace.isEmpty(res)) {
118
+ return JSON.parse(res);
119
+ }
120
+ // tslint:disable-next-line: no-invalid-this
121
+ res = await value.apply(this, props);
122
+ // prevent cache penetration
123
+ if (helper__namespace.isEmpty(res)) {
124
+ res = "";
125
+ timeout = 60;
126
+ }
127
+ // async set store
128
+ store.hset(cacheName, key, JSON.stringify(res), timeout).catch(() => null);
129
+ return res;
130
+ }
131
+ else {
132
+ // tslint:disable-next-line: no-invalid-this
133
+ return value.apply(this, props);
134
+ }
135
+ }
136
+ };
137
+ // bind app_ready hook event
138
+ InitCacheStore();
139
+ return descriptor;
140
+ };
141
+ }
142
+ /**
143
+ * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
144
+ *
145
+ * @export
146
+ * @param {string} cacheName cacheName cache name
147
+ * @param {eventTimes} [eventTime="Before"]
148
+ * @returns
149
+ */
150
+ function CacheEvict(cacheName, eventTime = "Before") {
151
+ return (target, methodName, descriptor) => {
152
+ const componentType = koatty_container.IOCContainer.getType(target);
153
+ if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
154
+ throw Error("This decorator only used in the service、component class.");
155
+ }
156
+ koatty_container.IOCContainer.getIdentifier(target);
157
+ const { value, configurable, enumerable } = descriptor;
158
+ descriptor = {
159
+ configurable,
160
+ enumerable,
161
+ writable: true,
162
+ async value(...props) {
163
+ let cacheFlag = true;
164
+ const store = await GetCacheStore(this.app).catch(() => {
165
+ cacheFlag = false;
166
+ koatty_logger.DefaultLogger.Error("Get cache store instance failed.");
167
+ return null;
168
+ });
169
+ if (cacheFlag) {
170
+ if (eventTime === "Before") {
171
+ await store.del(cacheName).catch(() => null);
172
+ // tslint:disable-next-line: no-invalid-this
173
+ return value.apply(this, props);
174
+ }
175
+ else {
176
+ // tslint:disable-next-line: no-invalid-this
177
+ const res = await value.apply(this, props);
178
+ store.del(cacheName).catch(() => null);
179
+ return res;
180
+ }
181
+ }
182
+ else {
183
+ // tslint:disable-next-line: no-invalid-this
184
+ return value.apply(this, props);
185
+ }
186
+ }
187
+ };
188
+ // bind app_ready hook event
189
+ InitCacheStore();
190
+ return descriptor;
191
+ };
117
192
  }
193
+
118
194
  exports.CacheAble = CacheAble;
119
- /**
120
- * Decorating the execution of this method will trigger a cache clear operation. Redis server config from db.ts.
121
- *
122
- * @export
123
- * @param {string} cacheName cacheName cache name
124
- * @param {eventTimes} [eventTime="Before"]
125
- * @returns
126
- */
127
- function CacheEvict(cacheName, eventTime = "Before") {
128
- return (target, methodName, descriptor) => {
129
- const componentType = koatty_container_1.IOCContainer.getType(target);
130
- if (componentType !== "SERVICE" && componentType !== "COMPONENT") {
131
- throw Error("This decorator only used in the service、component class.");
132
- }
133
- const identifier = koatty_container_1.IOCContainer.getIdentifier(target);
134
- const { value, configurable, enumerable } = descriptor;
135
- descriptor = {
136
- configurable,
137
- enumerable,
138
- writable: true,
139
- async value(...props) {
140
- let cacheFlag = true;
141
- const store = await GetCacheStore(this.app).catch(() => {
142
- cacheFlag = false;
143
- koatty_logger_1.DefaultLogger.Error("Get cache store instance failed.");
144
- return null;
145
- });
146
- if (cacheFlag) {
147
- if (eventTime === "Before") {
148
- await store.del(cacheName).catch(() => null);
149
- // tslint:disable-next-line: no-invalid-this
150
- return value.apply(this, props);
151
- }
152
- else {
153
- // tslint:disable-next-line: no-invalid-this
154
- const res = await value.apply(this, props);
155
- store.del(cacheName).catch(() => null);
156
- return res;
157
- }
158
- }
159
- else {
160
- // tslint:disable-next-line: no-invalid-this
161
- return value.apply(this, props);
162
- }
163
- }
164
- };
165
- // bind app_ready hook event
166
- InitCacheStore();
167
- return descriptor;
168
- };
169
- }
170
195
  exports.CacheEvict = CacheEvict;
171
- //# sourceMappingURL=index.js.map
196
+ exports.GetCacheStore = GetCacheStore;