@sebspark/promise-cache 1.2.4 → 1.4.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/dist/index.d.mts +16 -11
- package/dist/index.d.ts +16 -11
- package/dist/index.js +32 -22
- package/dist/index.mjs +32 -22
- package/package.json +3 -2
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createClient, RedisClientOptions } from 'redis';
|
|
2
|
+
export { RedisClientOptions } from 'redis';
|
|
2
3
|
|
|
3
4
|
type GetType<T> = {
|
|
4
5
|
value: T;
|
|
@@ -12,14 +13,17 @@ type SetParams<T> = {
|
|
|
12
13
|
};
|
|
13
14
|
type PersistorConstructorType = {
|
|
14
15
|
redis?: RedisClientOptions;
|
|
15
|
-
onError?: () => void;
|
|
16
|
-
onSuccess?: () => void;
|
|
16
|
+
onError?: (c: string) => void;
|
|
17
|
+
onSuccess?: (c: string) => void;
|
|
17
18
|
};
|
|
18
19
|
declare class Persistor {
|
|
19
20
|
client: ReturnType<typeof createClient> | null;
|
|
21
|
+
private onError;
|
|
22
|
+
private onSuccess;
|
|
20
23
|
private readonly redis?;
|
|
21
24
|
constructor(options: PersistorConstructorType);
|
|
22
|
-
connect(
|
|
25
|
+
connect(): Promise<void>;
|
|
26
|
+
startConnection(): Promise<unknown>;
|
|
23
27
|
size(): Promise<number>;
|
|
24
28
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
25
29
|
private createOptions;
|
|
@@ -33,6 +37,13 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
|
33
37
|
}) => Persistor;
|
|
34
38
|
declare const clean: () => void;
|
|
35
39
|
|
|
40
|
+
type PromiseCacheOptions = {
|
|
41
|
+
ttlInSeconds?: number;
|
|
42
|
+
caseSensitive?: boolean;
|
|
43
|
+
redis?: RedisClientOptions;
|
|
44
|
+
onError?: () => void;
|
|
45
|
+
onSuccess?: () => void;
|
|
46
|
+
};
|
|
36
47
|
declare class PromiseCache<U> {
|
|
37
48
|
persistor: Persistor;
|
|
38
49
|
private readonly caseSensitive;
|
|
@@ -42,13 +53,7 @@ declare class PromiseCache<U> {
|
|
|
42
53
|
* @param ttlInSeconds Default cache TTL.
|
|
43
54
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
44
55
|
*/
|
|
45
|
-
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }:
|
|
46
|
-
ttlInSeconds?: number;
|
|
47
|
-
caseSensitive?: boolean;
|
|
48
|
-
redis?: RedisClientOptions;
|
|
49
|
-
onError?: () => void;
|
|
50
|
-
onSuccess?: () => void;
|
|
51
|
-
});
|
|
56
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: PromiseCacheOptions);
|
|
52
57
|
/**
|
|
53
58
|
* Cache size.
|
|
54
59
|
* @returns The number of entries in the cache.
|
|
@@ -90,4 +95,4 @@ declare class LocalStorage {
|
|
|
90
95
|
}
|
|
91
96
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
92
97
|
|
|
93
|
-
export { LocalStorage, Persistor, PromiseCache, clean, createLocalMemoryClient, createPersistor };
|
|
98
|
+
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, clean, createLocalMemoryClient, createPersistor };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createClient, RedisClientOptions } from 'redis';
|
|
2
|
+
export { RedisClientOptions } from 'redis';
|
|
2
3
|
|
|
3
4
|
type GetType<T> = {
|
|
4
5
|
value: T;
|
|
@@ -12,14 +13,17 @@ type SetParams<T> = {
|
|
|
12
13
|
};
|
|
13
14
|
type PersistorConstructorType = {
|
|
14
15
|
redis?: RedisClientOptions;
|
|
15
|
-
onError?: () => void;
|
|
16
|
-
onSuccess?: () => void;
|
|
16
|
+
onError?: (c: string) => void;
|
|
17
|
+
onSuccess?: (c: string) => void;
|
|
17
18
|
};
|
|
18
19
|
declare class Persistor {
|
|
19
20
|
client: ReturnType<typeof createClient> | null;
|
|
21
|
+
private onError;
|
|
22
|
+
private onSuccess;
|
|
20
23
|
private readonly redis?;
|
|
21
24
|
constructor(options: PersistorConstructorType);
|
|
22
|
-
connect(
|
|
25
|
+
connect(): Promise<void>;
|
|
26
|
+
startConnection(): Promise<unknown>;
|
|
23
27
|
size(): Promise<number>;
|
|
24
28
|
get<T>(key: string): Promise<GetType<T> | null>;
|
|
25
29
|
private createOptions;
|
|
@@ -33,6 +37,13 @@ declare const createPersistor: ({ redis, onError, onSuccess, }: {
|
|
|
33
37
|
}) => Persistor;
|
|
34
38
|
declare const clean: () => void;
|
|
35
39
|
|
|
40
|
+
type PromiseCacheOptions = {
|
|
41
|
+
ttlInSeconds?: number;
|
|
42
|
+
caseSensitive?: boolean;
|
|
43
|
+
redis?: RedisClientOptions;
|
|
44
|
+
onError?: () => void;
|
|
45
|
+
onSuccess?: () => void;
|
|
46
|
+
};
|
|
36
47
|
declare class PromiseCache<U> {
|
|
37
48
|
persistor: Persistor;
|
|
38
49
|
private readonly caseSensitive;
|
|
@@ -42,13 +53,7 @@ declare class PromiseCache<U> {
|
|
|
42
53
|
* @param ttlInSeconds Default cache TTL.
|
|
43
54
|
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
44
55
|
*/
|
|
45
|
-
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }:
|
|
46
|
-
ttlInSeconds?: number;
|
|
47
|
-
caseSensitive?: boolean;
|
|
48
|
-
redis?: RedisClientOptions;
|
|
49
|
-
onError?: () => void;
|
|
50
|
-
onSuccess?: () => void;
|
|
51
|
-
});
|
|
56
|
+
constructor({ ttlInSeconds, caseSensitive, redis, onSuccess, onError, }: PromiseCacheOptions);
|
|
52
57
|
/**
|
|
53
58
|
* Cache size.
|
|
54
59
|
* @returns The number of entries in the cache.
|
|
@@ -90,4 +95,4 @@ declare class LocalStorage {
|
|
|
90
95
|
}
|
|
91
96
|
declare const createLocalMemoryClient: () => LocalStorage;
|
|
92
97
|
|
|
93
|
-
export { LocalStorage, Persistor, PromiseCache, clean, createLocalMemoryClient, createPersistor };
|
|
98
|
+
export { LocalStorage, Persistor, PromiseCache, type PromiseCacheOptions, clean, createLocalMemoryClient, createPersistor };
|
package/dist/index.js
CHANGED
|
@@ -30,6 +30,7 @@ __export(src_exports, {
|
|
|
30
30
|
module.exports = __toCommonJS(src_exports);
|
|
31
31
|
|
|
32
32
|
// src/persistor.ts
|
|
33
|
+
var import_retry = require("@sebspark/retry");
|
|
33
34
|
var import_redis = require("redis");
|
|
34
35
|
|
|
35
36
|
// src/localMemory.ts
|
|
@@ -76,48 +77,57 @@ var CACHE_CLIENT = import_redis.createClient;
|
|
|
76
77
|
var isTestRunning = process.env.NODE_ENV === "test";
|
|
77
78
|
var Persistor = class {
|
|
78
79
|
client = null;
|
|
80
|
+
onError;
|
|
81
|
+
onSuccess;
|
|
79
82
|
redis;
|
|
80
83
|
constructor(options) {
|
|
81
84
|
const { redis, onError, onSuccess } = options;
|
|
85
|
+
this.onError = onError;
|
|
86
|
+
this.onSuccess = onSuccess;
|
|
82
87
|
if (redis && !isTestRunning) {
|
|
83
88
|
this.redis = redis;
|
|
84
89
|
} else {
|
|
85
90
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
86
91
|
}
|
|
87
|
-
this.connect(
|
|
92
|
+
this.connect();
|
|
88
93
|
}
|
|
89
|
-
async connect(
|
|
94
|
+
async connect() {
|
|
95
|
+
const settings = {
|
|
96
|
+
interval: (x) => {
|
|
97
|
+
return x * 2 * 1e3;
|
|
98
|
+
},
|
|
99
|
+
maxRetries: 5,
|
|
100
|
+
retryCondition: () => {
|
|
101
|
+
console.log("Trying to connect!");
|
|
102
|
+
return true;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
await (0, import_retry.retry)(() => this.startConnection(), settings);
|
|
106
|
+
}
|
|
107
|
+
async startConnection() {
|
|
90
108
|
var _a, _b;
|
|
91
109
|
try {
|
|
92
110
|
this.client = CACHE_CLIENT(this.redis);
|
|
93
111
|
this.client.on("error", (err) => {
|
|
94
112
|
var _a2, _b2;
|
|
95
|
-
if (onError) {
|
|
96
|
-
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
97
|
-
} else {
|
|
98
|
-
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
113
|
+
if (this.onError) {
|
|
114
|
+
this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
99
115
|
}
|
|
116
|
+
throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
100
117
|
});
|
|
101
|
-
this.client.connect()
|
|
102
|
-
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
return;
|
|
118
|
+
this.client.on("connect", () => {
|
|
119
|
+
var _a2, _b2;
|
|
120
|
+
if (this.onSuccess) {
|
|
121
|
+
this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
106
122
|
}
|
|
107
|
-
|
|
108
|
-
var _a2;
|
|
109
|
-
if (onSuccess) {
|
|
110
|
-
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
111
|
-
}
|
|
112
|
-
resolve(true);
|
|
113
|
-
});
|
|
123
|
+
console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
|
|
114
124
|
});
|
|
125
|
+
return await this.client.connect();
|
|
115
126
|
} catch (err) {
|
|
116
|
-
if (onError) {
|
|
117
|
-
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
118
|
-
} else {
|
|
119
|
-
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
127
|
+
if (this.onError) {
|
|
128
|
+
this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
120
129
|
}
|
|
130
|
+
throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
121
131
|
}
|
|
122
132
|
}
|
|
123
133
|
async size() {
|
package/dist/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// src/persistor.ts
|
|
2
|
+
import { retry } from "@sebspark/retry";
|
|
2
3
|
import { createClient } from "redis";
|
|
3
4
|
|
|
4
5
|
// src/localMemory.ts
|
|
@@ -45,48 +46,57 @@ var CACHE_CLIENT = createClient;
|
|
|
45
46
|
var isTestRunning = process.env.NODE_ENV === "test";
|
|
46
47
|
var Persistor = class {
|
|
47
48
|
client = null;
|
|
49
|
+
onError;
|
|
50
|
+
onSuccess;
|
|
48
51
|
redis;
|
|
49
52
|
constructor(options) {
|
|
50
53
|
const { redis, onError, onSuccess } = options;
|
|
54
|
+
this.onError = onError;
|
|
55
|
+
this.onSuccess = onSuccess;
|
|
51
56
|
if (redis && !isTestRunning) {
|
|
52
57
|
this.redis = redis;
|
|
53
58
|
} else {
|
|
54
59
|
CACHE_CLIENT = createLocalMemoryClient;
|
|
55
60
|
}
|
|
56
|
-
this.connect(
|
|
61
|
+
this.connect();
|
|
57
62
|
}
|
|
58
|
-
async connect(
|
|
63
|
+
async connect() {
|
|
64
|
+
const settings = {
|
|
65
|
+
interval: (x) => {
|
|
66
|
+
return x * 2 * 1e3;
|
|
67
|
+
},
|
|
68
|
+
maxRetries: 5,
|
|
69
|
+
retryCondition: () => {
|
|
70
|
+
console.log("Trying to connect!");
|
|
71
|
+
return true;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
await retry(() => this.startConnection(), settings);
|
|
75
|
+
}
|
|
76
|
+
async startConnection() {
|
|
59
77
|
var _a, _b;
|
|
60
78
|
try {
|
|
61
79
|
this.client = CACHE_CLIENT(this.redis);
|
|
62
80
|
this.client.on("error", (err) => {
|
|
63
81
|
var _a2, _b2;
|
|
64
|
-
if (onError) {
|
|
65
|
-
onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
66
|
-
} else {
|
|
67
|
-
console.error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
82
|
+
if (this.onError) {
|
|
83
|
+
this.onError(`\u274C REDIS | Client Error | ${(_a2 = this.redis) == null ? void 0 : _a2.url} ${err}`);
|
|
68
84
|
}
|
|
85
|
+
throw new Error(`\u274C REDIS | Client Error | ${(_b2 = this.redis) == null ? void 0 : _b2.url} ${err}`);
|
|
69
86
|
});
|
|
70
|
-
this.client.connect()
|
|
71
|
-
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
return;
|
|
87
|
+
this.client.on("connect", () => {
|
|
88
|
+
var _a2, _b2;
|
|
89
|
+
if (this.onSuccess) {
|
|
90
|
+
this.onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
75
91
|
}
|
|
76
|
-
|
|
77
|
-
var _a2;
|
|
78
|
-
if (onSuccess) {
|
|
79
|
-
onSuccess(`\u{1F4E6} REDIS | Connection Ready | ${(_a2 = this.redis) == null ? void 0 : _a2.url}`);
|
|
80
|
-
}
|
|
81
|
-
resolve(true);
|
|
82
|
-
});
|
|
92
|
+
console.log(`\u{1F4E6} REDIS | Connection Ready | ${(_b2 = this.redis) == null ? void 0 : _b2.url}`);
|
|
83
93
|
});
|
|
94
|
+
return await this.client.connect();
|
|
84
95
|
} catch (err) {
|
|
85
|
-
if (onError) {
|
|
86
|
-
onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
87
|
-
} else {
|
|
88
|
-
console.error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
96
|
+
if (this.onError) {
|
|
97
|
+
this.onError(`\u274C REDIS | Connection Error | ${(_a = this.redis) == null ? void 0 : _a.url} ${err}`);
|
|
89
98
|
}
|
|
99
|
+
throw new Error(`\u274C REDIS | Connection Error | ${(_b = this.redis) == null ? void 0 : _b.url} ${err}`);
|
|
90
100
|
}
|
|
91
101
|
}
|
|
92
102
|
async size() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/promise-cache",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.4.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"tsconfig": "*"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"redis": "4.6.14"
|
|
22
|
+
"redis": "4.6.14",
|
|
23
|
+
"@sebspark/retry": "*"
|
|
23
24
|
}
|
|
24
25
|
}
|