@sebspark/promise-cache 0.2.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 CHANGED
@@ -1,4 +1,4 @@
1
- declare class PromiseCache<T, U> {
1
+ declare class PromiseCache<U> {
2
2
  private cache;
3
3
  private readonly caseSensitive;
4
4
  private readonly ttl;
@@ -12,7 +12,7 @@ declare class PromiseCache<T, U> {
12
12
  * Cache size.
13
13
  * @returns The number of entries in the cache.
14
14
  */
15
- size(): number;
15
+ size(): Promise<number>;
16
16
  /**
17
17
  * A simple promise cache wrapper.
18
18
  * @param key Cache key.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare class PromiseCache<T, U> {
1
+ declare class PromiseCache<U> {
2
2
  private cache;
3
3
  private readonly caseSensitive;
4
4
  private readonly ttl;
@@ -12,7 +12,7 @@ declare class PromiseCache<T, U> {
12
12
  * Cache size.
13
13
  * @returns The number of entries in the cache.
14
14
  */
15
- size(): number;
15
+ size(): Promise<number>;
16
16
  /**
17
17
  * A simple promise cache wrapper.
18
18
  * @param key Cache key.
package/dist/index.js CHANGED
@@ -24,6 +24,71 @@ __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;
@@ -36,7 +101,7 @@ var PromiseCache = class {
36
101
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
37
102
  */
38
103
  constructor(ttlInSeconds, caseSensitive = false) {
39
- this.cache = /* @__PURE__ */ new Map();
104
+ this.cache = persistor;
40
105
  this.caseSensitive = caseSensitive;
41
106
  this.ttl = ttlInSeconds * 1e3;
42
107
  }
@@ -44,8 +109,8 @@ var PromiseCache = class {
44
109
  * Cache size.
45
110
  * @returns The number of entries in the cache.
46
111
  */
47
- size() {
48
- return this.cache.size;
112
+ async size() {
113
+ return await this.cache.size();
49
114
  }
50
115
  /**
51
116
  * A simple promise cache wrapper.
@@ -58,7 +123,7 @@ var PromiseCache = class {
58
123
  const now = Date.now();
59
124
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
60
125
  const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
61
- const cached = this.cache.get(effectiveKey);
126
+ const cached = await this.cache.get(effectiveKey);
62
127
  if (cached) {
63
128
  if (cached.ttl !== effectiveTTL) {
64
129
  console.error(
@@ -73,9 +138,6 @@ var PromiseCache = class {
73
138
  timestamp: now,
74
139
  ttl: effectiveTTL
75
140
  });
76
- setTimeout(() => {
77
- this.cache.delete(effectiveKey);
78
- }, effectiveTTL);
79
141
  return response;
80
142
  }
81
143
  };
package/dist/index.mjs CHANGED
@@ -1,3 +1,68 @@
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;
@@ -10,7 +75,7 @@ var PromiseCache = class {
10
75
  * @param caseSensitive Set to true if you want to differentiate between keys with different casing.
11
76
  */
12
77
  constructor(ttlInSeconds, caseSensitive = false) {
13
- this.cache = /* @__PURE__ */ new Map();
78
+ this.cache = persistor;
14
79
  this.caseSensitive = caseSensitive;
15
80
  this.ttl = ttlInSeconds * 1e3;
16
81
  }
@@ -18,8 +83,8 @@ var PromiseCache = class {
18
83
  * Cache size.
19
84
  * @returns The number of entries in the cache.
20
85
  */
21
- size() {
22
- return this.cache.size;
86
+ async size() {
87
+ return await this.cache.size();
23
88
  }
24
89
  /**
25
90
  * A simple promise cache wrapper.
@@ -32,7 +97,7 @@ var PromiseCache = class {
32
97
  const now = Date.now();
33
98
  const effectiveKey = this.caseSensitive ? key : key.toLowerCase();
34
99
  const effectiveTTL = ttlInSeconds !== void 0 ? ttlInSeconds * 1e3 : this.ttl;
35
- const cached = this.cache.get(effectiveKey);
100
+ const cached = await this.cache.get(effectiveKey);
36
101
  if (cached) {
37
102
  if (cached.ttl !== effectiveTTL) {
38
103
  console.error(
@@ -47,9 +112,6 @@ var PromiseCache = class {
47
112
  timestamp: now,
48
113
  ttl: effectiveTTL
49
114
  });
50
- setTimeout(() => {
51
- this.cache.delete(effectiveKey);
52
- }, effectiveTTL);
53
115
  return response;
54
116
  }
55
117
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sebspark/promise-cache",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",
@@ -15,5 +15,8 @@
15
15
  },
16
16
  "devDependencies": {
17
17
  "tsconfig": "*"
18
+ },
19
+ "dependencies": {
20
+ "redis": "^4.6.13"
18
21
  }
19
22
  }