@hazeljs/cache 0.2.0-alpha.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/LICENSE +192 -0
- package/README.md +524 -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.test.d.ts +2 -0
- package/dist/cache.test.d.ts.map +1 -0
- package/dist/cache.test.js +374 -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/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/package.json +54 -0
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { CacheService, CacheManager } from './cache.service';
|
|
2
|
+
import { CacheStrategy } from './cache.types';
|
|
3
|
+
/**
|
|
4
|
+
* Cache module options
|
|
5
|
+
*/
|
|
6
|
+
export interface CacheModuleOptions {
|
|
7
|
+
/**
|
|
8
|
+
* Cache strategy to use
|
|
9
|
+
* @default 'memory'
|
|
10
|
+
*/
|
|
11
|
+
strategy?: CacheStrategy;
|
|
12
|
+
/**
|
|
13
|
+
* Whether this is a global module
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
isGlobal?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Redis options (if using redis or multi-tier strategy)
|
|
19
|
+
*/
|
|
20
|
+
redis?: {
|
|
21
|
+
host?: string;
|
|
22
|
+
port?: number;
|
|
23
|
+
password?: string;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Memory cache cleanup interval in milliseconds
|
|
27
|
+
* @default 60000
|
|
28
|
+
*/
|
|
29
|
+
cleanupInterval?: number;
|
|
30
|
+
/**
|
|
31
|
+
* Multiple cache configurations
|
|
32
|
+
*/
|
|
33
|
+
stores?: Array<{
|
|
34
|
+
name: string;
|
|
35
|
+
strategy: CacheStrategy;
|
|
36
|
+
isDefault?: boolean;
|
|
37
|
+
options?: {
|
|
38
|
+
redis?: {
|
|
39
|
+
host?: string;
|
|
40
|
+
port?: number;
|
|
41
|
+
password?: string;
|
|
42
|
+
};
|
|
43
|
+
cleanupInterval?: number;
|
|
44
|
+
};
|
|
45
|
+
}>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Cache module for HazelJS
|
|
49
|
+
*/
|
|
50
|
+
export declare class CacheModule {
|
|
51
|
+
/**
|
|
52
|
+
* Configure cache module
|
|
53
|
+
*/
|
|
54
|
+
static forRoot(options?: CacheModuleOptions): {
|
|
55
|
+
module: typeof CacheModule;
|
|
56
|
+
providers: Array<{
|
|
57
|
+
provide: typeof CacheService | typeof CacheManager;
|
|
58
|
+
useFactory?: () => CacheService;
|
|
59
|
+
useValue?: CacheManager;
|
|
60
|
+
}>;
|
|
61
|
+
exports: Array<typeof CacheService | typeof CacheManager>;
|
|
62
|
+
global: boolean;
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Configure cache module asynchronously
|
|
66
|
+
*/
|
|
67
|
+
static forRootAsync(options: {
|
|
68
|
+
useFactory: (...args: unknown[]) => Promise<CacheModuleOptions> | CacheModuleOptions;
|
|
69
|
+
inject?: unknown[];
|
|
70
|
+
}): {
|
|
71
|
+
module: typeof CacheModule;
|
|
72
|
+
providers: Array<{
|
|
73
|
+
provide: string | typeof CacheManager | typeof CacheService;
|
|
74
|
+
useFactory: unknown;
|
|
75
|
+
inject?: unknown[];
|
|
76
|
+
}>;
|
|
77
|
+
exports: Array<typeof CacheService | typeof CacheManager>;
|
|
78
|
+
global: boolean;
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=cache.module.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.module.d.ts","sourceRoot":"","sources":["../src/cache.module.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG9C;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;OAGG;IACH,QAAQ,CAAC,EAAE,aAAa,CAAC;IAEzB;;;OAGG;IACH,QAAQ,CAAC,EAAE,OAAO,CAAC;IAEnB;;OAEG;IACH,KAAK,CAAC,EAAE;QACN,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,IAAI,CAAC,EAAE,MAAM,CAAC;QACd,QAAQ,CAAC,EAAE,MAAM,CAAC;KACnB,CAAC;IAEF;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IAEzB;;OAEG;IACH,MAAM,CAAC,EAAE,KAAK,CAAC;QACb,IAAI,EAAE,MAAM,CAAC;QACb,QAAQ,EAAE,aAAa,CAAC;QACxB,SAAS,CAAC,EAAE,OAAO,CAAC;QACpB,OAAO,CAAC,EAAE;YACR,KAAK,CAAC,EAAE;gBACN,IAAI,CAAC,EAAE,MAAM,CAAC;gBACd,IAAI,CAAC,EAAE,MAAM,CAAC;gBACd,QAAQ,CAAC,EAAE,MAAM,CAAC;aACnB,CAAC;YACF,eAAe,CAAC,EAAE,MAAM,CAAC;SAC1B,CAAC;KACH,CAAC,CAAC;CACJ;AAED;;GAEG;AACH,qBAIa,WAAW;IACtB;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,GAAE,kBAAuB,GAAG;QAChD,MAAM,EAAE,OAAO,WAAW,CAAC;QAC3B,SAAS,EAAE,KAAK,CAAC;YACf,OAAO,EAAE,OAAO,YAAY,GAAG,OAAO,YAAY,CAAC;YACnD,UAAU,CAAC,EAAE,MAAM,YAAY,CAAC;YAChC,QAAQ,CAAC,EAAE,YAAY,CAAC;SACzB,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,CAAC,OAAO,YAAY,GAAG,OAAO,YAAY,CAAC,CAAC;QAC1D,MAAM,EAAE,OAAO,CAAC;KACjB;IAwCD;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,OAAO,EAAE;QAC3B,UAAU,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,OAAO,CAAC,kBAAkB,CAAC,GAAG,kBAAkB,CAAC;QACrF,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;KACpB,GAAG;QACF,MAAM,EAAE,OAAO,WAAW,CAAC;QAC3B,SAAS,EAAE,KAAK,CAAC;YACf,OAAO,EAAE,MAAM,GAAG,OAAO,YAAY,GAAG,OAAO,YAAY,CAAC;YAC5D,UAAU,EAAE,OAAO,CAAC;YACpB,MAAM,CAAC,EAAE,OAAO,EAAE,CAAC;SACpB,CAAC,CAAC;QACH,OAAO,EAAE,KAAK,CAAC,OAAO,YAAY,GAAG,OAAO,YAAY,CAAC,CAAC;QAC1D,MAAM,EAAE,OAAO,CAAC;KACjB;CA2CF"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
var CacheModule_1;
|
|
12
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
+
exports.CacheModule = void 0;
|
|
14
|
+
const core_1 = require("@hazeljs/core");
|
|
15
|
+
const cache_service_1 = require("./cache.service");
|
|
16
|
+
const core_2 = __importDefault(require("@hazeljs/core"));
|
|
17
|
+
/**
|
|
18
|
+
* Cache module for HazelJS
|
|
19
|
+
*/
|
|
20
|
+
let CacheModule = CacheModule_1 = class CacheModule {
|
|
21
|
+
/**
|
|
22
|
+
* Configure cache module
|
|
23
|
+
*/
|
|
24
|
+
static forRoot(options = {}) {
|
|
25
|
+
const { strategy = 'memory', isGlobal = true, redis, cleanupInterval, stores } = options;
|
|
26
|
+
core_2.default.info('Configuring cache module...');
|
|
27
|
+
// Create cache manager
|
|
28
|
+
const cacheManager = new cache_service_1.CacheManager();
|
|
29
|
+
// If multiple stores are configured
|
|
30
|
+
if (stores && stores.length > 0) {
|
|
31
|
+
for (const storeConfig of stores) {
|
|
32
|
+
const cache = new cache_service_1.CacheService(storeConfig.strategy, {
|
|
33
|
+
redis: storeConfig.options?.redis || redis,
|
|
34
|
+
cleanupInterval: storeConfig.options?.cleanupInterval || cleanupInterval,
|
|
35
|
+
});
|
|
36
|
+
cacheManager.register(storeConfig.name, cache, storeConfig.isDefault);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
// Single cache configuration
|
|
41
|
+
const cache = new cache_service_1.CacheService(strategy, { redis, cleanupInterval });
|
|
42
|
+
cacheManager.register('default', cache, true);
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
module: CacheModule_1,
|
|
46
|
+
providers: [
|
|
47
|
+
{
|
|
48
|
+
provide: cache_service_1.CacheService,
|
|
49
|
+
useFactory: () => cacheManager.get(),
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
provide: cache_service_1.CacheManager,
|
|
53
|
+
useValue: cacheManager,
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
exports: [cache_service_1.CacheService, cache_service_1.CacheManager],
|
|
57
|
+
global: isGlobal,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Configure cache module asynchronously
|
|
62
|
+
*/
|
|
63
|
+
static forRootAsync(options) {
|
|
64
|
+
return {
|
|
65
|
+
module: CacheModule_1,
|
|
66
|
+
providers: [
|
|
67
|
+
{
|
|
68
|
+
provide: 'CACHE_OPTIONS',
|
|
69
|
+
useFactory: options.useFactory,
|
|
70
|
+
inject: options.inject || [],
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
provide: cache_service_1.CacheManager,
|
|
74
|
+
useFactory: async (cacheOptions) => {
|
|
75
|
+
const { strategy = 'memory', redis, cleanupInterval, stores } = cacheOptions;
|
|
76
|
+
const cacheManager = new cache_service_1.CacheManager();
|
|
77
|
+
if (stores && stores.length > 0) {
|
|
78
|
+
for (const storeConfig of stores) {
|
|
79
|
+
const cache = new cache_service_1.CacheService(storeConfig.strategy, {
|
|
80
|
+
redis: storeConfig.options?.redis || redis,
|
|
81
|
+
cleanupInterval: storeConfig.options?.cleanupInterval || cleanupInterval,
|
|
82
|
+
});
|
|
83
|
+
cacheManager.register(storeConfig.name, cache, storeConfig.isDefault);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const cache = new cache_service_1.CacheService(strategy, { redis, cleanupInterval });
|
|
88
|
+
cacheManager.register('default', cache, true);
|
|
89
|
+
}
|
|
90
|
+
return cacheManager;
|
|
91
|
+
},
|
|
92
|
+
inject: ['CACHE_OPTIONS'],
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
provide: cache_service_1.CacheService,
|
|
96
|
+
useFactory: (cacheManager) => cacheManager.get(),
|
|
97
|
+
inject: [cache_service_1.CacheManager],
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
exports: [cache_service_1.CacheService, cache_service_1.CacheManager],
|
|
101
|
+
global: true,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
exports.CacheModule = CacheModule;
|
|
106
|
+
exports.CacheModule = CacheModule = CacheModule_1 = __decorate([
|
|
107
|
+
(0, core_1.HazelModule)({
|
|
108
|
+
providers: [],
|
|
109
|
+
exports: [],
|
|
110
|
+
})
|
|
111
|
+
], CacheModule);
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ICacheStore, CacheStats, CacheWarmingOptions, CacheStrategy } from './cache.types';
|
|
2
|
+
/**
|
|
3
|
+
* Cache service for managing cache operations
|
|
4
|
+
*/
|
|
5
|
+
export declare class CacheService {
|
|
6
|
+
private store;
|
|
7
|
+
private strategy;
|
|
8
|
+
constructor(strategy?: CacheStrategy, options?: unknown);
|
|
9
|
+
/**
|
|
10
|
+
* Create cache store based on strategy
|
|
11
|
+
*/
|
|
12
|
+
private createStore;
|
|
13
|
+
/**
|
|
14
|
+
* Get a value from cache
|
|
15
|
+
*/
|
|
16
|
+
get<T>(key: string): Promise<T | null>;
|
|
17
|
+
/**
|
|
18
|
+
* Set a value in cache
|
|
19
|
+
*/
|
|
20
|
+
set<T>(key: string, value: T, ttl?: number): Promise<void>;
|
|
21
|
+
/**
|
|
22
|
+
* Set a value with tags
|
|
23
|
+
*/
|
|
24
|
+
setWithTags<T>(key: string, value: T, ttl: number, tags?: string[]): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Delete a value from cache
|
|
27
|
+
*/
|
|
28
|
+
delete(key: string): Promise<void>;
|
|
29
|
+
/**
|
|
30
|
+
* Delete entries by tag
|
|
31
|
+
*/
|
|
32
|
+
deleteByTag(tag: string): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Invalidate cache by tags
|
|
35
|
+
*/
|
|
36
|
+
invalidateTags(tags: string[]): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* Check if key exists in cache
|
|
39
|
+
*/
|
|
40
|
+
has(key: string): Promise<boolean>;
|
|
41
|
+
/**
|
|
42
|
+
* Clear all cache entries
|
|
43
|
+
*/
|
|
44
|
+
clear(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Get all keys matching a pattern
|
|
47
|
+
*/
|
|
48
|
+
keys(pattern?: string): Promise<string[]>;
|
|
49
|
+
/**
|
|
50
|
+
* Get cache statistics
|
|
51
|
+
*/
|
|
52
|
+
getStats(): Promise<CacheStats>;
|
|
53
|
+
/**
|
|
54
|
+
* Warm up cache with predefined data
|
|
55
|
+
*/
|
|
56
|
+
warmUp(options: CacheWarmingOptions): Promise<void>;
|
|
57
|
+
/**
|
|
58
|
+
* Get or set a value (cache-aside pattern)
|
|
59
|
+
*/
|
|
60
|
+
getOrSet<T>(key: string, fetcher: () => Promise<T>, ttl?: number, tags?: string[]): Promise<T>;
|
|
61
|
+
/**
|
|
62
|
+
* Wrap a function with caching
|
|
63
|
+
*/
|
|
64
|
+
wrap<T>(key: string, fn: () => Promise<T>, ttl?: number): Promise<T>;
|
|
65
|
+
/**
|
|
66
|
+
* Get the underlying cache store
|
|
67
|
+
*/
|
|
68
|
+
getStore(): ICacheStore;
|
|
69
|
+
/**
|
|
70
|
+
* Get cache strategy
|
|
71
|
+
*/
|
|
72
|
+
getStrategy(): CacheStrategy;
|
|
73
|
+
/**
|
|
74
|
+
* Reset cache statistics
|
|
75
|
+
*/
|
|
76
|
+
resetStats(): void;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Cache manager for managing multiple cache instances
|
|
80
|
+
*/
|
|
81
|
+
export declare class CacheManager {
|
|
82
|
+
private caches;
|
|
83
|
+
private defaultCache?;
|
|
84
|
+
/**
|
|
85
|
+
* Register a cache instance
|
|
86
|
+
*/
|
|
87
|
+
register(name: string, cache: CacheService, isDefault?: boolean): void;
|
|
88
|
+
/**
|
|
89
|
+
* Get a cache instance by name
|
|
90
|
+
*/
|
|
91
|
+
get(name?: string): CacheService;
|
|
92
|
+
/**
|
|
93
|
+
* Get all cache instances
|
|
94
|
+
*/
|
|
95
|
+
getAll(): Map<string, CacheService>;
|
|
96
|
+
/**
|
|
97
|
+
* Clear all caches
|
|
98
|
+
*/
|
|
99
|
+
clearAll(): Promise<void>;
|
|
100
|
+
/**
|
|
101
|
+
* Get statistics for all caches
|
|
102
|
+
*/
|
|
103
|
+
getAllStats(): Promise<Map<string, CacheStats>>;
|
|
104
|
+
/**
|
|
105
|
+
* Invalidate tags across all caches
|
|
106
|
+
*/
|
|
107
|
+
invalidateTagsGlobal(tags: string[]): Promise<void>;
|
|
108
|
+
}
|
|
109
|
+
//# sourceMappingURL=cache.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.service.d.ts","sourceRoot":"","sources":["../src/cache.service.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
9
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.CacheManager = exports.CacheService = void 0;
|
|
16
|
+
const core_1 = require("@hazeljs/core");
|
|
17
|
+
const memory_strategy_1 = require("./strategies/memory.strategy");
|
|
18
|
+
const redis_strategy_1 = require("./strategies/redis.strategy");
|
|
19
|
+
const multi_tier_strategy_1 = require("./strategies/multi-tier.strategy");
|
|
20
|
+
const core_2 = __importDefault(require("@hazeljs/core"));
|
|
21
|
+
/**
|
|
22
|
+
* Cache service for managing cache operations
|
|
23
|
+
*/
|
|
24
|
+
let CacheService = class CacheService {
|
|
25
|
+
constructor(strategy = 'memory', options) {
|
|
26
|
+
this.strategy = strategy;
|
|
27
|
+
this.store = this.createStore(strategy, options);
|
|
28
|
+
core_2.default.info(`Cache service initialized with strategy: ${strategy}`);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create cache store based on strategy
|
|
32
|
+
*/
|
|
33
|
+
createStore(strategy, options) {
|
|
34
|
+
const opts = options;
|
|
35
|
+
switch (strategy) {
|
|
36
|
+
case 'redis':
|
|
37
|
+
return new redis_strategy_1.RedisCacheStore(opts?.redis);
|
|
38
|
+
case 'multi-tier':
|
|
39
|
+
return new multi_tier_strategy_1.MultiTierCacheStore(opts);
|
|
40
|
+
case 'memory':
|
|
41
|
+
default:
|
|
42
|
+
return new memory_strategy_1.MemoryCacheStore(opts?.cleanupInterval);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Get a value from cache
|
|
47
|
+
*/
|
|
48
|
+
async get(key) {
|
|
49
|
+
return await this.store.get(key);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Set a value in cache
|
|
53
|
+
*/
|
|
54
|
+
async set(key, value, ttl) {
|
|
55
|
+
await this.store.set(key, value, ttl);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Set a value with tags
|
|
59
|
+
*/
|
|
60
|
+
async setWithTags(key, value, ttl, tags) {
|
|
61
|
+
if ('setWithTags' in this.store &&
|
|
62
|
+
typeof this.store.setWithTags === 'function') {
|
|
63
|
+
await this.store.setWithTags(key, value, ttl, tags);
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
await this.store.set(key, value, ttl);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Delete a value from cache
|
|
71
|
+
*/
|
|
72
|
+
async delete(key) {
|
|
73
|
+
await this.store.delete(key);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Delete entries by tag
|
|
77
|
+
*/
|
|
78
|
+
async deleteByTag(tag) {
|
|
79
|
+
if ('deleteByTag' in this.store &&
|
|
80
|
+
typeof this.store.deleteByTag === 'function') {
|
|
81
|
+
await this.store.deleteByTag(tag);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
core_2.default.warn(`deleteByTag not supported for ${this.strategy} strategy`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Invalidate cache by tags
|
|
89
|
+
*/
|
|
90
|
+
async invalidateTags(tags) {
|
|
91
|
+
for (const tag of tags) {
|
|
92
|
+
await this.deleteByTag(tag);
|
|
93
|
+
}
|
|
94
|
+
core_2.default.info(`Invalidated cache for tags: ${tags.join(', ')}`);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Check if key exists in cache
|
|
98
|
+
*/
|
|
99
|
+
async has(key) {
|
|
100
|
+
return await this.store.has(key);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Clear all cache entries
|
|
104
|
+
*/
|
|
105
|
+
async clear() {
|
|
106
|
+
await this.store.clear();
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Get all keys matching a pattern
|
|
110
|
+
*/
|
|
111
|
+
async keys(pattern) {
|
|
112
|
+
return await this.store.keys(pattern);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Get cache statistics
|
|
116
|
+
*/
|
|
117
|
+
async getStats() {
|
|
118
|
+
return await this.store.getStats();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Warm up cache with predefined data
|
|
122
|
+
*/
|
|
123
|
+
async warmUp(options) {
|
|
124
|
+
const { keys, fetcher, ttl = 3600, parallel = true } = options;
|
|
125
|
+
core_2.default.info(`Warming up cache for ${keys.length} keys...`);
|
|
126
|
+
const warmUpKey = async (key) => {
|
|
127
|
+
try {
|
|
128
|
+
const data = await fetcher(key);
|
|
129
|
+
await this.set(key, data, ttl);
|
|
130
|
+
core_2.default.debug(`Warmed up cache key: ${key}`);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
core_2.default.error(`Failed to warm up cache key ${key}:`, error);
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
if (parallel) {
|
|
137
|
+
await Promise.all(keys.map(warmUpKey));
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
for (const key of keys) {
|
|
141
|
+
await warmUpKey(key);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
core_2.default.info(`Cache warming completed for ${keys.length} keys`);
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Get or set a value (cache-aside pattern)
|
|
148
|
+
*/
|
|
149
|
+
async getOrSet(key, fetcher, ttl, tags) {
|
|
150
|
+
// Try to get from cache
|
|
151
|
+
const cached = await this.get(key);
|
|
152
|
+
if (cached !== null) {
|
|
153
|
+
return cached;
|
|
154
|
+
}
|
|
155
|
+
// Fetch data
|
|
156
|
+
const data = await fetcher();
|
|
157
|
+
// Store in cache
|
|
158
|
+
if (tags) {
|
|
159
|
+
await this.setWithTags(key, data, ttl || 3600, tags);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
await this.set(key, data, ttl);
|
|
163
|
+
}
|
|
164
|
+
return data;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Wrap a function with caching
|
|
168
|
+
*/
|
|
169
|
+
wrap(key, fn, ttl) {
|
|
170
|
+
return this.getOrSet(key, fn, ttl);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Get the underlying cache store
|
|
174
|
+
*/
|
|
175
|
+
getStore() {
|
|
176
|
+
return this.store;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get cache strategy
|
|
180
|
+
*/
|
|
181
|
+
getStrategy() {
|
|
182
|
+
return this.strategy;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Reset cache statistics
|
|
186
|
+
*/
|
|
187
|
+
resetStats() {
|
|
188
|
+
if ('resetStats' in this.store &&
|
|
189
|
+
typeof this.store.resetStats === 'function') {
|
|
190
|
+
this.store.resetStats();
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
exports.CacheService = CacheService;
|
|
195
|
+
exports.CacheService = CacheService = __decorate([
|
|
196
|
+
(0, core_1.Service)(),
|
|
197
|
+
__metadata("design:paramtypes", [String, Object])
|
|
198
|
+
], CacheService);
|
|
199
|
+
/**
|
|
200
|
+
* Cache manager for managing multiple cache instances
|
|
201
|
+
*/
|
|
202
|
+
class CacheManager {
|
|
203
|
+
constructor() {
|
|
204
|
+
this.caches = new Map();
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Register a cache instance
|
|
208
|
+
*/
|
|
209
|
+
register(name, cache, isDefault = false) {
|
|
210
|
+
this.caches.set(name, cache);
|
|
211
|
+
if (isDefault || !this.defaultCache) {
|
|
212
|
+
this.defaultCache = cache;
|
|
213
|
+
}
|
|
214
|
+
core_2.default.info(`Cache registered: ${name}${isDefault ? ' (default)' : ''}`);
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Get a cache instance by name
|
|
218
|
+
*/
|
|
219
|
+
get(name) {
|
|
220
|
+
if (!name) {
|
|
221
|
+
if (!this.defaultCache) {
|
|
222
|
+
throw new Error('No default cache configured');
|
|
223
|
+
}
|
|
224
|
+
return this.defaultCache;
|
|
225
|
+
}
|
|
226
|
+
const cache = this.caches.get(name);
|
|
227
|
+
if (!cache) {
|
|
228
|
+
throw new Error(`Cache not found: ${name}`);
|
|
229
|
+
}
|
|
230
|
+
return cache;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Get all cache instances
|
|
234
|
+
*/
|
|
235
|
+
getAll() {
|
|
236
|
+
return this.caches;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Clear all caches
|
|
240
|
+
*/
|
|
241
|
+
async clearAll() {
|
|
242
|
+
await Promise.all(Array.from(this.caches.values()).map((cache) => cache.clear()));
|
|
243
|
+
core_2.default.info('All caches cleared');
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Get statistics for all caches
|
|
247
|
+
*/
|
|
248
|
+
async getAllStats() {
|
|
249
|
+
const stats = new Map();
|
|
250
|
+
for (const [name, cache] of this.caches.entries()) {
|
|
251
|
+
stats.set(name, await cache.getStats());
|
|
252
|
+
}
|
|
253
|
+
return stats;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Invalidate tags across all caches
|
|
257
|
+
*/
|
|
258
|
+
async invalidateTagsGlobal(tags) {
|
|
259
|
+
await Promise.all(Array.from(this.caches.values()).map((cache) => cache.invalidateTags(tags)));
|
|
260
|
+
core_2.default.info(`Invalidated tags globally: ${tags.join(', ')}`);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
exports.CacheManager = CacheManager;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cache.test.d.ts","sourceRoot":"","sources":["../src/cache.test.ts"],"names":[],"mappings":""}
|