@hazeljs/cache 0.2.0-beta.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/README.md +523 -0
- package/dist/cache.module.d.ts +81 -0
- package/dist/cache.module.d.ts.map +1 -0
- package/dist/cache.module.js +111 -0
- package/dist/cache.service.d.ts +109 -0
- package/dist/cache.service.d.ts.map +1 -0
- package/dist/cache.service.js +263 -0
- package/dist/cache.types.d.ts +180 -0
- package/dist/cache.types.d.ts.map +1 -0
- package/dist/cache.types.js +2 -0
- package/dist/decorators/cache.decorator.d.ts +93 -0
- package/dist/decorators/cache.decorator.d.ts.map +1 -0
- package/dist/decorators/cache.decorator.js +155 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +26 -0
- package/dist/src/cache.module.d.ts +81 -0
- package/dist/src/cache.module.d.ts.map +1 -0
- package/dist/src/cache.module.js +111 -0
- package/dist/src/cache.service.d.ts +109 -0
- package/dist/src/cache.service.d.ts.map +1 -0
- package/dist/src/cache.service.js +263 -0
- package/dist/src/cache.types.d.ts +180 -0
- package/dist/src/cache.types.d.ts.map +1 -0
- package/dist/src/cache.types.js +2 -0
- package/dist/src/decorators/cache.decorator.d.ts +93 -0
- package/dist/src/decorators/cache.decorator.d.ts.map +1 -0
- package/dist/src/decorators/cache.decorator.js +155 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +26 -0
- package/dist/src/strategies/memory.strategy.d.ts +73 -0
- package/dist/src/strategies/memory.strategy.d.ts.map +1 -0
- package/dist/src/strategies/memory.strategy.js +236 -0
- package/dist/src/strategies/multi-tier.strategy.d.ts +69 -0
- package/dist/src/strategies/multi-tier.strategy.d.ts.map +1 -0
- package/dist/src/strategies/multi-tier.strategy.js +154 -0
- package/dist/src/strategies/redis.strategy.d.ts +61 -0
- package/dist/src/strategies/redis.strategy.d.ts.map +1 -0
- package/dist/src/strategies/redis.strategy.js +185 -0
- package/dist/strategies/memory.strategy.d.ts +73 -0
- package/dist/strategies/memory.strategy.d.ts.map +1 -0
- package/dist/strategies/memory.strategy.js +236 -0
- package/dist/strategies/multi-tier.strategy.d.ts +69 -0
- package/dist/strategies/multi-tier.strategy.d.ts.map +1 -0
- package/dist/strategies/multi-tier.strategy.js +154 -0
- package/dist/strategies/redis.strategy.d.ts +61 -0
- package/dist/strategies/redis.strategy.d.ts.map +1 -0
- package/dist/strategies/redis.strategy.js +185 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +47 -0
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @hazeljs/cache - Caching module for HazelJS
|
|
3
|
+
*/
|
|
4
|
+
export { CacheModule, type CacheModuleOptions } from './cache.module';
|
|
5
|
+
export { CacheService, CacheManager } from './cache.service';
|
|
6
|
+
export { Cache, CacheKey, CacheTTL, CacheTags, CacheEvict, getCacheMetadata, hasCacheMetadata, getCacheEvictMetadata, } from './decorators/cache.decorator';
|
|
7
|
+
export { type CacheOptions, type CacheStrategy, type TTLStrategy, type CacheEntry, type CacheStats, type ICacheStore, type CacheWarmingOptions, type CacheInvalidationEvent, } from './cache.types';
|
|
8
|
+
export { MemoryCacheStore } from './strategies/memory.strategy';
|
|
9
|
+
export { RedisCacheStore } from './strategies/redis.strategy';
|
|
10
|
+
export { MultiTierCacheStore } from './strategies/multi-tier.strategy';
|
|
11
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,WAAW,EAAE,KAAK,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACtE,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EACL,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,UAAU,EACV,gBAAgB,EAChB,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,aAAa,EAClB,KAAK,WAAW,EAChB,KAAK,UAAU,EACf,KAAK,UAAU,EACf,KAAK,WAAW,EAChB,KAAK,mBAAmB,EACxB,KAAK,sBAAsB,GAC5B,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @hazeljs/cache - Caching module for HazelJS
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MultiTierCacheStore = exports.RedisCacheStore = exports.MemoryCacheStore = exports.getCacheEvictMetadata = exports.hasCacheMetadata = exports.getCacheMetadata = exports.CacheEvict = exports.CacheTags = exports.CacheTTL = exports.CacheKey = exports.Cache = exports.CacheManager = exports.CacheService = exports.CacheModule = void 0;
|
|
7
|
+
var cache_module_1 = require("./cache.module");
|
|
8
|
+
Object.defineProperty(exports, "CacheModule", { enumerable: true, get: function () { return cache_module_1.CacheModule; } });
|
|
9
|
+
var cache_service_1 = require("./cache.service");
|
|
10
|
+
Object.defineProperty(exports, "CacheService", { enumerable: true, get: function () { return cache_service_1.CacheService; } });
|
|
11
|
+
Object.defineProperty(exports, "CacheManager", { enumerable: true, get: function () { return cache_service_1.CacheManager; } });
|
|
12
|
+
var cache_decorator_1 = require("./decorators/cache.decorator");
|
|
13
|
+
Object.defineProperty(exports, "Cache", { enumerable: true, get: function () { return cache_decorator_1.Cache; } });
|
|
14
|
+
Object.defineProperty(exports, "CacheKey", { enumerable: true, get: function () { return cache_decorator_1.CacheKey; } });
|
|
15
|
+
Object.defineProperty(exports, "CacheTTL", { enumerable: true, get: function () { return cache_decorator_1.CacheTTL; } });
|
|
16
|
+
Object.defineProperty(exports, "CacheTags", { enumerable: true, get: function () { return cache_decorator_1.CacheTags; } });
|
|
17
|
+
Object.defineProperty(exports, "CacheEvict", { enumerable: true, get: function () { return cache_decorator_1.CacheEvict; } });
|
|
18
|
+
Object.defineProperty(exports, "getCacheMetadata", { enumerable: true, get: function () { return cache_decorator_1.getCacheMetadata; } });
|
|
19
|
+
Object.defineProperty(exports, "hasCacheMetadata", { enumerable: true, get: function () { return cache_decorator_1.hasCacheMetadata; } });
|
|
20
|
+
Object.defineProperty(exports, "getCacheEvictMetadata", { enumerable: true, get: function () { return cache_decorator_1.getCacheEvictMetadata; } });
|
|
21
|
+
var memory_strategy_1 = require("./strategies/memory.strategy");
|
|
22
|
+
Object.defineProperty(exports, "MemoryCacheStore", { enumerable: true, get: function () { return memory_strategy_1.MemoryCacheStore; } });
|
|
23
|
+
var redis_strategy_1 = require("./strategies/redis.strategy");
|
|
24
|
+
Object.defineProperty(exports, "RedisCacheStore", { enumerable: true, get: function () { return redis_strategy_1.RedisCacheStore; } });
|
|
25
|
+
var multi_tier_strategy_1 = require("./strategies/multi-tier.strategy");
|
|
26
|
+
Object.defineProperty(exports, "MultiTierCacheStore", { enumerable: true, get: function () { return multi_tier_strategy_1.MultiTierCacheStore; } });
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { ICacheStore, CacheStats } from '../cache.types';
|
|
2
|
+
/**
|
|
3
|
+
* In-memory cache store implementation
|
|
4
|
+
*/
|
|
5
|
+
export declare class MemoryCacheStore implements ICacheStore {
|
|
6
|
+
private cleanupIntervalMs;
|
|
7
|
+
private cache;
|
|
8
|
+
private tagIndex;
|
|
9
|
+
private stats;
|
|
10
|
+
private cleanupInterval?;
|
|
11
|
+
constructor(cleanupIntervalMs?: number);
|
|
12
|
+
/**
|
|
13
|
+
* Get a value from cache
|
|
14
|
+
*/
|
|
15
|
+
get<T>(key: string): Promise<T | null>;
|
|
16
|
+
/**
|
|
17
|
+
* Set a value in cache
|
|
18
|
+
*/
|
|
19
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Set a value with tags
|
|
22
|
+
*/
|
|
23
|
+
setWithTags<T>(key: string, value: T, ttl: number, tags?: string[]): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Delete a value from cache
|
|
26
|
+
*/
|
|
27
|
+
delete(key: string): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Delete entries by tag
|
|
30
|
+
*/
|
|
31
|
+
deleteByTag(tag: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Check if key exists in cache
|
|
34
|
+
*/
|
|
35
|
+
has(key: string): Promise<boolean>;
|
|
36
|
+
/**
|
|
37
|
+
* Clear all cache entries
|
|
38
|
+
*/
|
|
39
|
+
clear(): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Get all keys matching a pattern
|
|
42
|
+
*/
|
|
43
|
+
keys(pattern?: string): Promise<string[]>;
|
|
44
|
+
/**
|
|
45
|
+
* Get cache statistics
|
|
46
|
+
*/
|
|
47
|
+
getStats(): Promise<CacheStats>;
|
|
48
|
+
/**
|
|
49
|
+
* Reset statistics
|
|
50
|
+
*/
|
|
51
|
+
resetStats(): void;
|
|
52
|
+
/**
|
|
53
|
+
* Start cleanup interval
|
|
54
|
+
*/
|
|
55
|
+
private startCleanup;
|
|
56
|
+
/**
|
|
57
|
+
* Stop cleanup interval
|
|
58
|
+
*/
|
|
59
|
+
stopCleanup(): void;
|
|
60
|
+
/**
|
|
61
|
+
* Cleanup expired entries
|
|
62
|
+
*/
|
|
63
|
+
private cleanup;
|
|
64
|
+
/**
|
|
65
|
+
* Remove key from tag index
|
|
66
|
+
*/
|
|
67
|
+
private removeFromTagIndex;
|
|
68
|
+
/**
|
|
69
|
+
* Destroy the cache store
|
|
70
|
+
*/
|
|
71
|
+
destroy(): void;
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=memory.strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.strategy.d.ts","sourceRoot":"","sources":["../../../src/strategies/memory.strategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAc,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGrE;;GAEG;AACH,qBAAa,gBAAiB,YAAW,WAAW;IAStC,OAAO,CAAC,iBAAiB;IARrC,OAAO,CAAC,KAAK,CAAsC;IACnD,OAAO,CAAC,QAAQ,CAAuC;IACvD,OAAO,CAAC,KAAK,CAGX;IACF,OAAO,CAAC,eAAe,CAAC,CAAiB;gBAErB,iBAAiB,GAAE,MAAc;IAIrD;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IA4B5C;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,GAAE,MAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IActE;;OAEG;IACG,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IA0BxF;;OAEG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IASxC;;OAEG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAc7C;;OAEG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAgBxC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5B;;OAEG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAY/C;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC;IAoBrC;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;OAEG;IACH,OAAO,CAAC,YAAY;IAMpB;;OAEG;IACH,WAAW,IAAI,IAAI;IAOnB;;OAEG;IACH,OAAO,CAAC,OAAO;IAiBf;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAgB1B;;OAEG;IACH,OAAO,IAAI,IAAI;CAKhB"}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MemoryCacheStore = void 0;
|
|
7
|
+
const core_1 = __importDefault(require("@hazeljs/core"));
|
|
8
|
+
/**
|
|
9
|
+
* In-memory cache store implementation
|
|
10
|
+
*/
|
|
11
|
+
class MemoryCacheStore {
|
|
12
|
+
constructor(cleanupIntervalMs = 60000) {
|
|
13
|
+
this.cleanupIntervalMs = cleanupIntervalMs;
|
|
14
|
+
this.cache = new Map();
|
|
15
|
+
this.tagIndex = new Map();
|
|
16
|
+
this.stats = {
|
|
17
|
+
hits: 0,
|
|
18
|
+
misses: 0,
|
|
19
|
+
};
|
|
20
|
+
this.startCleanup();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get a value from cache
|
|
24
|
+
*/
|
|
25
|
+
async get(key) {
|
|
26
|
+
const entry = this.cache.get(key);
|
|
27
|
+
if (!entry) {
|
|
28
|
+
this.stats.misses++;
|
|
29
|
+
core_1.default.debug(`Cache miss: ${key}`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
// Check if expired
|
|
33
|
+
if (Date.now() > entry.expiresAt) {
|
|
34
|
+
this.cache.delete(key);
|
|
35
|
+
this.removeFromTagIndex(key, entry.tags);
|
|
36
|
+
this.stats.misses++;
|
|
37
|
+
core_1.default.debug(`Cache expired: ${key}`);
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
// Update last accessed for sliding TTL
|
|
41
|
+
if (entry.lastAccessedAt !== undefined) {
|
|
42
|
+
entry.lastAccessedAt = Date.now();
|
|
43
|
+
}
|
|
44
|
+
this.stats.hits++;
|
|
45
|
+
core_1.default.debug(`Cache hit: ${key}`);
|
|
46
|
+
return entry.value;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Set a value in cache
|
|
50
|
+
*/
|
|
51
|
+
async set(key, value, ttl = 3600) {
|
|
52
|
+
const now = Date.now();
|
|
53
|
+
const entry = {
|
|
54
|
+
value,
|
|
55
|
+
key,
|
|
56
|
+
cachedAt: now,
|
|
57
|
+
expiresAt: now + ttl * 1000,
|
|
58
|
+
lastAccessedAt: now,
|
|
59
|
+
};
|
|
60
|
+
this.cache.set(key, entry);
|
|
61
|
+
core_1.default.debug(`Cache set: ${key} (TTL: ${ttl}s)`);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Set a value with tags
|
|
65
|
+
*/
|
|
66
|
+
async setWithTags(key, value, ttl, tags) {
|
|
67
|
+
const now = Date.now();
|
|
68
|
+
const entry = {
|
|
69
|
+
value,
|
|
70
|
+
key,
|
|
71
|
+
cachedAt: now,
|
|
72
|
+
expiresAt: now + ttl * 1000,
|
|
73
|
+
lastAccessedAt: now,
|
|
74
|
+
tags,
|
|
75
|
+
};
|
|
76
|
+
this.cache.set(key, entry);
|
|
77
|
+
// Update tag index
|
|
78
|
+
if (tags) {
|
|
79
|
+
tags.forEach((tag) => {
|
|
80
|
+
if (!this.tagIndex.has(tag)) {
|
|
81
|
+
this.tagIndex.set(tag, new Set());
|
|
82
|
+
}
|
|
83
|
+
this.tagIndex.get(tag).add(key);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
core_1.default.debug(`Cache set with tags: ${key} (TTL: ${ttl}s, Tags: ${tags?.join(', ')})`);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Delete a value from cache
|
|
90
|
+
*/
|
|
91
|
+
async delete(key) {
|
|
92
|
+
const entry = this.cache.get(key);
|
|
93
|
+
if (entry) {
|
|
94
|
+
this.removeFromTagIndex(key, entry.tags);
|
|
95
|
+
}
|
|
96
|
+
this.cache.delete(key);
|
|
97
|
+
core_1.default.debug(`Cache deleted: ${key}`);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Delete entries by tag
|
|
101
|
+
*/
|
|
102
|
+
async deleteByTag(tag) {
|
|
103
|
+
const keys = this.tagIndex.get(tag);
|
|
104
|
+
if (!keys) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
for (const key of keys) {
|
|
108
|
+
await this.delete(key);
|
|
109
|
+
}
|
|
110
|
+
this.tagIndex.delete(tag);
|
|
111
|
+
core_1.default.debug(`Cache invalidated by tag: ${tag} (${keys.size} entries)`);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Check if key exists in cache
|
|
115
|
+
*/
|
|
116
|
+
async has(key) {
|
|
117
|
+
const entry = this.cache.get(key);
|
|
118
|
+
if (!entry) {
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
// Check if expired
|
|
122
|
+
if (Date.now() > entry.expiresAt) {
|
|
123
|
+
this.cache.delete(key);
|
|
124
|
+
this.removeFromTagIndex(key, entry.tags);
|
|
125
|
+
return false;
|
|
126
|
+
}
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Clear all cache entries
|
|
131
|
+
*/
|
|
132
|
+
async clear() {
|
|
133
|
+
this.cache.clear();
|
|
134
|
+
this.tagIndex.clear();
|
|
135
|
+
core_1.default.info('Cache cleared');
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Get all keys matching a pattern
|
|
139
|
+
*/
|
|
140
|
+
async keys(pattern) {
|
|
141
|
+
const allKeys = Array.from(this.cache.keys());
|
|
142
|
+
if (!pattern) {
|
|
143
|
+
return allKeys;
|
|
144
|
+
}
|
|
145
|
+
// Simple pattern matching (supports * wildcard)
|
|
146
|
+
const regex = new RegExp('^' + pattern.replace(/\*/g, '.*') + '$');
|
|
147
|
+
return allKeys.filter((key) => regex.test(key));
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Get cache statistics
|
|
151
|
+
*/
|
|
152
|
+
async getStats() {
|
|
153
|
+
const total = this.stats.hits + this.stats.misses;
|
|
154
|
+
const hitRate = total > 0 ? (this.stats.hits / total) * 100 : 0;
|
|
155
|
+
// Calculate approximate memory usage
|
|
156
|
+
let memoryUsage = 0;
|
|
157
|
+
for (const entry of this.cache.values()) {
|
|
158
|
+
const entrySize = JSON.stringify(entry.value).length;
|
|
159
|
+
memoryUsage += entrySize;
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
hits: this.stats.hits,
|
|
163
|
+
misses: this.stats.misses,
|
|
164
|
+
hitRate: Math.round(hitRate * 100) / 100,
|
|
165
|
+
size: this.cache.size,
|
|
166
|
+
memoryUsage,
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Reset statistics
|
|
171
|
+
*/
|
|
172
|
+
resetStats() {
|
|
173
|
+
this.stats.hits = 0;
|
|
174
|
+
this.stats.misses = 0;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Start cleanup interval
|
|
178
|
+
*/
|
|
179
|
+
startCleanup() {
|
|
180
|
+
this.cleanupInterval = setInterval(() => {
|
|
181
|
+
this.cleanup();
|
|
182
|
+
}, this.cleanupIntervalMs);
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Stop cleanup interval
|
|
186
|
+
*/
|
|
187
|
+
stopCleanup() {
|
|
188
|
+
if (this.cleanupInterval) {
|
|
189
|
+
clearInterval(this.cleanupInterval);
|
|
190
|
+
this.cleanupInterval = undefined;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Cleanup expired entries
|
|
195
|
+
*/
|
|
196
|
+
cleanup() {
|
|
197
|
+
const now = Date.now();
|
|
198
|
+
let expiredCount = 0;
|
|
199
|
+
for (const [key, entry] of this.cache.entries()) {
|
|
200
|
+
if (now > entry.expiresAt) {
|
|
201
|
+
this.cache.delete(key);
|
|
202
|
+
this.removeFromTagIndex(key, entry.tags);
|
|
203
|
+
expiredCount++;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (expiredCount > 0) {
|
|
207
|
+
core_1.default.debug(`Cleaned up ${expiredCount} expired cache entries`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Remove key from tag index
|
|
212
|
+
*/
|
|
213
|
+
removeFromTagIndex(key, tags) {
|
|
214
|
+
if (!tags) {
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
tags.forEach((tag) => {
|
|
218
|
+
const keys = this.tagIndex.get(tag);
|
|
219
|
+
if (keys) {
|
|
220
|
+
keys.delete(key);
|
|
221
|
+
if (keys.size === 0) {
|
|
222
|
+
this.tagIndex.delete(tag);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Destroy the cache store
|
|
229
|
+
*/
|
|
230
|
+
destroy() {
|
|
231
|
+
this.stopCleanup();
|
|
232
|
+
this.cache.clear();
|
|
233
|
+
this.tagIndex.clear();
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
exports.MemoryCacheStore = MemoryCacheStore;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { ICacheStore, CacheStats } from '../cache.types';
|
|
2
|
+
/**
|
|
3
|
+
* Multi-tier cache store implementation
|
|
4
|
+
* Uses memory cache as L1 and Redis as L2
|
|
5
|
+
*/
|
|
6
|
+
export declare class MultiTierCacheStore implements ICacheStore {
|
|
7
|
+
private l1Cache;
|
|
8
|
+
private l2Cache;
|
|
9
|
+
constructor(options?: {
|
|
10
|
+
redis?: {
|
|
11
|
+
host?: string;
|
|
12
|
+
port?: number;
|
|
13
|
+
password?: string;
|
|
14
|
+
};
|
|
15
|
+
});
|
|
16
|
+
/**
|
|
17
|
+
* Get a value from cache (L1 first, then L2)
|
|
18
|
+
*/
|
|
19
|
+
get<T>(key: string): Promise<T | null>;
|
|
20
|
+
/**
|
|
21
|
+
* Set a value in cache (both L1 and L2)
|
|
22
|
+
*/
|
|
23
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Set a value with tags
|
|
26
|
+
*/
|
|
27
|
+
setWithTags<T>(key: string, value: T, ttl: number, tags?: string[]): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Delete a value from cache (both L1 and L2)
|
|
30
|
+
*/
|
|
31
|
+
delete(key: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Delete entries by tag
|
|
34
|
+
*/
|
|
35
|
+
deleteByTag(tag: string): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Check if key exists in cache
|
|
38
|
+
*/
|
|
39
|
+
has(key: string): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Clear all cache entries
|
|
42
|
+
*/
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Get all keys matching a pattern
|
|
46
|
+
*/
|
|
47
|
+
keys(pattern?: string): Promise<string[]>;
|
|
48
|
+
/**
|
|
49
|
+
* Get cache statistics
|
|
50
|
+
*/
|
|
51
|
+
getStats(): Promise<CacheStats>;
|
|
52
|
+
/**
|
|
53
|
+
* Get L1 cache statistics
|
|
54
|
+
*/
|
|
55
|
+
getL1Stats(): Promise<CacheStats>;
|
|
56
|
+
/**
|
|
57
|
+
* Get L2 cache statistics
|
|
58
|
+
*/
|
|
59
|
+
getL2Stats(): Promise<CacheStats>;
|
|
60
|
+
/**
|
|
61
|
+
* Reset statistics
|
|
62
|
+
*/
|
|
63
|
+
resetStats(): void;
|
|
64
|
+
/**
|
|
65
|
+
* Destroy the cache store
|
|
66
|
+
*/
|
|
67
|
+
destroy(): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=multi-tier.strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-tier.strategy.d.ts","sourceRoot":"","sources":["../../../src/strategies/multi-tier.strategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAKzD;;;GAGG;AACH,qBAAa,mBAAoB,YAAW,WAAW;IACrD,OAAO,CAAC,OAAO,CAAmB;IAClC,OAAO,CAAC,OAAO,CAAkB;gBAErB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE;YAAE,IAAI,CAAC,EAAE,MAAM,CAAC;YAAC,IAAI,CAAC,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE;IAMrF;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IAqB5C;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,GAAE,MAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAUtE;;OAEG;IACG,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAWxF;;OAEG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAMxC;;OAEG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAM7C;;OAEG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IASxC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAM5B;;OAEG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAW/C;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC;IAqBrC;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC;IAIvC;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,UAAU,CAAC;IAIvC;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;CAK/B"}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.MultiTierCacheStore = void 0;
|
|
7
|
+
const memory_strategy_1 = require("./memory.strategy");
|
|
8
|
+
const redis_strategy_1 = require("./redis.strategy");
|
|
9
|
+
const core_1 = __importDefault(require("@hazeljs/core"));
|
|
10
|
+
/**
|
|
11
|
+
* Multi-tier cache store implementation
|
|
12
|
+
* Uses memory cache as L1 and Redis as L2
|
|
13
|
+
*/
|
|
14
|
+
class MultiTierCacheStore {
|
|
15
|
+
constructor(options) {
|
|
16
|
+
this.l1Cache = new memory_strategy_1.MemoryCacheStore();
|
|
17
|
+
this.l2Cache = new redis_strategy_1.RedisCacheStore(options?.redis);
|
|
18
|
+
core_1.default.info('Multi-tier cache store initialized (L1: Memory, L2: Redis)');
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Get a value from cache (L1 first, then L2)
|
|
22
|
+
*/
|
|
23
|
+
async get(key) {
|
|
24
|
+
// Try L1 cache first
|
|
25
|
+
let value = await this.l1Cache.get(key);
|
|
26
|
+
if (value !== null) {
|
|
27
|
+
core_1.default.debug(`Multi-tier cache hit (L1): ${key}`);
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
// Try L2 cache
|
|
31
|
+
value = await this.l2Cache.get(key);
|
|
32
|
+
if (value !== null) {
|
|
33
|
+
core_1.default.debug(`Multi-tier cache hit (L2): ${key}`);
|
|
34
|
+
// Promote to L1
|
|
35
|
+
await this.l1Cache.set(key, value, 300); // 5 minutes in L1
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
core_1.default.debug(`Multi-tier cache miss: ${key}`);
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Set a value in cache (both L1 and L2)
|
|
43
|
+
*/
|
|
44
|
+
async set(key, value, ttl = 3600) {
|
|
45
|
+
// Set in both caches
|
|
46
|
+
await Promise.all([
|
|
47
|
+
this.l1Cache.set(key, value, Math.min(ttl, 300)), // Max 5 minutes in L1
|
|
48
|
+
this.l2Cache.set(key, value, ttl),
|
|
49
|
+
]);
|
|
50
|
+
core_1.default.debug(`Multi-tier cache set: ${key} (TTL: ${ttl}s)`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Set a value with tags
|
|
54
|
+
*/
|
|
55
|
+
async setWithTags(key, value, ttl, tags) {
|
|
56
|
+
await Promise.all([
|
|
57
|
+
this.l1Cache.setWithTags(key, value, Math.min(ttl, 300), tags),
|
|
58
|
+
this.l2Cache.setWithTags(key, value, ttl, tags),
|
|
59
|
+
]);
|
|
60
|
+
core_1.default.debug(`Multi-tier cache set with tags: ${key} (TTL: ${ttl}s, Tags: ${tags?.join(', ')})`);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Delete a value from cache (both L1 and L2)
|
|
64
|
+
*/
|
|
65
|
+
async delete(key) {
|
|
66
|
+
await Promise.all([this.l1Cache.delete(key), this.l2Cache.delete(key)]);
|
|
67
|
+
core_1.default.debug(`Multi-tier cache deleted: ${key}`);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Delete entries by tag
|
|
71
|
+
*/
|
|
72
|
+
async deleteByTag(tag) {
|
|
73
|
+
await Promise.all([this.l1Cache.deleteByTag(tag), this.l2Cache.deleteByTag(tag)]);
|
|
74
|
+
core_1.default.debug(`Multi-tier cache invalidated by tag: ${tag}`);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Check if key exists in cache
|
|
78
|
+
*/
|
|
79
|
+
async has(key) {
|
|
80
|
+
const inL1 = await this.l1Cache.has(key);
|
|
81
|
+
if (inL1) {
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
return await this.l2Cache.has(key);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Clear all cache entries
|
|
88
|
+
*/
|
|
89
|
+
async clear() {
|
|
90
|
+
await Promise.all([this.l1Cache.clear(), this.l2Cache.clear()]);
|
|
91
|
+
core_1.default.info('Multi-tier cache cleared');
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Get all keys matching a pattern
|
|
95
|
+
*/
|
|
96
|
+
async keys(pattern) {
|
|
97
|
+
// Get keys from both caches and merge
|
|
98
|
+
const [l1Keys, l2Keys] = await Promise.all([
|
|
99
|
+
this.l1Cache.keys(pattern),
|
|
100
|
+
this.l2Cache.keys(pattern),
|
|
101
|
+
]);
|
|
102
|
+
// Merge and deduplicate
|
|
103
|
+
return Array.from(new Set([...l1Keys, ...l2Keys]));
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Get cache statistics
|
|
107
|
+
*/
|
|
108
|
+
async getStats() {
|
|
109
|
+
const [l1Stats, l2Stats] = await Promise.all([
|
|
110
|
+
this.l1Cache.getStats(),
|
|
111
|
+
this.l2Cache.getStats(),
|
|
112
|
+
]);
|
|
113
|
+
// Combine stats
|
|
114
|
+
const totalHits = l1Stats.hits + l2Stats.hits;
|
|
115
|
+
const totalMisses = l1Stats.misses + l2Stats.misses;
|
|
116
|
+
const total = totalHits + totalMisses;
|
|
117
|
+
const hitRate = total > 0 ? (totalHits / total) * 100 : 0;
|
|
118
|
+
return {
|
|
119
|
+
hits: totalHits,
|
|
120
|
+
misses: totalMisses,
|
|
121
|
+
hitRate: Math.round(hitRate * 100) / 100,
|
|
122
|
+
size: l1Stats.size + l2Stats.size,
|
|
123
|
+
memoryUsage: l1Stats.memoryUsage,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Get L1 cache statistics
|
|
128
|
+
*/
|
|
129
|
+
async getL1Stats() {
|
|
130
|
+
return await this.l1Cache.getStats();
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Get L2 cache statistics
|
|
134
|
+
*/
|
|
135
|
+
async getL2Stats() {
|
|
136
|
+
return await this.l2Cache.getStats();
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Reset statistics
|
|
140
|
+
*/
|
|
141
|
+
resetStats() {
|
|
142
|
+
this.l1Cache.resetStats();
|
|
143
|
+
this.l2Cache.resetStats();
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Destroy the cache store
|
|
147
|
+
*/
|
|
148
|
+
async destroy() {
|
|
149
|
+
this.l1Cache.destroy();
|
|
150
|
+
await this.l2Cache.disconnect();
|
|
151
|
+
core_1.default.info('Multi-tier cache destroyed');
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
exports.MultiTierCacheStore = MultiTierCacheStore;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { ICacheStore, CacheStats } from '../cache.types';
|
|
2
|
+
/**
|
|
3
|
+
* Redis cache store implementation
|
|
4
|
+
* Note: This is a mock implementation. In production, you would use a real Redis client like ioredis
|
|
5
|
+
*/
|
|
6
|
+
export declare class RedisCacheStore implements ICacheStore {
|
|
7
|
+
private options?;
|
|
8
|
+
private mockStore;
|
|
9
|
+
private tagIndex;
|
|
10
|
+
private stats;
|
|
11
|
+
constructor(options?: {
|
|
12
|
+
host?: string;
|
|
13
|
+
port?: number;
|
|
14
|
+
password?: string;
|
|
15
|
+
} | undefined);
|
|
16
|
+
/**
|
|
17
|
+
* Get a value from cache
|
|
18
|
+
*/
|
|
19
|
+
get<T>(key: string): Promise<T | null>;
|
|
20
|
+
/**
|
|
21
|
+
* Set a value in cache
|
|
22
|
+
*/
|
|
23
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Set a value with tags
|
|
26
|
+
*/
|
|
27
|
+
setWithTags<T>(key: string, value: T, ttl: number, tags?: string[]): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Delete a value from cache
|
|
30
|
+
*/
|
|
31
|
+
delete(key: string): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* Delete entries by tag
|
|
34
|
+
*/
|
|
35
|
+
deleteByTag(tag: string): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Check if key exists in cache
|
|
38
|
+
*/
|
|
39
|
+
has(key: string): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Clear all cache entries
|
|
42
|
+
*/
|
|
43
|
+
clear(): Promise<void>;
|
|
44
|
+
/**
|
|
45
|
+
* Get all keys matching a pattern
|
|
46
|
+
*/
|
|
47
|
+
keys(pattern?: string): Promise<string[]>;
|
|
48
|
+
/**
|
|
49
|
+
* Get cache statistics
|
|
50
|
+
*/
|
|
51
|
+
getStats(): Promise<CacheStats>;
|
|
52
|
+
/**
|
|
53
|
+
* Reset statistics
|
|
54
|
+
*/
|
|
55
|
+
resetStats(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Disconnect from Redis
|
|
58
|
+
*/
|
|
59
|
+
disconnect(): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=redis.strategy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"redis.strategy.d.ts","sourceRoot":"","sources":["../../../src/strategies/redis.strategy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAGzD;;;GAGG;AACH,qBAAa,eAAgB,YAAW,WAAW;IAQrC,OAAO,CAAC,OAAO,CAAC;IAP5B,OAAO,CAAC,SAAS,CAAgE;IACjF,OAAO,CAAC,QAAQ,CAAuC;IACvD,OAAO,CAAC,KAAK,CAGX;gBAEkB,OAAO,CAAC,EAAE;QAAE,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAC;QAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;KAAE,YAAA;IAMjF;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC;IA4B5C;;OAEG;IACG,GAAG,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,GAAE,MAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAWtE;;OAEG;IACG,WAAW,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBxF;;OAEG;IACG,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAQxC;;OAEG;IACG,WAAW,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAqB7C;;OAEG;IACG,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;IAkBxC;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAS5B;;OAEG;IACG,IAAI,CAAC,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAe/C;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,UAAU,CAAC;IAgBrC;;OAEG;IACH,UAAU,IAAI,IAAI;IAKlB;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;CAQlC"}
|