@naman_deep_singh/cache 1.3.2 → 1.5.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/README.md +105 -9
- package/dist/cjs/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/dist/cjs/adapters/memcache/MemcacheCache.js +75 -281
- package/dist/cjs/adapters/memory/MemoryCache.js +76 -22
- package/dist/cjs/adapters/redis/RedisCache.js +84 -26
- package/dist/cjs/core/BaseCache.js +13 -4
- package/dist/cjs/core/factory.js +26 -4
- package/dist/cjs/errors/CacheError.d.ts +10 -7
- package/dist/cjs/errors/CacheError.js +6 -11
- package/dist/cjs/errors/cacheErrorCodes.d.ts +22 -0
- package/dist/cjs/errors/cacheErrorCodes.js +24 -0
- package/dist/cjs/errors/index.js +3 -0
- package/dist/cjs/middleware/express/cacheMiddleware.js +8 -8
- package/dist/cjs/session/SessionStore.js +84 -28
- package/dist/esm/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/dist/esm/adapters/memcache/MemcacheCache.js +75 -281
- package/dist/esm/adapters/memory/MemoryCache.js +76 -22
- package/dist/esm/adapters/redis/RedisCache.js +84 -26
- package/dist/esm/core/BaseCache.js +13 -4
- package/dist/esm/core/factory.js +26 -4
- package/dist/esm/errors/CacheError.d.ts +10 -7
- package/dist/esm/errors/CacheError.js +6 -11
- package/dist/esm/errors/cacheErrorCodes.d.ts +22 -0
- package/dist/esm/errors/cacheErrorCodes.js +21 -0
- package/dist/esm/errors/index.js +3 -0
- package/dist/esm/middleware/express/cacheMiddleware.js +8 -8
- package/dist/esm/session/SessionStore.js +84 -28
- package/dist/types/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/dist/types/errors/CacheError.d.ts +10 -7
- package/dist/types/errors/cacheErrorCodes.d.ts +22 -0
- package/package.json +10 -6
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SessionStore = void 0;
|
|
4
4
|
const errors_1 = require("../errors");
|
|
5
|
+
const cacheErrorCodes_1 = require("src/errors/cacheErrorCodes");
|
|
5
6
|
/**
|
|
6
7
|
* Session store wrapper around cache adapters
|
|
7
8
|
* Provides session management functionality
|
|
@@ -23,8 +24,13 @@ class SessionStore {
|
|
|
23
24
|
const ttlValue = ttl ?? this.options.ttl;
|
|
24
25
|
await this.cache.set(sessionId, data, ttlValue);
|
|
25
26
|
}
|
|
26
|
-
catch (
|
|
27
|
-
throw new errors_1.CacheError(
|
|
27
|
+
catch (error) {
|
|
28
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_CREATE_ERROR, {
|
|
29
|
+
adapter: 'session',
|
|
30
|
+
operation: 'create',
|
|
31
|
+
details: { sessionId },
|
|
32
|
+
cause: error instanceof Error ? error : undefined,
|
|
33
|
+
});
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
36
|
/**
|
|
@@ -34,8 +40,13 @@ class SessionStore {
|
|
|
34
40
|
try {
|
|
35
41
|
return await this.cache.get(sessionId);
|
|
36
42
|
}
|
|
37
|
-
catch (
|
|
38
|
-
throw new errors_1.CacheError(
|
|
43
|
+
catch (error) {
|
|
44
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_GET_ERROR, {
|
|
45
|
+
adapter: 'session',
|
|
46
|
+
operation: 'get',
|
|
47
|
+
details: { sessionId },
|
|
48
|
+
cause: error instanceof Error ? error : undefined,
|
|
49
|
+
});
|
|
39
50
|
}
|
|
40
51
|
}
|
|
41
52
|
/**
|
|
@@ -45,16 +56,25 @@ class SessionStore {
|
|
|
45
56
|
try {
|
|
46
57
|
const current = await this.cache.get(sessionId);
|
|
47
58
|
if (!current) {
|
|
48
|
-
throw new errors_1.CacheError(
|
|
59
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_NOT_FOUND, {
|
|
60
|
+
adapter: 'session',
|
|
61
|
+
operation: 'update',
|
|
62
|
+
details: { sessionId },
|
|
63
|
+
});
|
|
49
64
|
}
|
|
50
65
|
const merged = { ...current, ...data };
|
|
51
66
|
await this.cache.set(sessionId, merged, this.options.ttl);
|
|
52
67
|
}
|
|
53
|
-
catch (
|
|
54
|
-
if (
|
|
55
|
-
throw
|
|
68
|
+
catch (error) {
|
|
69
|
+
if (error instanceof errors_1.CacheError) {
|
|
70
|
+
throw error;
|
|
56
71
|
}
|
|
57
|
-
throw new errors_1.CacheError(
|
|
72
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_UPDATE_ERROR, {
|
|
73
|
+
adapter: 'session',
|
|
74
|
+
operation: 'update',
|
|
75
|
+
details: { sessionId },
|
|
76
|
+
cause: error instanceof Error ? error : undefined,
|
|
77
|
+
});
|
|
58
78
|
}
|
|
59
79
|
}
|
|
60
80
|
/**
|
|
@@ -64,8 +84,13 @@ class SessionStore {
|
|
|
64
84
|
try {
|
|
65
85
|
return await this.cache.delete(sessionId);
|
|
66
86
|
}
|
|
67
|
-
catch (
|
|
68
|
-
throw new errors_1.CacheError(
|
|
87
|
+
catch (error) {
|
|
88
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_DELETE_ERROR, {
|
|
89
|
+
adapter: 'session',
|
|
90
|
+
operation: 'delete',
|
|
91
|
+
details: { sessionId },
|
|
92
|
+
cause: error instanceof Error ? error : undefined,
|
|
93
|
+
});
|
|
69
94
|
}
|
|
70
95
|
}
|
|
71
96
|
/**
|
|
@@ -75,8 +100,13 @@ class SessionStore {
|
|
|
75
100
|
try {
|
|
76
101
|
return await this.cache.exists(sessionId);
|
|
77
102
|
}
|
|
78
|
-
catch (
|
|
79
|
-
throw new errors_1.CacheError(
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_EXISTS_ERROR, {
|
|
105
|
+
adapter: 'session',
|
|
106
|
+
operation: 'exists',
|
|
107
|
+
details: { sessionId },
|
|
108
|
+
cause: error instanceof Error ? error : undefined,
|
|
109
|
+
});
|
|
80
110
|
}
|
|
81
111
|
}
|
|
82
112
|
/**
|
|
@@ -86,8 +116,12 @@ class SessionStore {
|
|
|
86
116
|
try {
|
|
87
117
|
await this.cache.clear();
|
|
88
118
|
}
|
|
89
|
-
catch (
|
|
90
|
-
throw new errors_1.CacheError(
|
|
119
|
+
catch (error) {
|
|
120
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_CLEAR_ERROR, {
|
|
121
|
+
adapter: 'session',
|
|
122
|
+
operation: 'clear',
|
|
123
|
+
cause: error instanceof Error ? error : undefined,
|
|
124
|
+
});
|
|
91
125
|
}
|
|
92
126
|
}
|
|
93
127
|
/**
|
|
@@ -97,8 +131,12 @@ class SessionStore {
|
|
|
97
131
|
try {
|
|
98
132
|
return await this.cache.getMultiple(sessionIds);
|
|
99
133
|
}
|
|
100
|
-
catch (
|
|
101
|
-
throw new errors_1.CacheError(
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_GET_MULTIPLE_ERROR, {
|
|
136
|
+
adapter: 'session',
|
|
137
|
+
operation: 'getMultiple',
|
|
138
|
+
cause: error instanceof Error ? error : undefined,
|
|
139
|
+
});
|
|
102
140
|
}
|
|
103
141
|
}
|
|
104
142
|
/**
|
|
@@ -108,8 +146,12 @@ class SessionStore {
|
|
|
108
146
|
try {
|
|
109
147
|
return await this.cache.deleteMultiple(sessionIds);
|
|
110
148
|
}
|
|
111
|
-
catch (
|
|
112
|
-
throw new errors_1.CacheError(
|
|
149
|
+
catch (error) {
|
|
150
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_DELETE_MULTIPLE_ERROR, {
|
|
151
|
+
adapter: 'session',
|
|
152
|
+
operation: 'deleteMultiple',
|
|
153
|
+
cause: error instanceof Error ? error : undefined,
|
|
154
|
+
});
|
|
113
155
|
}
|
|
114
156
|
}
|
|
115
157
|
/**
|
|
@@ -119,16 +161,25 @@ class SessionStore {
|
|
|
119
161
|
try {
|
|
120
162
|
const current = await this.cache.get(sessionId);
|
|
121
163
|
if (!current) {
|
|
122
|
-
throw new errors_1.CacheError(
|
|
164
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_NOT_FOUND, {
|
|
165
|
+
adapter: 'session',
|
|
166
|
+
operation: 'extend',
|
|
167
|
+
details: { sessionId },
|
|
168
|
+
});
|
|
123
169
|
}
|
|
124
170
|
const ttlValue = ttl ?? this.options.ttl;
|
|
125
171
|
await this.cache.set(sessionId, current, ttlValue);
|
|
126
172
|
}
|
|
127
|
-
catch (
|
|
128
|
-
if (
|
|
129
|
-
throw
|
|
173
|
+
catch (error) {
|
|
174
|
+
if (error instanceof errors_1.CacheError) {
|
|
175
|
+
throw error;
|
|
130
176
|
}
|
|
131
|
-
throw new errors_1.CacheError(
|
|
177
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_EXTEND_ERROR, {
|
|
178
|
+
adapter: 'session',
|
|
179
|
+
operation: 'extend',
|
|
180
|
+
details: { sessionId },
|
|
181
|
+
cause: error instanceof Error ? error : undefined,
|
|
182
|
+
});
|
|
132
183
|
}
|
|
133
184
|
}
|
|
134
185
|
/**
|
|
@@ -142,11 +193,16 @@ class SessionStore {
|
|
|
142
193
|
}
|
|
143
194
|
return data;
|
|
144
195
|
}
|
|
145
|
-
catch (
|
|
146
|
-
if (
|
|
147
|
-
throw
|
|
196
|
+
catch (error) {
|
|
197
|
+
if (error instanceof errors_1.CacheError) {
|
|
198
|
+
throw error;
|
|
148
199
|
}
|
|
149
|
-
throw new errors_1.CacheError(
|
|
200
|
+
throw new errors_1.CacheError(cacheErrorCodes_1.CACHE_ERROR_CODES.SESSION_GET_EXTEND_ERROR, {
|
|
201
|
+
adapter: 'session',
|
|
202
|
+
operation: 'getAndExtend',
|
|
203
|
+
details: { sessionId },
|
|
204
|
+
cause: error instanceof Error ? error : undefined,
|
|
205
|
+
});
|
|
150
206
|
}
|
|
151
207
|
}
|
|
152
208
|
/**
|
|
@@ -1,71 +1,18 @@
|
|
|
1
1
|
import { BaseCache } from '../../core/BaseCache';
|
|
2
2
|
import type { HealthCheckResponse, MemcacheCacheConfig } from '../../types';
|
|
3
|
-
/**
|
|
4
|
-
* Memcache adapter
|
|
5
|
-
*/
|
|
6
3
|
export declare class MemcacheCache<T = unknown> extends BaseCache<T> {
|
|
7
4
|
private memcacheConfig;
|
|
8
5
|
private client;
|
|
9
6
|
private isConnected;
|
|
10
7
|
constructor(memcacheConfig: MemcacheCacheConfig);
|
|
11
|
-
/**
|
|
12
|
-
* Connect to Memcache
|
|
13
|
-
*/
|
|
14
8
|
connect(): Promise<void>;
|
|
15
|
-
/**
|
|
16
|
-
* Ensure client is connected
|
|
17
|
-
*/
|
|
18
9
|
private ensureConnected;
|
|
19
|
-
/**
|
|
20
|
-
* Ping memcache
|
|
21
|
-
*/
|
|
22
10
|
private ping;
|
|
23
|
-
|
|
24
|
-
* Get a value from Memcache
|
|
25
|
-
*/
|
|
11
|
+
exists(key: string): Promise<boolean>;
|
|
26
12
|
get(key: string): Promise<T | null>;
|
|
27
|
-
/**
|
|
28
|
-
* Set a value in Memcache
|
|
29
|
-
*/
|
|
30
13
|
set(key: string, value: T, ttl?: number): Promise<void>;
|
|
31
|
-
/**
|
|
32
|
-
* Delete a key from Memcache
|
|
33
|
-
*/
|
|
34
14
|
delete(key: string): Promise<boolean>;
|
|
35
|
-
/**
|
|
36
|
-
* Check if key exists
|
|
37
|
-
*/
|
|
38
|
-
exists(key: string): Promise<boolean>;
|
|
39
|
-
/**
|
|
40
|
-
* Clear all keys (Memcache limitation: flushes entire cache)
|
|
41
|
-
*/
|
|
42
15
|
clear(): Promise<void>;
|
|
43
|
-
/**
|
|
44
|
-
* Get multiple values at once
|
|
45
|
-
*/
|
|
46
|
-
getMultiple(keys: string[]): Promise<Record<string, T | null>>;
|
|
47
|
-
/**
|
|
48
|
-
* Set multiple values at once
|
|
49
|
-
*/
|
|
50
|
-
setMultiple(data: Record<string, T>, ttl?: number): Promise<void>;
|
|
51
|
-
/**
|
|
52
|
-
* Delete multiple keys at once
|
|
53
|
-
*/
|
|
54
|
-
deleteMultiple(keys: string[]): Promise<number>;
|
|
55
|
-
/**
|
|
56
|
-
* Increment a numeric value (not natively supported by Memcache in this library)
|
|
57
|
-
*/
|
|
58
|
-
increment(key: string, amount?: number): Promise<number>;
|
|
59
|
-
/**
|
|
60
|
-
* Decrement a numeric value
|
|
61
|
-
*/
|
|
62
|
-
decrement(key: string, amount?: number): Promise<number>;
|
|
63
|
-
/**
|
|
64
|
-
* Check if Memcache is alive
|
|
65
|
-
*/
|
|
66
16
|
isAlive(): Promise<HealthCheckResponse>;
|
|
67
|
-
/**
|
|
68
|
-
* Close Memcache connection
|
|
69
|
-
*/
|
|
70
17
|
close(): Promise<void>;
|
|
71
18
|
}
|