@naman_deep_singh/cache 1.1.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 +637 -0
- package/dist/cjs/adapters/memcache/MemcacheCache.d.ts +71 -0
- package/dist/cjs/adapters/memcache/MemcacheCache.js +347 -0
- package/dist/cjs/adapters/memcache/index.d.ts +1 -0
- package/dist/cjs/adapters/memcache/index.js +5 -0
- package/dist/cjs/adapters/memory/MemoryCache.d.ts +63 -0
- package/dist/cjs/adapters/memory/MemoryCache.js +273 -0
- package/dist/cjs/adapters/memory/index.d.ts +1 -0
- package/dist/cjs/adapters/memory/index.js +5 -0
- package/dist/cjs/adapters/redis/RedisCache.d.ts +67 -0
- package/dist/cjs/adapters/redis/RedisCache.js +288 -0
- package/dist/cjs/adapters/redis/index.d.ts +1 -0
- package/dist/cjs/adapters/redis/index.js +5 -0
- package/dist/cjs/core/BaseCache.d.ts +78 -0
- package/dist/cjs/core/BaseCache.js +138 -0
- package/dist/cjs/core/factory.d.ts +16 -0
- package/dist/cjs/core/factory.js +51 -0
- package/dist/cjs/core/interfaces/ICache.d.ts +58 -0
- package/dist/cjs/core/interfaces/ICache.js +2 -0
- package/dist/cjs/core/interfaces/ISession.d.ts +30 -0
- package/dist/cjs/core/interfaces/ISession.js +2 -0
- package/dist/cjs/core/interfaces/index.d.ts +2 -0
- package/dist/cjs/core/interfaces/index.js +2 -0
- package/dist/cjs/errors/CacheError.d.ts +9 -0
- package/dist/cjs/errors/CacheError.js +17 -0
- package/dist/cjs/errors/index.d.ts +1 -0
- package/dist/cjs/errors/index.js +5 -0
- package/dist/cjs/index.d.ts +10 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/middleware/express/cacheMiddleware.d.ts +22 -0
- package/dist/cjs/middleware/express/cacheMiddleware.js +100 -0
- package/dist/cjs/middleware/express/index.d.ts +1 -0
- package/dist/cjs/middleware/express/index.js +7 -0
- package/dist/cjs/session/SessionStore.d.ts +51 -0
- package/dist/cjs/session/SessionStore.js +153 -0
- package/dist/cjs/session/index.d.ts +2 -0
- package/dist/cjs/session/index.js +5 -0
- package/dist/cjs/types.d.ts +83 -0
- package/dist/cjs/types.js +2 -0
- package/dist/esm/adapters/memcache/MemcacheCache.d.ts +71 -0
- package/dist/esm/adapters/memcache/MemcacheCache.js +340 -0
- package/dist/esm/adapters/memcache/index.d.ts +1 -0
- package/dist/esm/adapters/memcache/index.js +1 -0
- package/dist/esm/adapters/memory/MemoryCache.d.ts +63 -0
- package/dist/esm/adapters/memory/MemoryCache.js +269 -0
- package/dist/esm/adapters/memory/index.d.ts +1 -0
- package/dist/esm/adapters/memory/index.js +1 -0
- package/dist/esm/adapters/redis/RedisCache.d.ts +67 -0
- package/dist/esm/adapters/redis/RedisCache.js +284 -0
- package/dist/esm/adapters/redis/index.d.ts +1 -0
- package/dist/esm/adapters/redis/index.js +1 -0
- package/dist/esm/core/BaseCache.d.ts +78 -0
- package/dist/esm/core/BaseCache.js +134 -0
- package/dist/esm/core/factory.d.ts +16 -0
- package/dist/esm/core/factory.js +47 -0
- package/dist/esm/core/interfaces/ICache.d.ts +58 -0
- package/dist/esm/core/interfaces/ICache.js +1 -0
- package/dist/esm/core/interfaces/ISession.d.ts +30 -0
- package/dist/esm/core/interfaces/ISession.js +1 -0
- package/dist/esm/core/interfaces/index.d.ts +2 -0
- package/dist/esm/core/interfaces/index.js +1 -0
- package/dist/esm/errors/CacheError.d.ts +9 -0
- package/dist/esm/errors/CacheError.js +13 -0
- package/dist/esm/errors/index.d.ts +1 -0
- package/dist/esm/errors/index.js +1 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +14 -0
- package/dist/esm/middleware/express/cacheMiddleware.d.ts +22 -0
- package/dist/esm/middleware/express/cacheMiddleware.js +95 -0
- package/dist/esm/middleware/express/index.d.ts +1 -0
- package/dist/esm/middleware/express/index.js +1 -0
- package/dist/esm/session/SessionStore.d.ts +51 -0
- package/dist/esm/session/SessionStore.js +149 -0
- package/dist/esm/session/index.d.ts +2 -0
- package/dist/esm/session/index.js +1 -0
- package/dist/esm/types.d.ts +83 -0
- package/dist/esm/types.js +1 -0
- package/dist/types/adapters/memcache/MemcacheCache.d.ts +71 -0
- package/dist/types/adapters/memcache/index.d.ts +1 -0
- package/dist/types/adapters/memory/MemoryCache.d.ts +63 -0
- package/dist/types/adapters/memory/index.d.ts +1 -0
- package/dist/types/adapters/redis/RedisCache.d.ts +67 -0
- package/dist/types/adapters/redis/index.d.ts +1 -0
- package/dist/types/core/BaseCache.d.ts +78 -0
- package/dist/types/core/factory.d.ts +16 -0
- package/dist/types/core/interfaces/ICache.d.ts +58 -0
- package/dist/types/core/interfaces/ISession.d.ts +30 -0
- package/dist/types/core/interfaces/index.d.ts +2 -0
- package/dist/types/errors/CacheError.d.ts +9 -0
- package/dist/types/errors/index.d.ts +1 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/middleware/express/cacheMiddleware.d.ts +22 -0
- package/dist/types/middleware/express/index.d.ts +1 -0
- package/dist/types/session/SessionStore.d.ts +51 -0
- package/dist/types/session/index.d.ts +2 -0
- package/dist/types/types.d.ts +83 -0
- package/package.json +48 -0
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import type { MemcacheCacheConfig, HealthCheckResponse } from '../../types';
|
|
2
|
+
import { BaseCache } from '../../core/BaseCache';
|
|
3
|
+
/**
|
|
4
|
+
* Memcache adapter
|
|
5
|
+
*/
|
|
6
|
+
export declare class MemcacheCache<T = unknown> extends BaseCache<T> {
|
|
7
|
+
private memcacheConfig;
|
|
8
|
+
private client;
|
|
9
|
+
private isConnected;
|
|
10
|
+
constructor(memcacheConfig: MemcacheCacheConfig);
|
|
11
|
+
/**
|
|
12
|
+
* Connect to Memcache
|
|
13
|
+
*/
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Ensure client is connected
|
|
17
|
+
*/
|
|
18
|
+
private ensureConnected;
|
|
19
|
+
/**
|
|
20
|
+
* Ping memcache
|
|
21
|
+
*/
|
|
22
|
+
private ping;
|
|
23
|
+
/**
|
|
24
|
+
* Get a value from Memcache
|
|
25
|
+
*/
|
|
26
|
+
get(key: string): Promise<T | null>;
|
|
27
|
+
/**
|
|
28
|
+
* Set a value in Memcache
|
|
29
|
+
*/
|
|
30
|
+
set(key: string, value: T, ttl?: number): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Delete a key from Memcache
|
|
33
|
+
*/
|
|
34
|
+
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
|
+
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
|
+
isAlive(): Promise<HealthCheckResponse>;
|
|
67
|
+
/**
|
|
68
|
+
* Close Memcache connection
|
|
69
|
+
*/
|
|
70
|
+
close(): Promise<void>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import Memcached from 'memcached';
|
|
2
|
+
import { BaseCache } from '../../core/BaseCache';
|
|
3
|
+
import { CacheError } from '../../errors';
|
|
4
|
+
/**
|
|
5
|
+
* Memcache adapter
|
|
6
|
+
*/
|
|
7
|
+
export class MemcacheCache extends BaseCache {
|
|
8
|
+
constructor(memcacheConfig) {
|
|
9
|
+
super(memcacheConfig);
|
|
10
|
+
this.memcacheConfig = memcacheConfig;
|
|
11
|
+
this.client = null;
|
|
12
|
+
this.isConnected = false;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Connect to Memcache
|
|
16
|
+
*/
|
|
17
|
+
async connect() {
|
|
18
|
+
try {
|
|
19
|
+
const servers = Array.isArray(this.memcacheConfig.servers)
|
|
20
|
+
? this.memcacheConfig.servers
|
|
21
|
+
: [this.memcacheConfig.servers];
|
|
22
|
+
const options = {
|
|
23
|
+
retries: 2,
|
|
24
|
+
retry: 30000,
|
|
25
|
+
remove: true,
|
|
26
|
+
failOverServers: [],
|
|
27
|
+
maxValue: 1048576, // 1MB default
|
|
28
|
+
idle: 30000
|
|
29
|
+
};
|
|
30
|
+
if (this.memcacheConfig.username) {
|
|
31
|
+
options.username = this.memcacheConfig.username;
|
|
32
|
+
}
|
|
33
|
+
if (this.memcacheConfig.password) {
|
|
34
|
+
options.password = this.memcacheConfig.password;
|
|
35
|
+
}
|
|
36
|
+
this.client = new Memcached(servers, options);
|
|
37
|
+
this.isConnected = true;
|
|
38
|
+
// Test connection
|
|
39
|
+
await this.ping();
|
|
40
|
+
}
|
|
41
|
+
catch (err) {
|
|
42
|
+
throw new CacheError('Failed to connect to Memcache', 'MEMCACHE_CONNECTION_ERROR', 'memcache', err);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Ensure client is connected
|
|
47
|
+
*/
|
|
48
|
+
async ensureConnected() {
|
|
49
|
+
if (!this.client) {
|
|
50
|
+
await this.connect();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Ping memcache
|
|
55
|
+
*/
|
|
56
|
+
async ping() {
|
|
57
|
+
return new Promise((resolve, reject) => {
|
|
58
|
+
if (!this.client) {
|
|
59
|
+
reject(new Error('Memcache client not initialized'));
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.client.touch('__ping', 1, (err) => {
|
|
63
|
+
if (err)
|
|
64
|
+
reject(err);
|
|
65
|
+
else
|
|
66
|
+
resolve();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get a value from Memcache
|
|
72
|
+
*/
|
|
73
|
+
async get(key) {
|
|
74
|
+
try {
|
|
75
|
+
await this.ensureConnected();
|
|
76
|
+
const fullKey = this.buildKey(key);
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
this.client.get(fullKey, (err, data) => {
|
|
79
|
+
if (err) {
|
|
80
|
+
reject(err);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
if (data === undefined) {
|
|
84
|
+
this.recordMiss();
|
|
85
|
+
resolve(null);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
this.recordHit();
|
|
89
|
+
try {
|
|
90
|
+
resolve(this.deserialize(data));
|
|
91
|
+
}
|
|
92
|
+
catch (parseErr) {
|
|
93
|
+
reject(parseErr);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
throw new CacheError(`Failed to get key "${key}" from Memcache`, 'MEMCACHE_GET_ERROR', 'memcache', err);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Set a value in Memcache
|
|
105
|
+
*/
|
|
106
|
+
async set(key, value, ttl) {
|
|
107
|
+
try {
|
|
108
|
+
await this.ensureConnected();
|
|
109
|
+
const fullKey = this.buildKey(key);
|
|
110
|
+
const serialized = this.serialize(value);
|
|
111
|
+
const expiry = ttl ?? this.ttl;
|
|
112
|
+
return new Promise((resolve, reject) => {
|
|
113
|
+
this.client.set(fullKey, serialized, expiry, (err) => {
|
|
114
|
+
if (err) {
|
|
115
|
+
reject(err);
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
this.recordSet();
|
|
119
|
+
resolve();
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
throw new CacheError(`Failed to set key "${key}" in Memcache`, 'MEMCACHE_SET_ERROR', 'memcache', err);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Delete a key from Memcache
|
|
130
|
+
*/
|
|
131
|
+
async delete(key) {
|
|
132
|
+
try {
|
|
133
|
+
await this.ensureConnected();
|
|
134
|
+
const fullKey = this.buildKey(key);
|
|
135
|
+
return new Promise((resolve, reject) => {
|
|
136
|
+
this.client.del(fullKey, (err) => {
|
|
137
|
+
if (err) {
|
|
138
|
+
reject(err);
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this.recordDelete();
|
|
142
|
+
resolve(true);
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
catch (err) {
|
|
148
|
+
throw new CacheError(`Failed to delete key "${key}" from Memcache`, 'MEMCACHE_DELETE_ERROR', 'memcache', err);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Check if key exists
|
|
153
|
+
*/
|
|
154
|
+
async exists(key) {
|
|
155
|
+
try {
|
|
156
|
+
const value = await this.get(key);
|
|
157
|
+
return value !== null;
|
|
158
|
+
}
|
|
159
|
+
catch (err) {
|
|
160
|
+
throw new CacheError(`Failed to check existence of key "${key}" in Memcache`, 'MEMCACHE_EXISTS_ERROR', 'memcache', err);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Clear all keys (Memcache limitation: flushes entire cache)
|
|
165
|
+
*/
|
|
166
|
+
async clear() {
|
|
167
|
+
try {
|
|
168
|
+
await this.ensureConnected();
|
|
169
|
+
return new Promise((resolve, reject) => {
|
|
170
|
+
this.client.flush((err) => {
|
|
171
|
+
if (err) {
|
|
172
|
+
reject(err);
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
resolve();
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
catch (err) {
|
|
181
|
+
throw new CacheError('Failed to clear Memcache', 'MEMCACHE_CLEAR_ERROR', 'memcache', err);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Get multiple values at once
|
|
186
|
+
*/
|
|
187
|
+
async getMultiple(keys) {
|
|
188
|
+
try {
|
|
189
|
+
await this.ensureConnected();
|
|
190
|
+
const fullKeys = keys.map(k => this.buildKey(k));
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
this.client.getMulti(fullKeys, (err, data) => {
|
|
193
|
+
if (err) {
|
|
194
|
+
reject(err);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const result = {};
|
|
198
|
+
keys.forEach((key) => {
|
|
199
|
+
const fullKey = this.buildKey(key);
|
|
200
|
+
if (fullKey in data) {
|
|
201
|
+
this.recordHit();
|
|
202
|
+
try {
|
|
203
|
+
result[key] = this.deserialize(data[fullKey]);
|
|
204
|
+
}
|
|
205
|
+
catch (parseErr) {
|
|
206
|
+
reject(parseErr);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
this.recordMiss();
|
|
211
|
+
result[key] = null;
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
resolve(result);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
catch (err) {
|
|
219
|
+
throw new CacheError('Failed to get multiple keys from Memcache', 'MEMCACHE_GET_MULTIPLE_ERROR', 'memcache', err);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Set multiple values at once
|
|
224
|
+
*/
|
|
225
|
+
async setMultiple(data, ttl) {
|
|
226
|
+
try {
|
|
227
|
+
await this.ensureConnected();
|
|
228
|
+
const expiry = ttl ?? this.ttl;
|
|
229
|
+
const promises = [];
|
|
230
|
+
for (const [key, value] of Object.entries(data)) {
|
|
231
|
+
const fullKey = this.buildKey(key);
|
|
232
|
+
const serialized = this.serialize(value);
|
|
233
|
+
promises.push(new Promise((resolve, reject) => {
|
|
234
|
+
this.client.set(fullKey, serialized, expiry, (err) => {
|
|
235
|
+
if (err)
|
|
236
|
+
reject(err);
|
|
237
|
+
else
|
|
238
|
+
resolve();
|
|
239
|
+
});
|
|
240
|
+
}));
|
|
241
|
+
}
|
|
242
|
+
return Promise.all(promises).then(() => {
|
|
243
|
+
this.stats.sets += Object.keys(data).length;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
catch (err) {
|
|
247
|
+
throw new CacheError('Failed to set multiple keys in Memcache', 'MEMCACHE_SET_MULTIPLE_ERROR', 'memcache', err);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Delete multiple keys at once
|
|
252
|
+
*/
|
|
253
|
+
async deleteMultiple(keys) {
|
|
254
|
+
try {
|
|
255
|
+
await this.ensureConnected();
|
|
256
|
+
const fullKeys = keys.map(k => this.buildKey(k));
|
|
257
|
+
let deletedCount = 0;
|
|
258
|
+
await Promise.all(fullKeys.map(key => new Promise((resolve, reject) => {
|
|
259
|
+
this.client.del(key, (err) => {
|
|
260
|
+
if (err) {
|
|
261
|
+
reject(err);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
deletedCount++;
|
|
265
|
+
resolve();
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
})));
|
|
269
|
+
this.stats.deletes += deletedCount;
|
|
270
|
+
return deletedCount;
|
|
271
|
+
}
|
|
272
|
+
catch (err) {
|
|
273
|
+
throw new CacheError('Failed to delete multiple keys from Memcache', 'MEMCACHE_DELETE_MULTIPLE_ERROR', 'memcache', err);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Increment a numeric value (not natively supported by Memcache in this library)
|
|
278
|
+
*/
|
|
279
|
+
async increment(key, amount = 1) {
|
|
280
|
+
try {
|
|
281
|
+
const current = await this.get(key);
|
|
282
|
+
const value = (typeof current === 'number' ? current : 0) + amount;
|
|
283
|
+
await this.set(key, value);
|
|
284
|
+
return value;
|
|
285
|
+
}
|
|
286
|
+
catch (err) {
|
|
287
|
+
throw new CacheError(`Failed to increment key "${key}" in Memcache`, 'MEMCACHE_INCREMENT_ERROR', 'memcache', err);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Decrement a numeric value
|
|
292
|
+
*/
|
|
293
|
+
async decrement(key, amount = 1) {
|
|
294
|
+
try {
|
|
295
|
+
const current = await this.get(key);
|
|
296
|
+
const value = (typeof current === 'number' ? current : 0) - amount;
|
|
297
|
+
await this.set(key, value);
|
|
298
|
+
return value;
|
|
299
|
+
}
|
|
300
|
+
catch (err) {
|
|
301
|
+
throw new CacheError(`Failed to decrement key "${key}" in Memcache`, 'MEMCACHE_DECREMENT_ERROR', 'memcache', err);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Check if Memcache is alive
|
|
306
|
+
*/
|
|
307
|
+
async isAlive() {
|
|
308
|
+
try {
|
|
309
|
+
await this.ping();
|
|
310
|
+
return {
|
|
311
|
+
isAlive: true,
|
|
312
|
+
adapter: 'memcache',
|
|
313
|
+
timestamp: new Date()
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
catch (err) {
|
|
317
|
+
return {
|
|
318
|
+
isAlive: false,
|
|
319
|
+
adapter: 'memcache',
|
|
320
|
+
timestamp: new Date(),
|
|
321
|
+
error: err.message
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Close Memcache connection
|
|
327
|
+
*/
|
|
328
|
+
async close() {
|
|
329
|
+
try {
|
|
330
|
+
if (this.client && this.isConnected) {
|
|
331
|
+
this.client.end();
|
|
332
|
+
this.isConnected = false;
|
|
333
|
+
this.client = null;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
catch (err) {
|
|
337
|
+
throw new CacheError('Failed to close Memcache connection', 'MEMCACHE_CLOSE_ERROR', 'memcache', err);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MemcacheCache } from './MemcacheCache';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MemcacheCache } from './MemcacheCache';
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { MemoryCacheConfig, HealthCheckResponse } from '../../types';
|
|
2
|
+
import { BaseCache } from '../../core/BaseCache';
|
|
3
|
+
/**
|
|
4
|
+
* In-memory cache adapter for development and testing
|
|
5
|
+
*/
|
|
6
|
+
export declare class MemoryCache<T = unknown> extends BaseCache<T> {
|
|
7
|
+
private memoryCacheConfig;
|
|
8
|
+
private store;
|
|
9
|
+
private cleanupInterval;
|
|
10
|
+
constructor(memoryCacheConfig: MemoryCacheConfig);
|
|
11
|
+
/**
|
|
12
|
+
* Start periodic cleanup of expired items
|
|
13
|
+
*/
|
|
14
|
+
private startCleanup;
|
|
15
|
+
/**
|
|
16
|
+
* Get a value from memory
|
|
17
|
+
*/
|
|
18
|
+
get(key: string): Promise<T | null>;
|
|
19
|
+
/**
|
|
20
|
+
* Set a value in memory
|
|
21
|
+
*/
|
|
22
|
+
set(key: string, value: T, ttl?: number): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Delete a key from memory
|
|
25
|
+
*/
|
|
26
|
+
delete(key: string): Promise<boolean>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if key exists
|
|
29
|
+
*/
|
|
30
|
+
exists(key: string): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Clear all keys with current namespace
|
|
33
|
+
*/
|
|
34
|
+
clear(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Get multiple values at once
|
|
37
|
+
*/
|
|
38
|
+
getMultiple(keys: string[]): Promise<Record<string, T | null>>;
|
|
39
|
+
/**
|
|
40
|
+
* Set multiple values at once
|
|
41
|
+
*/
|
|
42
|
+
setMultiple(data: Record<string, T>, ttl?: number): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Delete multiple keys at once
|
|
45
|
+
*/
|
|
46
|
+
deleteMultiple(keys: string[]): Promise<number>;
|
|
47
|
+
/**
|
|
48
|
+
* Increment a numeric value
|
|
49
|
+
*/
|
|
50
|
+
increment(key: string, amount?: number): Promise<number>;
|
|
51
|
+
/**
|
|
52
|
+
* Decrement a numeric value
|
|
53
|
+
*/
|
|
54
|
+
decrement(key: string, amount?: number): Promise<number>;
|
|
55
|
+
/**
|
|
56
|
+
* Check if memory cache is alive
|
|
57
|
+
*/
|
|
58
|
+
isAlive(): Promise<HealthCheckResponse>;
|
|
59
|
+
/**
|
|
60
|
+
* Close memory cache
|
|
61
|
+
*/
|
|
62
|
+
close(): Promise<void>;
|
|
63
|
+
}
|