@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,149 @@
|
|
|
1
|
+
import { CacheError } from '../errors';
|
|
2
|
+
/**
|
|
3
|
+
* Session store wrapper around cache adapters
|
|
4
|
+
* Provides session management functionality
|
|
5
|
+
*/
|
|
6
|
+
export class SessionStore {
|
|
7
|
+
constructor(cache, options = {}) {
|
|
8
|
+
this.cache = cache;
|
|
9
|
+
this.options = {
|
|
10
|
+
ttl: options.ttl ?? 3600, // 1 hour default
|
|
11
|
+
serialize: options.serialize ?? ((data) => JSON.stringify(data)),
|
|
12
|
+
deserialize: options.deserialize ?? ((data) => JSON.parse(data))
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Create a new session
|
|
17
|
+
*/
|
|
18
|
+
async create(sessionId, data, ttl) {
|
|
19
|
+
try {
|
|
20
|
+
const ttlValue = ttl ?? this.options.ttl;
|
|
21
|
+
await this.cache.set(sessionId, data, ttlValue);
|
|
22
|
+
}
|
|
23
|
+
catch (err) {
|
|
24
|
+
throw new CacheError(`Failed to create session "${sessionId}"`, 'SESSION_CREATE_ERROR', 'session', err);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get session data
|
|
29
|
+
*/
|
|
30
|
+
async get(sessionId) {
|
|
31
|
+
try {
|
|
32
|
+
return await this.cache.get(sessionId);
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
throw new CacheError(`Failed to get session "${sessionId}"`, 'SESSION_GET_ERROR', 'session', err);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Update/merge session data
|
|
40
|
+
*/
|
|
41
|
+
async update(sessionId, data) {
|
|
42
|
+
try {
|
|
43
|
+
const current = await this.cache.get(sessionId);
|
|
44
|
+
if (!current) {
|
|
45
|
+
throw new CacheError(`Session "${sessionId}" not found`, 'SESSION_NOT_FOUND');
|
|
46
|
+
}
|
|
47
|
+
const merged = { ...current, ...data };
|
|
48
|
+
await this.cache.set(sessionId, merged, this.options.ttl);
|
|
49
|
+
}
|
|
50
|
+
catch (err) {
|
|
51
|
+
if (err instanceof CacheError) {
|
|
52
|
+
throw err;
|
|
53
|
+
}
|
|
54
|
+
throw new CacheError(`Failed to update session "${sessionId}"`, 'SESSION_UPDATE_ERROR', 'session', err);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Delete session
|
|
59
|
+
*/
|
|
60
|
+
async delete(sessionId) {
|
|
61
|
+
try {
|
|
62
|
+
return await this.cache.delete(sessionId);
|
|
63
|
+
}
|
|
64
|
+
catch (err) {
|
|
65
|
+
throw new CacheError(`Failed to delete session "${sessionId}"`, 'SESSION_DELETE_ERROR', 'session', err);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Check if session exists
|
|
70
|
+
*/
|
|
71
|
+
async exists(sessionId) {
|
|
72
|
+
try {
|
|
73
|
+
return await this.cache.exists(sessionId);
|
|
74
|
+
}
|
|
75
|
+
catch (err) {
|
|
76
|
+
throw new CacheError(`Failed to check session "${sessionId}" existence`, 'SESSION_EXISTS_ERROR', 'session', err);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Clear all sessions
|
|
81
|
+
*/
|
|
82
|
+
async clear() {
|
|
83
|
+
try {
|
|
84
|
+
await this.cache.clear();
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
throw new CacheError('Failed to clear sessions', 'SESSION_CLEAR_ERROR', 'session', err);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Get all session keys (limited use - may be slow with large datasets)
|
|
92
|
+
*/
|
|
93
|
+
async getMultiple(sessionIds) {
|
|
94
|
+
try {
|
|
95
|
+
return await this.cache.getMultiple(sessionIds);
|
|
96
|
+
}
|
|
97
|
+
catch (err) {
|
|
98
|
+
throw new CacheError('Failed to get multiple sessions', 'SESSION_GET_MULTIPLE_ERROR', 'session', err);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Delete multiple sessions
|
|
103
|
+
*/
|
|
104
|
+
async deleteMultiple(sessionIds) {
|
|
105
|
+
try {
|
|
106
|
+
return await this.cache.deleteMultiple(sessionIds);
|
|
107
|
+
}
|
|
108
|
+
catch (err) {
|
|
109
|
+
throw new CacheError('Failed to delete multiple sessions', 'SESSION_DELETE_MULTIPLE_ERROR', 'session', err);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Extend session expiry
|
|
114
|
+
*/
|
|
115
|
+
async extend(sessionId, ttl) {
|
|
116
|
+
try {
|
|
117
|
+
const current = await this.cache.get(sessionId);
|
|
118
|
+
if (!current) {
|
|
119
|
+
throw new CacheError(`Session "${sessionId}" not found`, 'SESSION_NOT_FOUND');
|
|
120
|
+
}
|
|
121
|
+
const ttlValue = ttl ?? this.options.ttl;
|
|
122
|
+
await this.cache.set(sessionId, current, ttlValue);
|
|
123
|
+
}
|
|
124
|
+
catch (err) {
|
|
125
|
+
if (err instanceof CacheError) {
|
|
126
|
+
throw err;
|
|
127
|
+
}
|
|
128
|
+
throw new CacheError(`Failed to extend session "${sessionId}"`, 'SESSION_EXTEND_ERROR', 'session', err);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Get session data and extend expiry in one operation
|
|
133
|
+
*/
|
|
134
|
+
async getAndExtend(sessionId, ttl) {
|
|
135
|
+
try {
|
|
136
|
+
const data = await this.get(sessionId);
|
|
137
|
+
if (data) {
|
|
138
|
+
await this.extend(sessionId, ttl);
|
|
139
|
+
}
|
|
140
|
+
return data;
|
|
141
|
+
}
|
|
142
|
+
catch (err) {
|
|
143
|
+
if (err instanceof CacheError) {
|
|
144
|
+
throw err;
|
|
145
|
+
}
|
|
146
|
+
throw new CacheError(`Failed to get and extend session "${sessionId}"`, 'SESSION_GET_EXTEND_ERROR', 'session', err);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { SessionStore } from './SessionStore';
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache configuration options
|
|
3
|
+
*/
|
|
4
|
+
export interface CacheConfig {
|
|
5
|
+
adapter: 'redis' | 'memcache' | 'memory';
|
|
6
|
+
namespace?: string;
|
|
7
|
+
ttl?: number;
|
|
8
|
+
fallback?: boolean;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Redis-specific configuration
|
|
12
|
+
*/
|
|
13
|
+
export interface RedisCacheConfig extends CacheConfig {
|
|
14
|
+
adapter: 'redis';
|
|
15
|
+
host?: string;
|
|
16
|
+
port?: number;
|
|
17
|
+
username?: string;
|
|
18
|
+
password?: string;
|
|
19
|
+
db?: number;
|
|
20
|
+
tls?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Memcache-specific configuration
|
|
24
|
+
*/
|
|
25
|
+
export interface MemcacheCacheConfig extends CacheConfig {
|
|
26
|
+
adapter: 'memcache';
|
|
27
|
+
servers: string | string[];
|
|
28
|
+
username?: string;
|
|
29
|
+
password?: string;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Memory cache configuration
|
|
33
|
+
*/
|
|
34
|
+
export interface MemoryCacheConfig extends CacheConfig {
|
|
35
|
+
adapter: 'memory';
|
|
36
|
+
maxSize?: number;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Session data structure
|
|
40
|
+
*/
|
|
41
|
+
export interface SessionData {
|
|
42
|
+
[key: string]: unknown;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Session store options
|
|
46
|
+
*/
|
|
47
|
+
export interface SessionOptions {
|
|
48
|
+
ttl?: number;
|
|
49
|
+
serialize?: (data: SessionData) => string;
|
|
50
|
+
deserialize?: (data: string) => SessionData;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Cache statistics
|
|
54
|
+
*/
|
|
55
|
+
export interface CacheStats {
|
|
56
|
+
hits: number;
|
|
57
|
+
misses: number;
|
|
58
|
+
sets: number;
|
|
59
|
+
deletes: number;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Health check response
|
|
63
|
+
*/
|
|
64
|
+
export interface HealthCheckResponse {
|
|
65
|
+
isAlive: boolean;
|
|
66
|
+
adapter: string;
|
|
67
|
+
timestamp: Date;
|
|
68
|
+
error?: string;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Batch operation result
|
|
72
|
+
*/
|
|
73
|
+
export interface BatchResult<T> {
|
|
74
|
+
[key: string]: T | null;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Cache key with optional metadata
|
|
78
|
+
*/
|
|
79
|
+
export interface CacheEntry<T = unknown> {
|
|
80
|
+
value: T;
|
|
81
|
+
expiresAt?: number;
|
|
82
|
+
createdAt: number;
|
|
83
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -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 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { MemoryCache } from './MemoryCache';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { RedisCacheConfig, HealthCheckResponse } from '../../types';
|
|
2
|
+
import { BaseCache } from '../../core/BaseCache';
|
|
3
|
+
/**
|
|
4
|
+
* Redis cache adapter
|
|
5
|
+
*/
|
|
6
|
+
export declare class RedisCache<T = unknown> extends BaseCache<T> {
|
|
7
|
+
private redisConfig;
|
|
8
|
+
private client;
|
|
9
|
+
private isConnected;
|
|
10
|
+
constructor(redisConfig: RedisCacheConfig);
|
|
11
|
+
/**
|
|
12
|
+
* Connect to Redis
|
|
13
|
+
*/
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Ensure client is connected
|
|
17
|
+
*/
|
|
18
|
+
private ensureConnected;
|
|
19
|
+
/**
|
|
20
|
+
* Get a value from Redis
|
|
21
|
+
*/
|
|
22
|
+
get(key: string): Promise<T | null>;
|
|
23
|
+
/**
|
|
24
|
+
* Set a value in Redis
|
|
25
|
+
*/
|
|
26
|
+
set(key: string, value: T, ttl?: number): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Delete a key from Redis
|
|
29
|
+
*/
|
|
30
|
+
delete(key: string): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Check if key exists
|
|
33
|
+
*/
|
|
34
|
+
exists(key: string): Promise<boolean>;
|
|
35
|
+
/**
|
|
36
|
+
* Clear all keys with current namespace
|
|
37
|
+
*/
|
|
38
|
+
clear(): Promise<void>;
|
|
39
|
+
/**
|
|
40
|
+
* Get multiple values at once
|
|
41
|
+
*/
|
|
42
|
+
getMultiple(keys: string[]): Promise<Record<string, T | null>>;
|
|
43
|
+
/**
|
|
44
|
+
* Set multiple values at once
|
|
45
|
+
*/
|
|
46
|
+
setMultiple(data: Record<string, T>, ttl?: number): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Delete multiple keys at once
|
|
49
|
+
*/
|
|
50
|
+
deleteMultiple(keys: string[]): Promise<number>;
|
|
51
|
+
/**
|
|
52
|
+
* Increment a numeric value
|
|
53
|
+
*/
|
|
54
|
+
increment(key: string, amount?: number): Promise<number>;
|
|
55
|
+
/**
|
|
56
|
+
* Decrement a numeric value
|
|
57
|
+
*/
|
|
58
|
+
decrement(key: string, amount?: number): Promise<number>;
|
|
59
|
+
/**
|
|
60
|
+
* Check if Redis is alive
|
|
61
|
+
*/
|
|
62
|
+
isAlive(): Promise<HealthCheckResponse>;
|
|
63
|
+
/**
|
|
64
|
+
* Close Redis connection
|
|
65
|
+
*/
|
|
66
|
+
close(): Promise<void>;
|
|
67
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { RedisCache } from './RedisCache';
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { CacheConfig, CacheStats, HealthCheckResponse, BatchResult } from '../types';
|
|
2
|
+
import type { ICache } from './interfaces';
|
|
3
|
+
/**
|
|
4
|
+
* Abstract base class for all cache adapters
|
|
5
|
+
*/
|
|
6
|
+
export declare abstract class BaseCache<T = unknown> implements ICache<T> {
|
|
7
|
+
protected config: CacheConfig;
|
|
8
|
+
protected readonly namespace: string;
|
|
9
|
+
protected readonly ttl: number;
|
|
10
|
+
protected stats: CacheStats;
|
|
11
|
+
constructor(config: CacheConfig);
|
|
12
|
+
/**
|
|
13
|
+
* Build full key with namespace prefix
|
|
14
|
+
*/
|
|
15
|
+
protected buildKey(key: string): string;
|
|
16
|
+
/**
|
|
17
|
+
* Deserialize string to object
|
|
18
|
+
*/
|
|
19
|
+
protected deserialize(data: string): T;
|
|
20
|
+
/**
|
|
21
|
+
* Serialize object to string
|
|
22
|
+
*/
|
|
23
|
+
protected serialize(value: T): string;
|
|
24
|
+
/**
|
|
25
|
+
* Track cache hit
|
|
26
|
+
*/
|
|
27
|
+
protected recordHit(): void;
|
|
28
|
+
/**
|
|
29
|
+
* Track cache miss
|
|
30
|
+
*/
|
|
31
|
+
protected recordMiss(): void;
|
|
32
|
+
/**
|
|
33
|
+
* Track cache set
|
|
34
|
+
*/
|
|
35
|
+
protected recordSet(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Track cache delete
|
|
38
|
+
*/
|
|
39
|
+
protected recordDelete(): void;
|
|
40
|
+
/**
|
|
41
|
+
* Reset statistics
|
|
42
|
+
*/
|
|
43
|
+
protected resetStats(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Get cache statistics
|
|
46
|
+
*/
|
|
47
|
+
getStats(): Promise<CacheStats>;
|
|
48
|
+
/**
|
|
49
|
+
* Abstract methods - must be implemented by subclasses
|
|
50
|
+
*/
|
|
51
|
+
abstract get(key: string): Promise<T | null>;
|
|
52
|
+
abstract set(key: string, value: T, ttl?: number): Promise<void>;
|
|
53
|
+
abstract delete(key: string): Promise<boolean>;
|
|
54
|
+
abstract exists(key: string): Promise<boolean>;
|
|
55
|
+
abstract clear(): Promise<void>;
|
|
56
|
+
abstract isAlive(): Promise<HealthCheckResponse>;
|
|
57
|
+
abstract close(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Default implementation for getMultiple
|
|
60
|
+
*/
|
|
61
|
+
getMultiple(keys: string[]): Promise<BatchResult<T>>;
|
|
62
|
+
/**
|
|
63
|
+
* Default implementation for setMultiple
|
|
64
|
+
*/
|
|
65
|
+
setMultiple(data: Record<string, T>, ttl?: number): Promise<void>;
|
|
66
|
+
/**
|
|
67
|
+
* Default implementation for deleteMultiple
|
|
68
|
+
*/
|
|
69
|
+
deleteMultiple(keys: string[]): Promise<number>;
|
|
70
|
+
/**
|
|
71
|
+
* Default implementation for increment
|
|
72
|
+
*/
|
|
73
|
+
increment(key: string, amount?: number): Promise<number>;
|
|
74
|
+
/**
|
|
75
|
+
* Default implementation for decrement
|
|
76
|
+
*/
|
|
77
|
+
decrement(key: string, amount?: number): Promise<number>;
|
|
78
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CacheConfig } from '../types';
|
|
2
|
+
import type { ICache } from './interfaces';
|
|
3
|
+
/**
|
|
4
|
+
* Factory for creating cache instances
|
|
5
|
+
*/
|
|
6
|
+
export declare class CacheFactory {
|
|
7
|
+
/**
|
|
8
|
+
* Create a cache instance based on configuration
|
|
9
|
+
*/
|
|
10
|
+
static create<T = unknown>(config: CacheConfig): ICache<T>;
|
|
11
|
+
/**
|
|
12
|
+
* Create a cache with fallback support
|
|
13
|
+
* If primary adapter fails to connect, falls back to memory cache
|
|
14
|
+
*/
|
|
15
|
+
static createWithFallback<T = unknown>(config: CacheConfig): Promise<ICache<T>>;
|
|
16
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { CacheStats, HealthCheckResponse, BatchResult } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Main cache interface - defines all cache operations
|
|
4
|
+
*/
|
|
5
|
+
export interface ICache<T = unknown> {
|
|
6
|
+
/**
|
|
7
|
+
* Get a value from cache
|
|
8
|
+
*/
|
|
9
|
+
get(key: string): Promise<T | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Set a value in cache
|
|
12
|
+
*/
|
|
13
|
+
set(key: string, value: T, ttl?: number): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Delete a value from cache
|
|
16
|
+
*/
|
|
17
|
+
delete(key: string): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Check if key exists
|
|
20
|
+
*/
|
|
21
|
+
exists(key: string): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Clear all cache entries
|
|
24
|
+
*/
|
|
25
|
+
clear(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Get multiple values at once
|
|
28
|
+
*/
|
|
29
|
+
getMultiple(keys: string[]): Promise<BatchResult<T>>;
|
|
30
|
+
/**
|
|
31
|
+
* Set multiple values at once
|
|
32
|
+
*/
|
|
33
|
+
setMultiple(data: Record<string, T>, ttl?: number): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Delete multiple keys at once
|
|
36
|
+
*/
|
|
37
|
+
deleteMultiple(keys: string[]): Promise<number>;
|
|
38
|
+
/**
|
|
39
|
+
* Increment a numeric value
|
|
40
|
+
*/
|
|
41
|
+
increment(key: string, amount?: number): Promise<number>;
|
|
42
|
+
/**
|
|
43
|
+
* Decrement a numeric value
|
|
44
|
+
*/
|
|
45
|
+
decrement(key: string, amount?: number): Promise<number>;
|
|
46
|
+
/**
|
|
47
|
+
* Get cache statistics
|
|
48
|
+
*/
|
|
49
|
+
getStats?(): Promise<CacheStats>;
|
|
50
|
+
/**
|
|
51
|
+
* Check cache health
|
|
52
|
+
*/
|
|
53
|
+
isAlive(): Promise<HealthCheckResponse>;
|
|
54
|
+
/**
|
|
55
|
+
* Close/disconnect cache connection
|
|
56
|
+
*/
|
|
57
|
+
close(): Promise<void>;
|
|
58
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { SessionData } from '../../types';
|
|
2
|
+
/**
|
|
3
|
+
* Session store interface
|
|
4
|
+
*/
|
|
5
|
+
export interface ISession {
|
|
6
|
+
/**
|
|
7
|
+
* Get session data
|
|
8
|
+
*/
|
|
9
|
+
get(sessionId: string): Promise<SessionData | null>;
|
|
10
|
+
/**
|
|
11
|
+
* Set session data
|
|
12
|
+
*/
|
|
13
|
+
set(sessionId: string, data: SessionData, ttl?: number): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Delete session
|
|
16
|
+
*/
|
|
17
|
+
delete(sessionId: string): Promise<boolean>;
|
|
18
|
+
/**
|
|
19
|
+
* Check if session exists
|
|
20
|
+
*/
|
|
21
|
+
exists(sessionId: string): Promise<boolean>;
|
|
22
|
+
/**
|
|
23
|
+
* Clear all sessions
|
|
24
|
+
*/
|
|
25
|
+
clear(): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Update/merge session data
|
|
28
|
+
*/
|
|
29
|
+
update(sessionId: string, data: Partial<SessionData>): Promise<void>;
|
|
30
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error class for cache operations
|
|
3
|
+
*/
|
|
4
|
+
export declare class CacheError extends Error {
|
|
5
|
+
readonly code: string;
|
|
6
|
+
readonly adapter?: string;
|
|
7
|
+
readonly originalError?: Error;
|
|
8
|
+
constructor(message: string, code?: string, adapter?: string, originalError?: Error);
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { CacheError } from './CacheError';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type { CacheConfig, RedisCacheConfig, MemcacheCacheConfig, MemoryCacheConfig, SessionData, SessionOptions, CacheStats, HealthCheckResponse, BatchResult, CacheEntry } from './types';
|
|
2
|
+
export { CacheError } from './errors';
|
|
3
|
+
export type { ICache, ISession } from './core/interfaces';
|
|
4
|
+
export { BaseCache } from './core/BaseCache';
|
|
5
|
+
export { RedisCache } from './adapters/redis';
|
|
6
|
+
export { MemcacheCache } from './adapters/memcache';
|
|
7
|
+
export { MemoryCache } from './adapters/memory';
|
|
8
|
+
export { CacheFactory } from './core/factory';
|
|
9
|
+
export { SessionStore } from './session';
|
|
10
|
+
export { cacheSessionMiddleware, cacheHealthCheckMiddleware, cacheResponseMiddleware } from './middleware/express';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import type { ICache } from '../../core/interfaces';
|
|
3
|
+
import { SessionStore } from '../../session/SessionStore';
|
|
4
|
+
/**
|
|
5
|
+
* Express middleware for session management using cache
|
|
6
|
+
*/
|
|
7
|
+
export declare function cacheSessionMiddleware(sessionStore: SessionStore, options?: {
|
|
8
|
+
sessionIdHeader?: string;
|
|
9
|
+
sessionDataKey?: string;
|
|
10
|
+
}): (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
11
|
+
/**
|
|
12
|
+
* Express middleware for cache health check
|
|
13
|
+
*/
|
|
14
|
+
export declare function cacheHealthCheckMiddleware(cache: ICache, endpoint?: string): (req: Request, res: Response, next: NextFunction) => void;
|
|
15
|
+
/**
|
|
16
|
+
* Express middleware for cache request/response caching
|
|
17
|
+
*/
|
|
18
|
+
export declare function cacheResponseMiddleware(cache: ICache<string>, options?: {
|
|
19
|
+
ttl?: number;
|
|
20
|
+
keyPrefix?: string;
|
|
21
|
+
excludeStatusCodes?: number[];
|
|
22
|
+
}): (req: Request, res: Response, next: NextFunction) => void;
|