@seidor-cloud-produtos/orbit-backend-lib 0.0.94 → 0.0.96
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/dist/clean-arch/application/cache/cache.d.ts +18 -8
- package/dist/clean-arch/application/cache/cache.js +93 -18
- package/dist/clean-arch/application/cache/client.d.ts +1 -0
- package/dist/clean-arch/infra/cache/clients/node-cache.d.ts +1 -0
- package/dist/clean-arch/infra/cache/clients/node-cache.js +3 -0
- package/dist/clean-arch/infra/cache/clients/redis.d.ts +4 -5
- package/dist/clean-arch/infra/cache/clients/redis.js +30 -49
- package/package.json +1 -1
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
import { CacheClient } from './client';
|
|
2
|
+
export type MethodOptions = {
|
|
3
|
+
throwOnError?: boolean;
|
|
4
|
+
};
|
|
5
|
+
export type CacheOptions = {
|
|
6
|
+
throwOnError?: boolean;
|
|
7
|
+
};
|
|
2
8
|
export declare abstract class Cache {
|
|
3
9
|
protected abstract client: CacheClient;
|
|
4
10
|
protected DEFAULT_EXPIRATION_SECONDS: number;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
private options?;
|
|
12
|
+
constructor(options?: CacheOptions);
|
|
13
|
+
private setOptions;
|
|
14
|
+
start(options?: MethodOptions): Promise<void>;
|
|
15
|
+
get(keyCache: string, options?: MethodOptions): Promise<any>;
|
|
16
|
+
set(keyCache: string, data: any, expiration?: number, options?: MethodOptions): Promise<void>;
|
|
17
|
+
del(keyCache: string, options?: MethodOptions): Promise<void>;
|
|
18
|
+
getKeys(pattern?: string, options?: MethodOptions): Promise<string[] | null>;
|
|
19
|
+
delBatch(patterns: string[], options?: MethodOptions): Promise<void>;
|
|
20
|
+
flush(): Promise<void>;
|
|
21
|
+
isRunning(options?: MethodOptions): Promise<any>;
|
|
22
|
+
close(options?: MethodOptions): Promise<void>;
|
|
13
23
|
}
|
|
@@ -3,41 +3,116 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Cache = void 0;
|
|
4
4
|
const env_1 = require("../../infra/environment/env");
|
|
5
5
|
class Cache {
|
|
6
|
-
constructor() {
|
|
6
|
+
constructor(options) {
|
|
7
7
|
this.DEFAULT_EXPIRATION_SECONDS = env_1.env.DEFAULT_CACHE_EXPIRATION;
|
|
8
|
+
if (options)
|
|
9
|
+
this.setOptions(options);
|
|
8
10
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
setOptions(options) {
|
|
12
|
+
this.options = {
|
|
13
|
+
...options,
|
|
14
|
+
throwOnError: options.throwOnError === undefined ? true : options.throwOnError,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
async start(options) {
|
|
18
|
+
try {
|
|
19
|
+
await this.client.start();
|
|
20
|
+
}
|
|
21
|
+
catch (e) {
|
|
22
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
11
27
|
if (process.env.NODE_ENV !== 'test') {
|
|
12
28
|
console.log('\n');
|
|
13
29
|
console.log('💾 Cache Connected');
|
|
14
30
|
}
|
|
15
31
|
}
|
|
16
|
-
async get(keyCache) {
|
|
17
|
-
|
|
18
|
-
|
|
32
|
+
async get(keyCache, options) {
|
|
33
|
+
try {
|
|
34
|
+
const dataFromCache = await this.client.get(keyCache);
|
|
35
|
+
return dataFromCache ? dataFromCache : null;
|
|
36
|
+
}
|
|
37
|
+
catch (e) {
|
|
38
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
39
|
+
throw e;
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
19
43
|
}
|
|
20
|
-
async set(keyCache, data, expiration = this.DEFAULT_EXPIRATION_SECONDS) {
|
|
21
|
-
|
|
44
|
+
async set(keyCache, data, expiration = this.DEFAULT_EXPIRATION_SECONDS, options) {
|
|
45
|
+
try {
|
|
46
|
+
return await this.client.set(keyCache, JSON.stringify(data), expiration);
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
50
|
+
throw e;
|
|
51
|
+
}
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
async del(keyCache, options) {
|
|
56
|
+
try {
|
|
57
|
+
return await this.client.del(keyCache);
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
22
65
|
}
|
|
23
|
-
async
|
|
24
|
-
|
|
66
|
+
async getKeys(pattern, options) {
|
|
67
|
+
try {
|
|
68
|
+
return await this.client.getKeys(pattern);
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
72
|
+
throw e;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
25
76
|
}
|
|
26
|
-
async
|
|
27
|
-
|
|
77
|
+
async delBatch(patterns, options) {
|
|
78
|
+
try {
|
|
79
|
+
return await this.client.delBatch(patterns);
|
|
80
|
+
}
|
|
81
|
+
catch (e) {
|
|
82
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
28
87
|
}
|
|
29
|
-
async
|
|
30
|
-
return await this.client.
|
|
88
|
+
async flush() {
|
|
89
|
+
return await this.client.flush();
|
|
31
90
|
}
|
|
32
|
-
async isRunning() {
|
|
33
|
-
|
|
91
|
+
async isRunning(options) {
|
|
92
|
+
try {
|
|
93
|
+
return await this.client.isRunning();
|
|
94
|
+
}
|
|
95
|
+
catch (e) {
|
|
96
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
97
|
+
throw e;
|
|
98
|
+
}
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
34
101
|
}
|
|
35
|
-
async close() {
|
|
102
|
+
async close(options) {
|
|
36
103
|
if (process.env.NODE_ENV !== 'test') {
|
|
37
104
|
console.log('\n');
|
|
38
105
|
console.log('💾 Cache closed');
|
|
39
106
|
}
|
|
40
|
-
|
|
107
|
+
try {
|
|
108
|
+
return await this.client.close();
|
|
109
|
+
}
|
|
110
|
+
catch (e) {
|
|
111
|
+
if (options?.throwOnError || this.options?.throwOnError) {
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
41
116
|
}
|
|
42
117
|
}
|
|
43
118
|
exports.Cache = Cache;
|
|
@@ -6,5 +6,6 @@ export declare abstract class CacheClient {
|
|
|
6
6
|
abstract del(keyCache: string): Promise<void>;
|
|
7
7
|
abstract delBatch(keyCache: string[]): Promise<void>;
|
|
8
8
|
abstract getKeys(pattern?: string): Promise<string[] | null>;
|
|
9
|
+
abstract flush(): Promise<void>;
|
|
9
10
|
isRunning(): Promise<any>;
|
|
10
11
|
}
|
|
@@ -9,6 +9,7 @@ export declare class NodeCache extends CacheClient {
|
|
|
9
9
|
delBatch(keyCache: string[]): Promise<void>;
|
|
10
10
|
getKeys(pattern?: string | undefined): Promise<string[]>;
|
|
11
11
|
close(): Promise<void>;
|
|
12
|
+
flush(): Promise<void>;
|
|
12
13
|
}
|
|
13
14
|
declare const _default: NodeCache;
|
|
14
15
|
export default _default;
|
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { RedisOptions } from 'ioredis';
|
|
2
2
|
import { CacheClient } from '../../../application/cache/client';
|
|
3
|
-
export type RedisOptionsType = RedisOptions & {
|
|
4
|
-
throwOnError?: boolean;
|
|
5
|
-
};
|
|
6
3
|
export declare class Redis extends CacheClient {
|
|
7
|
-
protected
|
|
4
|
+
protected connection: RedisOptions;
|
|
8
5
|
private client;
|
|
9
|
-
constructor(
|
|
6
|
+
constructor(connection: RedisOptions);
|
|
7
|
+
private static delayOperation;
|
|
10
8
|
start(): Promise<void>;
|
|
11
9
|
get(key: string): Promise<any>;
|
|
12
10
|
set(key: string, data: string, expiration?: number): Promise<void>;
|
|
13
11
|
del(keyCache: string): Promise<void>;
|
|
14
12
|
delBatch(keyCache: string[]): Promise<void>;
|
|
15
13
|
getKeys(pattern?: string | undefined): Promise<string[] | null>;
|
|
14
|
+
flush(): Promise<void>;
|
|
16
15
|
close(): Promise<void>;
|
|
17
16
|
}
|
|
@@ -5,76 +5,57 @@ const tslib_1 = require("tslib");
|
|
|
5
5
|
const ioredis_1 = tslib_1.__importDefault(require("ioredis"));
|
|
6
6
|
const client_1 = require("../../../application/cache/client");
|
|
7
7
|
class Redis extends client_1.CacheClient {
|
|
8
|
-
constructor(
|
|
8
|
+
constructor(connection) {
|
|
9
9
|
super();
|
|
10
|
-
this.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
this.connection = connection;
|
|
11
|
+
}
|
|
12
|
+
static async delayOperation() {
|
|
13
|
+
const maxDelayMs = 5000;
|
|
14
|
+
await new Promise(resolve => setTimeout(resolve, maxDelayMs));
|
|
15
|
+
return null;
|
|
15
16
|
}
|
|
16
17
|
async start() {
|
|
17
|
-
this.client = new ioredis_1.default(this.
|
|
18
|
+
this.client = new ioredis_1.default(this.connection);
|
|
18
19
|
}
|
|
19
20
|
async get(key) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
catch (e) {
|
|
24
|
-
if (this.options.throwOnError === false) {
|
|
21
|
+
const get = async () => {
|
|
22
|
+
const data = await this.client.get(key);
|
|
23
|
+
if (!data)
|
|
25
24
|
return null;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
25
|
+
return JSON.parse(data);
|
|
26
|
+
};
|
|
27
|
+
// If redis not connected the get method never returns
|
|
28
|
+
return await Promise.race([get(), Redis.delayOperation()]);
|
|
29
29
|
}
|
|
30
30
|
async set(key, data, expiration) {
|
|
31
|
-
|
|
31
|
+
const set = async () => {
|
|
32
32
|
await this.client.set(key, data);
|
|
33
33
|
if (expiration) {
|
|
34
34
|
await this.client.expire(key, expiration);
|
|
35
35
|
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return;
|
|
40
|
-
}
|
|
41
|
-
throw e;
|
|
42
|
-
}
|
|
36
|
+
};
|
|
37
|
+
// If redis not connected the set method never returns
|
|
38
|
+
await Promise.race([set(), Redis.delayOperation()]);
|
|
43
39
|
}
|
|
44
40
|
async del(keyCache) {
|
|
45
|
-
|
|
46
|
-
await this.client.del(keyCache);
|
|
47
|
-
}
|
|
48
|
-
catch (e) {
|
|
49
|
-
if (this.options.throwOnError === false) {
|
|
50
|
-
return;
|
|
51
|
-
}
|
|
52
|
-
throw e;
|
|
53
|
-
}
|
|
41
|
+
await Promise.race([this.client.del(keyCache), Redis.delayOperation()]);
|
|
54
42
|
}
|
|
55
43
|
async delBatch(keyCache) {
|
|
56
|
-
|
|
44
|
+
const delBatch = async () => {
|
|
57
45
|
const pipeline = this.client.pipeline();
|
|
58
46
|
keyCache.forEach(key => pipeline.del(key));
|
|
59
47
|
await pipeline.exec();
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (this.options.throwOnError === false) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
throw e;
|
|
66
|
-
}
|
|
48
|
+
};
|
|
49
|
+
await Promise.race([delBatch(), Redis.delayOperation()]);
|
|
67
50
|
}
|
|
68
51
|
async getKeys(pattern) {
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
throw e;
|
|
77
|
-
}
|
|
52
|
+
return await Promise.race([
|
|
53
|
+
this.client.keys(pattern || '*'),
|
|
54
|
+
Redis.delayOperation(),
|
|
55
|
+
]);
|
|
56
|
+
}
|
|
57
|
+
async flush() {
|
|
58
|
+
await this.client.flushdb();
|
|
78
59
|
}
|
|
79
60
|
async close() {
|
|
80
61
|
await this.client.quit();
|