@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,155 @@
|
|
|
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.Cache = Cache;
|
|
7
|
+
exports.getCacheMetadata = getCacheMetadata;
|
|
8
|
+
exports.hasCacheMetadata = hasCacheMetadata;
|
|
9
|
+
exports.CacheKey = CacheKey;
|
|
10
|
+
exports.CacheTTL = CacheTTL;
|
|
11
|
+
exports.CacheTags = CacheTags;
|
|
12
|
+
exports.CacheEvict = CacheEvict;
|
|
13
|
+
exports.getCacheEvictMetadata = getCacheEvictMetadata;
|
|
14
|
+
require("reflect-metadata");
|
|
15
|
+
const core_1 = __importDefault(require("@hazeljs/core"));
|
|
16
|
+
const CACHE_METADATA_KEY = 'hazel:cache';
|
|
17
|
+
/**
|
|
18
|
+
* Cache decorator for methods
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* @Cache({
|
|
23
|
+
* strategy: 'memory',
|
|
24
|
+
* ttl: 3600,
|
|
25
|
+
* key: 'user-{id}',
|
|
26
|
+
* tags: ['users']
|
|
27
|
+
* })
|
|
28
|
+
* @Get('/users/:id')
|
|
29
|
+
* async getUser(@Param('id') id: string) {
|
|
30
|
+
* return this.userService.findById(id);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
function Cache(options = {}) {
|
|
35
|
+
return (target, propertyKey, descriptor) => {
|
|
36
|
+
const defaults = {
|
|
37
|
+
strategy: 'memory',
|
|
38
|
+
ttl: 3600,
|
|
39
|
+
ttlStrategy: 'absolute',
|
|
40
|
+
cacheNull: false,
|
|
41
|
+
...options,
|
|
42
|
+
};
|
|
43
|
+
// Store metadata
|
|
44
|
+
Reflect.defineMetadata(CACHE_METADATA_KEY, defaults, target, propertyKey);
|
|
45
|
+
core_1.default.debug(`Cache decorator applied to ${target.constructor.name}.${String(propertyKey)}`);
|
|
46
|
+
// Note: The actual caching logic will be implemented by the CacheInterceptor
|
|
47
|
+
// This decorator just marks the method and stores configuration
|
|
48
|
+
return descriptor;
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get cache metadata from a method
|
|
53
|
+
*/
|
|
54
|
+
function getCacheMetadata(target, propertyKey) {
|
|
55
|
+
return Reflect.getMetadata(CACHE_METADATA_KEY, target, propertyKey);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check if a method has cache metadata
|
|
59
|
+
*/
|
|
60
|
+
function hasCacheMetadata(target, propertyKey) {
|
|
61
|
+
return Reflect.hasMetadata(CACHE_METADATA_KEY, target, propertyKey);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* CacheKey decorator to specify custom cache key generation
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* @CacheKey('user-{id}-{role}')
|
|
69
|
+
* @Get('/users/:id')
|
|
70
|
+
* async getUser(@Param('id') id: string, @Query('role') role: string) {
|
|
71
|
+
* return this.userService.findById(id);
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function CacheKey(keyPattern) {
|
|
76
|
+
return (target, propertyKey, descriptor) => {
|
|
77
|
+
const existingMetadata = getCacheMetadata(target, propertyKey) || {};
|
|
78
|
+
const updatedMetadata = {
|
|
79
|
+
...existingMetadata,
|
|
80
|
+
key: keyPattern,
|
|
81
|
+
};
|
|
82
|
+
Reflect.defineMetadata(CACHE_METADATA_KEY, updatedMetadata, target, propertyKey);
|
|
83
|
+
return descriptor;
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* CacheTTL decorator to specify cache TTL
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* @CacheTTL(7200) // 2 hours
|
|
92
|
+
* @Get('/users')
|
|
93
|
+
* async getUsers() {
|
|
94
|
+
* return this.userService.findAll();
|
|
95
|
+
* }
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
function CacheTTL(ttl) {
|
|
99
|
+
return (target, propertyKey, descriptor) => {
|
|
100
|
+
const existingMetadata = getCacheMetadata(target, propertyKey) || {};
|
|
101
|
+
const updatedMetadata = {
|
|
102
|
+
...existingMetadata,
|
|
103
|
+
ttl,
|
|
104
|
+
};
|
|
105
|
+
Reflect.defineMetadata(CACHE_METADATA_KEY, updatedMetadata, target, propertyKey);
|
|
106
|
+
return descriptor;
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* CacheTags decorator to specify cache tags
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* @CacheTags(['users', 'profiles'])
|
|
115
|
+
* @Get('/users/:id/profile')
|
|
116
|
+
* async getUserProfile(@Param('id') id: string) {
|
|
117
|
+
* return this.userService.getProfile(id);
|
|
118
|
+
* }
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
function CacheTags(tags) {
|
|
122
|
+
return (target, propertyKey, descriptor) => {
|
|
123
|
+
const existingMetadata = getCacheMetadata(target, propertyKey) || {};
|
|
124
|
+
const updatedMetadata = {
|
|
125
|
+
...existingMetadata,
|
|
126
|
+
tags,
|
|
127
|
+
};
|
|
128
|
+
Reflect.defineMetadata(CACHE_METADATA_KEY, updatedMetadata, target, propertyKey);
|
|
129
|
+
return descriptor;
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* CacheEvict decorator to evict cache entries
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* @CacheEvict({ tags: ['users'] })
|
|
138
|
+
* @Post('/users')
|
|
139
|
+
* async createUser(@Body() user: CreateUserDto) {
|
|
140
|
+
* return this.userService.create(user);
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
function CacheEvict(options) {
|
|
145
|
+
return (target, propertyKey, descriptor) => {
|
|
146
|
+
Reflect.defineMetadata('hazel:cache:evict', options, target, propertyKey);
|
|
147
|
+
return descriptor;
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Get cache evict metadata
|
|
152
|
+
*/
|
|
153
|
+
function getCacheEvictMetadata(target, propertyKey) {
|
|
154
|
+
return Reflect.getMetadata('hazel:cache:evict', target, propertyKey);
|
|
155
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -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"}
|
package/dist/index.js
ADDED
|
@@ -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,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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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":"AACA,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,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