@sebspark/promise-cache 0.2.1 → 0.2.2

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 CHANGED
@@ -1,5 +1,26 @@
1
+ type GetType<T> = {
2
+ value: T;
3
+ ttl: number;
4
+ timestamp: number;
5
+ };
6
+ type SetParams<T> = {
7
+ value: T;
8
+ timestamp: number;
9
+ ttl: number;
10
+ };
11
+ declare class Persistor {
12
+ private client;
13
+ status: 'connected' | 'disconnected';
14
+ constructor();
15
+ connect(): Promise<void>;
16
+ size(): Promise<number>;
17
+ get<T>(key: string): Promise<GetType<T> | null>;
18
+ set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
19
+ delete(key: string): Promise<void>;
20
+ }
21
+
1
22
  declare class PromiseCache<U> {
2
- private cache;
23
+ persistor: Persistor;
3
24
  private readonly caseSensitive;
4
25
  private readonly ttl;
5
26
  /**
@@ -13,6 +34,13 @@ declare class PromiseCache<U> {
13
34
  * @returns The number of entries in the cache.
14
35
  */
15
36
  size(): Promise<number>;
37
+ /**
38
+ * Set a value in the cache.
39
+ * @param key Cache key.
40
+ * @param value Cache value.
41
+ * @param ttlInSeconds Time to live in seconds.
42
+ */
43
+ override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
16
44
  /**
17
45
  * A simple promise cache wrapper.
18
46
  * @param key Cache key.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,26 @@
1
+ type GetType<T> = {
2
+ value: T;
3
+ ttl: number;
4
+ timestamp: number;
5
+ };
6
+ type SetParams<T> = {
7
+ value: T;
8
+ timestamp: number;
9
+ ttl: number;
10
+ };
11
+ declare class Persistor {
12
+ private client;
13
+ status: 'connected' | 'disconnected';
14
+ constructor();
15
+ connect(): Promise<void>;
16
+ size(): Promise<number>;
17
+ get<T>(key: string): Promise<GetType<T> | null>;
18
+ set<T>(key: string, { value, timestamp, ttl }: SetParams<T>): Promise<void>;
19
+ delete(key: string): Promise<void>;
20
+ }
21
+
1
22
  declare class PromiseCache<U> {
2
- private cache;
23
+ persistor: Persistor;
3
24
  private readonly caseSensitive;
4
25
  private readonly ttl;
5
26
  /**
@@ -13,6 +34,13 @@ declare class PromiseCache<U> {
13
34
  * @returns The number of entries in the cache.
14
35
  */
15
36
  size(): Promise<number>;
37
+ /**
38
+ * Set a value in the cache.
39
+ * @param key Cache key.
40
+ * @param value Cache value.
41
+ * @param ttlInSeconds Time to live in seconds.
42
+ */
43
+ override<U>(key: string, value: U, ttlInSeconds?: number): Promise<void>;
16
44
  /**
17
45
  * A simple promise cache wrapper.
18
46
  * @param key Cache key.
package/dist/index.js CHANGED
@@ -26,19 +26,38 @@ module.exports = __toCommonJS(src_exports);
26
26
 
27
27
  // src/persistor.ts
28
28
  var import_redis = require("redis");
29
- var REDIS_HOST = process.env.REDIS_HOST || "redis";
29
+ var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
30
30
  var REDIS_PORT = process.env.REDIS_PORT || 6379;
31
31
  var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
32
32
  var Persistor = class {
33
33
  client;
34
+ status = "disconnected";
34
35
  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
- );
36
+ this.connect();
37
+ }
38
+ async connect() {
39
+ try {
40
+ this.client = await (0, import_redis.createClient)({ url: REDIS_URL });
41
+ this.client.on("error", (err) => {
42
+ console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
43
+ this.status = "disconnected";
44
+ });
45
+ this.client.connect();
46
+ await new Promise((resolve, reject) => {
47
+ if (!this.client) {
48
+ reject("Client not initialized");
49
+ return;
50
+ }
51
+ this.client.on("connect", () => {
52
+ console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
53
+ this.status = "connected";
54
+ resolve(true);
55
+ });
56
+ });
57
+ } catch (err) {
58
+ console.error(`\u274C REDIS | Connection Error | ${REDIS_URL}`, err);
59
+ this.status = "disconnected";
60
+ }
42
61
  }
43
62
  async size() {
44
63
  if (!this.client) {
@@ -91,7 +110,7 @@ var persistor = new Persistor();
91
110
 
92
111
  // src/promiseCache.ts
93
112
  var PromiseCache = class {
94
- cache;
113
+ persistor;
95
114
  caseSensitive;
96
115
  ttl;
97
116
  // Time to live in milliseconds.
@@ -101,7 +120,7 @@ var PromiseCache = class {
101
120
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
102
121
  */
103
122
  constructor(ttlInSeconds, caseSensitive = false) {
104
- this.cache = persistor;
123
+ this.persistor = persistor;
105
124
  this.caseSensitive = caseSensitive;
106
125
  this.ttl = ttlInSeconds * 1e3;
107
126
  }
@@ -110,7 +129,22 @@ var PromiseCache = class {
110
129
  * @returns The number of entries in the cache.
111
130
  */
112
131
  async size() {
113
- return await this.cache.size();
132
+ return await this.persistor.size();
133
+ }
134
+ /**
135
+ * Set a value in the cache.
136
+ * @param key Cache key.
137
+ * @param value Cache value.
138
+ * @param ttlInSeconds Time to live in seconds.
139
+ */
140
+ async override(key, value, ttlInSeconds) {
141
+ const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
142
+ const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
143
+ await this.persistor.set(effectiveKey, {
144
+ value,
145
+ timestamp: Date.now(),
146
+ ttl: effectiveTTL
147
+ });
114
148
  }
115
149
  /**
116
150
  * A simple promise cache wrapper.
@@ -123,7 +157,7 @@ var PromiseCache = class {
123
157
  const now = Date.now();
124
158
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
125
159
  const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
126
- const cached = await this.cache.get(effectiveKey);
160
+ const cached = await this.persistor.get(effectiveKey);
127
161
  if (cached) {
128
162
  if (cached.ttl !== effectiveTTL) {
129
163
  console.error(
@@ -133,7 +167,7 @@ var PromiseCache = class {
133
167
  return cached.value;
134
168
  }
135
169
  const response = await delegate();
136
- this.cache.set(effectiveKey, {
170
+ this.persistor.set(effectiveKey, {
137
171
  value: response,
138
172
  timestamp: now,
139
173
  ttl: effectiveTTL
package/dist/index.mjs CHANGED
@@ -1,18 +1,37 @@
1
1
  // src/persistor.ts
2
2
  import { createClient } from "redis";
3
- var REDIS_HOST = process.env.REDIS_HOST || "redis";
3
+ var REDIS_HOST = process.env.REDIS_HOST || "127.0.0.1";
4
4
  var REDIS_PORT = process.env.REDIS_PORT || 6379;
5
5
  var REDIS_URL = `redis://${REDIS_HOST}:${REDIS_PORT}`;
6
6
  var Persistor = class {
7
7
  client;
8
+ status = "disconnected";
8
9
  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
- );
10
+ this.connect();
11
+ }
12
+ async connect() {
13
+ try {
14
+ this.client = await createClient({ url: REDIS_URL });
15
+ this.client.on("error", (err) => {
16
+ console.error(`\u274C REDIS | Client Error | ${REDIS_URL}`, err);
17
+ this.status = "disconnected";
18
+ });
19
+ this.client.connect();
20
+ await new Promise((resolve, reject) => {
21
+ if (!this.client) {
22
+ reject("Client not initialized");
23
+ return;
24
+ }
25
+ this.client.on("connect", () => {
26
+ console.info(`\u{1F4E6} REDIS | Connection Ready | ${REDIS_URL}`);
27
+ this.status = "connected";
28
+ resolve(true);
29
+ });
30
+ });
31
+ } catch (err) {
32
+ console.error(`\u274C REDIS | Connection Error | ${REDIS_URL}`, err);
33
+ this.status = "disconnected";
34
+ }
16
35
  }
17
36
  async size() {
18
37
  if (!this.client) {
@@ -65,7 +84,7 @@ var persistor = new Persistor();
65
84
 
66
85
  // src/promiseCache.ts
67
86
  var PromiseCache = class {
68
- cache;
87
+ persistor;
69
88
  caseSensitive;
70
89
  ttl;
71
90
  // Time to live in milliseconds.
@@ -75,7 +94,7 @@ var PromiseCache = class {
75
94
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
76
95
  */
77
96
  constructor(ttlInSeconds, caseSensitive = false) {
78
- this.cache = persistor;
97
+ this.persistor = persistor;
79
98
  this.caseSensitive = caseSensitive;
80
99
  this.ttl = ttlInSeconds * 1e3;
81
100
  }
@@ -84,7 +103,22 @@ var PromiseCache = class {
84
103
  * @returns The number of entries in the cache.
85
104
  */
86
105
  async size() {
87
- return await this.cache.size();
106
+ return await this.persistor.size();
107
+ }
108
+ /**
109
+ * Set a value in the cache.
110
+ * @param key Cache key.
111
+ * @param value Cache value.
112
+ * @param ttlInSeconds Time to live in seconds.
113
+ */
114
+ async override(key, value, ttlInSeconds) {
115
+ const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
116
+ const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
117
+ await this.persistor.set(effectiveKey, {
118
+ value,
119
+ timestamp: Date.now(),
120
+ ttl: effectiveTTL
121
+ });
88
122
  }
89
123
  /**
90
124
  * A simple promise cache wrapper.
@@ -97,7 +131,7 @@ var PromiseCache = class {
97
131
  const now = Date.now();
98
132
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
99
133
  const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
100
- const cached = await this.cache.get(effectiveKey);
134
+ const cached = await this.persistor.get(effectiveKey);
101
135
  if (cached) {
102
136
  if (cached.ttl !== effectiveTTL) {
103
137
  console.error(
@@ -107,7 +141,7 @@ var PromiseCache = class {
107
141
  return cached.value;
108
142
  }
109
143
  const response = await delegate();
110
- this.cache.set(effectiveKey, {
144
+ this.persistor.set(effectiveKey, {
111
145
  value: response,
112
146
  timestamp: now,
113
147
  ttl: effectiveTTL
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",