@sebspark/promise-cache 0.1.0 → 0.2.1
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 +21 -3
- package/dist/index.d.ts +21 -3
- package/dist/index.js +104 -7
- package/dist/index.mjs +104 -7
- package/package.json +6 -5
package/dist/index.d.mts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
declare class PromiseCache<
|
|
1
|
+
declare class PromiseCache<U> {
|
|
2
2
|
private cache;
|
|
3
|
+
private readonly caseSensitive;
|
|
3
4
|
private readonly ttl;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Initialize a new PromiseCache.
|
|
7
|
+
* @param ttlInSeconds Default cache TTL.
|
|
8
|
+
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
9
|
+
*/
|
|
10
|
+
constructor(ttlInSeconds: number, caseSensitive?: boolean);
|
|
11
|
+
/**
|
|
12
|
+
* Cache size.
|
|
13
|
+
* @returns The number of entries in the cache.
|
|
14
|
+
*/
|
|
15
|
+
size(): Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* A simple promise cache wrapper.
|
|
18
|
+
* @param key Cache key.
|
|
19
|
+
* @param delegate The function to execute if the key is not in the cache.
|
|
20
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
21
|
+
* @returns The result of the delegate function.
|
|
22
|
+
*/
|
|
23
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
6
24
|
}
|
|
7
25
|
|
|
8
26
|
export { PromiseCache };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,26 @@
|
|
|
1
|
-
declare class PromiseCache<
|
|
1
|
+
declare class PromiseCache<U> {
|
|
2
2
|
private cache;
|
|
3
|
+
private readonly caseSensitive;
|
|
3
4
|
private readonly ttl;
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
/**
|
|
6
|
+
* Initialize a new PromiseCache.
|
|
7
|
+
* @param ttlInSeconds Default cache TTL.
|
|
8
|
+
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
9
|
+
*/
|
|
10
|
+
constructor(ttlInSeconds: number, caseSensitive?: boolean);
|
|
11
|
+
/**
|
|
12
|
+
* Cache size.
|
|
13
|
+
* @returns The number of entries in the cache.
|
|
14
|
+
*/
|
|
15
|
+
size(): Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* A simple promise cache wrapper.
|
|
18
|
+
* @param key Cache key.
|
|
19
|
+
* @param delegate The function to execute if the key is not in the cache.
|
|
20
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
21
|
+
* @returns The result of the delegate function.
|
|
22
|
+
*/
|
|
23
|
+
wrap(key: string, delegate: () => Promise<U>, ttlInSeconds?: number): Promise<U>;
|
|
6
24
|
}
|
|
7
25
|
|
|
8
26
|
export { PromiseCache };
|
package/dist/index.js
CHANGED
|
@@ -24,23 +24,120 @@ __export(src_exports, {
|
|
|
24
24
|
});
|
|
25
25
|
module.exports = __toCommonJS(src_exports);
|
|
26
26
|
|
|
27
|
+
// src/persistor.ts
|
|
28
|
+
var import_redis = require("redis");
|
|
29
|
+
var REDIS_HOST = process.env.REDIS_HOST || "redis";
|
|
30
|
+
var REDIS_PORT = process.env.REDIS_PORT || 6379;
|
|
31
|
+
var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
|
|
32
|
+
var Persistor = class {
|
|
33
|
+
client;
|
|
34
|
+
constructor() {
|
|
35
|
+
this.client = (0, import_redis.createClient)({ url: REDIS_URL }).on(
|
|
36
|
+
"error",
|
|
37
|
+
(err) => console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err)
|
|
38
|
+
).on(
|
|
39
|
+
"connect",
|
|
40
|
+
() => console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`)
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
async size() {
|
|
44
|
+
if (!this.client) {
|
|
45
|
+
throw new Error("Client not initialized");
|
|
46
|
+
}
|
|
47
|
+
return await this.client.DBSIZE();
|
|
48
|
+
}
|
|
49
|
+
async get(key) {
|
|
50
|
+
if (!this.client) {
|
|
51
|
+
throw new Error("Client not initialized");
|
|
52
|
+
}
|
|
53
|
+
try {
|
|
54
|
+
const result = await this.client.get(key);
|
|
55
|
+
if (!result) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
return JSON.parse(result);
|
|
59
|
+
} catch (error) {
|
|
60
|
+
console.error(`Error getting data from redis: ${error}`);
|
|
61
|
+
throw error;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async set(key, { value, timestamp, ttl }) {
|
|
65
|
+
if (!this.client) {
|
|
66
|
+
throw new Error("Client not initialized");
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
70
|
+
await this.client.set(key, serializedData, {
|
|
71
|
+
EX: ttl / 1e3
|
|
72
|
+
});
|
|
73
|
+
} catch (error) {
|
|
74
|
+
console.error(`Error setting data in redis: ${error}`);
|
|
75
|
+
throw error;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async delete(key) {
|
|
79
|
+
if (!this.client) {
|
|
80
|
+
throw new Error("Client not initialized");
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
await this.client.del(key);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
console.error(`Error deleting data from redis: ${error}`);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
var persistor = new Persistor();
|
|
91
|
+
|
|
27
92
|
// src/promiseCache.ts
|
|
28
93
|
var PromiseCache = class {
|
|
29
94
|
cache;
|
|
95
|
+
caseSensitive;
|
|
30
96
|
ttl;
|
|
31
|
-
// Time to live in milliseconds
|
|
32
|
-
|
|
33
|
-
|
|
97
|
+
// Time to live in milliseconds.
|
|
98
|
+
/**
|
|
99
|
+
* Initialize a new PromiseCache.
|
|
100
|
+
* @param ttlInSeconds Default cache TTL.
|
|
101
|
+
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
102
|
+
*/
|
|
103
|
+
constructor(ttlInSeconds, caseSensitive = false) {
|
|
104
|
+
this.cache = persistor;
|
|
105
|
+
this.caseSensitive = caseSensitive;
|
|
34
106
|
this.ttl = ttlInSeconds * 1e3;
|
|
35
107
|
}
|
|
36
|
-
|
|
108
|
+
/**
|
|
109
|
+
* Cache size.
|
|
110
|
+
* @returns The number of entries in the cache.
|
|
111
|
+
*/
|
|
112
|
+
async size() {
|
|
113
|
+
return await this.cache.size();
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* A simple promise cache wrapper.
|
|
117
|
+
* @param key Cache key.
|
|
118
|
+
* @param delegate The function to execute if the key is not in the cache.
|
|
119
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
120
|
+
* @returns The result of the delegate function.
|
|
121
|
+
*/
|
|
122
|
+
async wrap(key, delegate, ttlInSeconds) {
|
|
37
123
|
const now = Date.now();
|
|
38
|
-
const
|
|
39
|
-
|
|
124
|
+
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
125
|
+
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
126
|
+
const cached = await this.cache.get(effectiveKey);
|
|
127
|
+
if (cached) {
|
|
128
|
+
if (cached.ttl !== effectiveTTL) {
|
|
129
|
+
console.error(
|
|
130
|
+
`WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
|
|
131
|
+
);
|
|
132
|
+
}
|
|
40
133
|
return cached.value;
|
|
41
134
|
}
|
|
42
135
|
const response = await delegate();
|
|
43
|
-
this.cache.set(
|
|
136
|
+
this.cache.set(effectiveKey, {
|
|
137
|
+
value: response,
|
|
138
|
+
timestamp: now,
|
|
139
|
+
ttl: effectiveTTL
|
|
140
|
+
});
|
|
44
141
|
return response;
|
|
45
142
|
}
|
|
46
143
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,20 +1,117 @@
|
|
|
1
|
+
// src/persistor.ts
|
|
2
|
+
import { createClient } from "redis";
|
|
3
|
+
var REDIS_HOST = process.env.REDIS_HOST || "redis";
|
|
4
|
+
var REDIS_PORT = process.env.REDIS_PORT || 6379;
|
|
5
|
+
var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
|
|
6
|
+
var Persistor = class {
|
|
7
|
+
client;
|
|
8
|
+
constructor() {
|
|
9
|
+
this.client = createClient({ url: REDIS_URL }).on(
|
|
10
|
+
"error",
|
|
11
|
+
(err) => console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err)
|
|
12
|
+
).on(
|
|
13
|
+
"connect",
|
|
14
|
+
() => console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`)
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
async size() {
|
|
18
|
+
if (!this.client) {
|
|
19
|
+
throw new Error("Client not initialized");
|
|
20
|
+
}
|
|
21
|
+
return await this.client.DBSIZE();
|
|
22
|
+
}
|
|
23
|
+
async get(key) {
|
|
24
|
+
if (!this.client) {
|
|
25
|
+
throw new Error("Client not initialized");
|
|
26
|
+
}
|
|
27
|
+
try {
|
|
28
|
+
const result = await this.client.get(key);
|
|
29
|
+
if (!result) {
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
return JSON.parse(result);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.error(`Error getting data from redis: ${error}`);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
async set(key, { value, timestamp, ttl }) {
|
|
39
|
+
if (!this.client) {
|
|
40
|
+
throw new Error("Client not initialized");
|
|
41
|
+
}
|
|
42
|
+
try {
|
|
43
|
+
const serializedData = JSON.stringify({ value, ttl, timestamp });
|
|
44
|
+
await this.client.set(key, serializedData, {
|
|
45
|
+
EX: ttl / 1e3
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.error(`Error setting data in redis: ${error}`);
|
|
49
|
+
throw error;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
async delete(key) {
|
|
53
|
+
if (!this.client) {
|
|
54
|
+
throw new Error("Client not initialized");
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
await this.client.del(key);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
console.error(`Error deleting data from redis: ${error}`);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
var persistor = new Persistor();
|
|
65
|
+
|
|
1
66
|
// src/promiseCache.ts
|
|
2
67
|
var PromiseCache = class {
|
|
3
68
|
cache;
|
|
69
|
+
caseSensitive;
|
|
4
70
|
ttl;
|
|
5
|
-
// Time to live in milliseconds
|
|
6
|
-
|
|
7
|
-
|
|
71
|
+
// Time to live in milliseconds.
|
|
72
|
+
/**
|
|
73
|
+
* Initialize a new PromiseCache.
|
|
74
|
+
* @param ttlInSeconds Default cache TTL.
|
|
75
|
+
* @param caseSensitive Set to true if you want to differentiate between keys with different casing.
|
|
76
|
+
*/
|
|
77
|
+
constructor(ttlInSeconds, caseSensitive = false) {
|
|
78
|
+
this.cache = persistor;
|
|
79
|
+
this.caseSensitive = caseSensitive;
|
|
8
80
|
this.ttl = ttlInSeconds * 1e3;
|
|
9
81
|
}
|
|
10
|
-
|
|
82
|
+
/**
|
|
83
|
+
* Cache size.
|
|
84
|
+
* @returns The number of entries in the cache.
|
|
85
|
+
*/
|
|
86
|
+
async size() {
|
|
87
|
+
return await this.cache.size();
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* A simple promise cache wrapper.
|
|
91
|
+
* @param key Cache key.
|
|
92
|
+
* @param delegate The function to execute if the key is not in the cache.
|
|
93
|
+
* @param ttlInSeconds Time to live in seconds.
|
|
94
|
+
* @returns The result of the delegate function.
|
|
95
|
+
*/
|
|
96
|
+
async wrap(key, delegate, ttlInSeconds) {
|
|
11
97
|
const now = Date.now();
|
|
12
|
-
const
|
|
13
|
-
|
|
98
|
+
const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
|
|
99
|
+
const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
|
|
100
|
+
const cached = await this.cache.get(effectiveKey);
|
|
101
|
+
if (cached) {
|
|
102
|
+
if (cached.ttl !== effectiveTTL) {
|
|
103
|
+
console.error(
|
|
104
|
+
`WARNING: TTL mismatch for key: ${effectiveKey}. It is recommended to use the same TTL for the same key.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
14
107
|
return cached.value;
|
|
15
108
|
}
|
|
16
109
|
const response = await delegate();
|
|
17
|
-
this.cache.set(
|
|
110
|
+
this.cache.set(effectiveKey, {
|
|
111
|
+
value: response,
|
|
112
|
+
timestamp: now,
|
|
113
|
+
ttl: effectiveTTL
|
|
114
|
+
});
|
|
18
115
|
return response;
|
|
19
116
|
}
|
|
20
117
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sebspark/promise-cache",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"dist"
|
|
10
|
-
],
|
|
8
|
+
"files": ["dist"],
|
|
11
9
|
"scripts": {
|
|
12
10
|
"build": "tsup-node src/index.ts --format esm,cjs --dts",
|
|
13
11
|
"dev": "tsc --watch --noEmit",
|
|
@@ -17,5 +15,8 @@
|
|
|
17
15
|
},
|
|
18
16
|
"devDependencies": {
|
|
19
17
|
"tsconfig": "*"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"redis": "^4.6.13"
|
|
20
21
|
}
|
|
21
|
-
}
|
|
22
|
+
}
|