@zintrust/cache-redis 0.4.67 → 0.4.71

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.
Files changed (2) hide show
  1. package/dist/index.js +64 -22
  2. package/package.json +3 -3
package/dist/index.js CHANGED
@@ -11,9 +11,27 @@ const safeJsonParse = (value) => {
11
11
  async function importRedis() {
12
12
  return (await import('redis'));
13
13
  }
14
+ const logCacheFailure = (action, error) => {
15
+ Logger.error(`Redis cache ${action} failed`, error);
16
+ };
17
+ const createCacheFailureState = () => {
18
+ let disabled = false;
19
+ return {
20
+ isDisabled: () => disabled,
21
+ disableAfterFailure: (action, error) => {
22
+ if (!disabled) {
23
+ logCacheFailure(action, error);
24
+ }
25
+ disabled = true;
26
+ },
27
+ };
28
+ };
14
29
  const createCacheOperations = (ensureClient, operations, defaultTtl) => {
30
+ const failureState = createCacheFailureState();
15
31
  return {
16
32
  async get(key) {
33
+ if (failureState.isDisabled())
34
+ return null;
17
35
  try {
18
36
  const client = await ensureClient();
19
37
  const value = await operations.get(client, key);
@@ -22,28 +40,57 @@ const createCacheOperations = (ensureClient, operations, defaultTtl) => {
22
40
  return safeJsonParse(value);
23
41
  }
24
42
  catch (error) {
25
- Logger.error('Redis cache GET failed', error);
43
+ failureState.disableAfterFailure('GET', error);
26
44
  return null;
27
45
  }
28
46
  },
29
47
  async set(key, value, ttl) {
30
- const client = await ensureClient();
31
- const json = JSON.stringify(value);
32
- const effectiveTtl = ttl ?? defaultTtl;
33
- await operations.set(client, key, json, effectiveTtl);
48
+ if (failureState.isDisabled())
49
+ return;
50
+ try {
51
+ const client = await ensureClient();
52
+ const json = JSON.stringify(value);
53
+ const effectiveTtl = ttl ?? defaultTtl;
54
+ await operations.set(client, key, json, effectiveTtl);
55
+ }
56
+ catch (error) {
57
+ failureState.disableAfterFailure('SET', error);
58
+ }
34
59
  },
35
60
  async delete(key) {
36
- const client = await ensureClient();
37
- await operations.del(client, key);
61
+ if (failureState.isDisabled())
62
+ return;
63
+ try {
64
+ const client = await ensureClient();
65
+ await operations.del(client, key);
66
+ }
67
+ catch (error) {
68
+ failureState.disableAfterFailure('DEL', error);
69
+ }
38
70
  },
39
71
  async clear() {
40
- const client = await ensureClient();
41
- await operations.clear(client);
72
+ if (failureState.isDisabled())
73
+ return;
74
+ try {
75
+ const client = await ensureClient();
76
+ await operations.clear(client);
77
+ }
78
+ catch (error) {
79
+ failureState.disableAfterFailure('FLUSHDB', error);
80
+ }
42
81
  },
43
82
  async has(key) {
44
- const client = await ensureClient();
45
- const count = await operations.exists(client, key);
46
- return count > 0;
83
+ if (failureState.isDisabled())
84
+ return false;
85
+ try {
86
+ const client = await ensureClient();
87
+ const count = await operations.exists(client, key);
88
+ return count > 0;
89
+ }
90
+ catch (error) {
91
+ failureState.disableAfterFailure('EXISTS', error);
92
+ return false;
93
+ }
47
94
  },
48
95
  };
49
96
  };
@@ -73,9 +120,8 @@ const createWorkersCacheDriver = (config) => {
73
120
  return redisClient.set(key, json);
74
121
  }
75
122
  },
76
- del: (redisClient, key) => {
77
- redisClient.del(key);
78
- return Promise.resolve();
123
+ del: async (redisClient, key) => {
124
+ await redisClient.del(key);
79
125
  },
80
126
  clear: (redisClient) => {
81
127
  if (typeof redisClient.flushDb === 'function') {
@@ -113,14 +159,10 @@ const createNodeCacheDriver = (config) => {
113
159
  return redisClient.set(key, json);
114
160
  }
115
161
  },
116
- del: (redisClient, key) => {
117
- redisClient.del(key);
118
- return Promise.resolve();
119
- },
120
- clear: (redisClient) => {
121
- redisClient.flushDb();
122
- return Promise.resolve();
162
+ del: async (redisClient, key) => {
163
+ await redisClient.del(key);
123
164
  },
165
+ clear: (redisClient) => redisClient.flushDb(),
124
166
  exists: (redisClient, key) => redisClient.exists(key),
125
167
  }, config.ttl ?? 300);
126
168
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/cache-redis",
3
- "version": "0.4.67",
3
+ "version": "0.4.71",
4
4
  "description": "Redis cache driver for ZinTrust.",
5
5
  "private": false,
6
6
  "type": "module",
@@ -23,7 +23,7 @@
23
23
  "node": ">=20.0.0"
24
24
  },
25
25
  "peerDependencies": {
26
- "@zintrust/core": "^0.4.67"
26
+ "@zintrust/core": "^0.4.71"
27
27
  },
28
28
  "publishConfig": {
29
29
  "access": "public"
@@ -42,4 +42,4 @@
42
42
  "build": "tsc -p tsconfig.json",
43
43
  "prepublishOnly": "npm run build"
44
44
  }
45
- }
45
+ }