itlab-internal-services 2.13.2 → 2.13.3
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.
|
@@ -1,24 +1,35 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* CacheModule
|
|
4
4
|
*
|
|
5
|
-
* This module
|
|
6
|
-
* It
|
|
7
|
-
* in-memory cache if Redis is unavailable. The module
|
|
8
|
-
*
|
|
9
|
-
*
|
|
5
|
+
* This module is responsible for setting up and configuring caching mechanisms within the application.
|
|
6
|
+
* It provides a flexible caching solution that integrates with Redis for persistent caching and falls back
|
|
7
|
+
* to an in-memory cache if Redis is unavailable. The module leverages `Keyv` for Redis integration and
|
|
8
|
+
* `CacheableMemory` for the in-memory store. Designed to be imported globally, it offers a `CacheService`
|
|
9
|
+
* to manage cache interactions efficiently.
|
|
10
10
|
*/
|
|
11
11
|
export declare class CacheModule {
|
|
12
12
|
/**
|
|
13
|
-
* Configures the CacheModule with the necessary providers and imports.
|
|
13
|
+
* Configures the CacheModule globally with the necessary providers and imports.
|
|
14
14
|
*
|
|
15
|
-
* This method sets up the cache stores,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
15
|
+
* This method sets up the cache stores, attempting to connect to a Redis instance using configuration
|
|
16
|
+
* values from the environment. If Redis is unavailable, it defaults to using an in-memory store.
|
|
17
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
18
|
+
* globally for caching operations.
|
|
19
19
|
*
|
|
20
|
-
* @param namespace The namespace used for cache keys to avoid key collisions.
|
|
21
|
-
* @returns The configured dynamic module, including the cache providers and stores.
|
|
20
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
21
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
22
22
|
*/
|
|
23
23
|
static forRoot(namespace: string): DynamicModule;
|
|
24
|
+
/**
|
|
25
|
+
* Configures the CacheModule locally with the necessary providers and imports.
|
|
26
|
+
*
|
|
27
|
+
* This method sets up the cache stores similarly to `forRoot`, but the module is not global.
|
|
28
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
29
|
+
* for caching operations within the importing module.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
32
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
33
|
+
*/
|
|
34
|
+
static register(namespace: string): DynamicModule;
|
|
24
35
|
}
|
|
@@ -5,87 +5,59 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
5
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
6
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
7
|
};
|
|
8
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
9
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
10
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
11
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
12
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
13
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
14
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
15
|
-
});
|
|
16
|
-
};
|
|
17
8
|
var CacheModule_1;
|
|
18
9
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
19
10
|
exports.CacheModule = void 0;
|
|
20
|
-
const identity_1 = require("@azure/identity");
|
|
21
|
-
const redis_1 = require("@keyv/redis");
|
|
22
11
|
const cache_manager_1 = require("@nestjs/cache-manager");
|
|
23
12
|
const common_1 = require("@nestjs/common");
|
|
24
|
-
const config_1 = require("@nestjs/config");
|
|
25
|
-
const cacheable_1 = require("cacheable");
|
|
26
|
-
const keyv_1 = require("keyv");
|
|
27
|
-
const env_1 = require("../../env");
|
|
28
13
|
const cache_service_1 = require("./cache.service");
|
|
29
14
|
/**
|
|
30
|
-
*
|
|
15
|
+
* CacheModule
|
|
31
16
|
*
|
|
32
|
-
* This module
|
|
33
|
-
* It
|
|
34
|
-
* in-memory cache if Redis is unavailable. The module
|
|
35
|
-
*
|
|
36
|
-
*
|
|
17
|
+
* This module is responsible for setting up and configuring caching mechanisms within the application.
|
|
18
|
+
* It provides a flexible caching solution that integrates with Redis for persistent caching and falls back
|
|
19
|
+
* to an in-memory cache if Redis is unavailable. The module leverages `Keyv` for Redis integration and
|
|
20
|
+
* `CacheableMemory` for the in-memory store. Designed to be imported globally, it offers a `CacheService`
|
|
21
|
+
* to manage cache interactions efficiently.
|
|
37
22
|
*/
|
|
38
23
|
let CacheModule = CacheModule_1 = class CacheModule {
|
|
39
24
|
/**
|
|
40
|
-
* Configures the CacheModule with the necessary providers and imports.
|
|
25
|
+
* Configures the CacheModule globally with the necessary providers and imports.
|
|
41
26
|
*
|
|
42
|
-
* This method sets up the cache stores,
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
27
|
+
* This method sets up the cache stores, attempting to connect to a Redis instance using configuration
|
|
28
|
+
* values from the environment. If Redis is unavailable, it defaults to using an in-memory store.
|
|
29
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
30
|
+
* globally for caching operations.
|
|
46
31
|
*
|
|
47
|
-
* @param namespace The namespace used for cache keys to avoid key collisions.
|
|
48
|
-
* @returns The configured dynamic module, including the cache providers and stores.
|
|
32
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
33
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
49
34
|
*/
|
|
50
35
|
static forRoot(namespace) {
|
|
51
36
|
return {
|
|
52
37
|
global: true,
|
|
53
38
|
module: CacheModule_1,
|
|
54
|
-
|
|
39
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace })],
|
|
40
|
+
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
41
|
+
exports: [cache_service_1.CacheService],
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Configures the CacheModule locally with the necessary providers and imports.
|
|
46
|
+
*
|
|
47
|
+
* This method sets up the cache stores similarly to `forRoot`, but the module is not global.
|
|
48
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
49
|
+
* for caching operations within the importing module.
|
|
50
|
+
*
|
|
51
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
52
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
53
|
+
*/
|
|
54
|
+
static register(namespace) {
|
|
55
|
+
return {
|
|
56
|
+
global: false,
|
|
57
|
+
module: CacheModule_1,
|
|
58
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace })],
|
|
59
|
+
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
55
60
|
exports: [cache_service_1.CacheService],
|
|
56
|
-
imports: [
|
|
57
|
-
cache_manager_1.CacheModule.registerAsync({
|
|
58
|
-
isGlobal: true,
|
|
59
|
-
inject: [config_1.ConfigService],
|
|
60
|
-
useFactory: (configService) => __awaiter(this, void 0, void 0, function* () {
|
|
61
|
-
// Initialize in-memory cache store using CacheableMemory
|
|
62
|
-
const memoryStore = new keyv_1.Keyv({ store: new cacheable_1.CacheableMemory({ lruSize: 5000 }), namespace });
|
|
63
|
-
try {
|
|
64
|
-
// Attempt to configure Redis store using provided environment variables
|
|
65
|
-
const host = configService.getOrThrow(env_1.ENV_REDIS_HOST);
|
|
66
|
-
const port = configService.getOrThrow(env_1.ENV_REDIS_PORT);
|
|
67
|
-
const user = configService.getOrThrow(env_1.ENV_REDIS_USER);
|
|
68
|
-
// Construct a Token Credential from Identity library
|
|
69
|
-
const credential = new identity_1.WorkloadIdentityCredential();
|
|
70
|
-
const redisScope = 'https://redis.azure.com/.default';
|
|
71
|
-
// Fetch a Microsoft Entra token to be used for authentication. This token will be used as the password.
|
|
72
|
-
const accessToken = yield credential.getToken(redisScope);
|
|
73
|
-
const password = accessToken.token;
|
|
74
|
-
const redisStore = (0, redis_1.createKeyv)({ username: user, password, socket: { host, port, tls: true, timeout: 5000 } }, { namespace });
|
|
75
|
-
// Ping the Redis server to check if it's available
|
|
76
|
-
yield redisStore.get('ping');
|
|
77
|
-
// Log success if Redis is available
|
|
78
|
-
new common_1.Logger('Cache').log('Redis is available.');
|
|
79
|
-
return { stores: [redisStore, memoryStore] };
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
// Log error and fallback to memory store if Redis is unavailable
|
|
83
|
-
new common_1.Logger('Cache').error('Redis is unavailable, falling back to memory store: ' + error);
|
|
84
|
-
return { stores: [memoryStore] };
|
|
85
|
-
}
|
|
86
|
-
}),
|
|
87
|
-
}),
|
|
88
|
-
],
|
|
89
61
|
};
|
|
90
62
|
}
|
|
91
63
|
};
|
|
@@ -1,71 +1,79 @@
|
|
|
1
|
+
import { OnModuleInit } from '@nestjs/common';
|
|
2
|
+
import { ConfigService } from '@nestjs/config';
|
|
1
3
|
import { Cache } from 'cache-manager';
|
|
2
4
|
/**
|
|
3
|
-
*
|
|
5
|
+
* CacheService
|
|
4
6
|
*
|
|
5
|
-
* This
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
7
|
+
* This service manages caching operations in a centralized manner, providing methods for
|
|
8
|
+
* retrieving, storing, and deleting cached values. It handles cache misses by executing a
|
|
9
|
+
* fallback function and asynchronously storing the result. The service supports both Redis
|
|
10
|
+
* and in-memory caching, using Redis for persistent storage and falling back to in-memory
|
|
11
|
+
* caching if Redis is unavailable.
|
|
10
12
|
*/
|
|
11
|
-
export declare class CacheService {
|
|
13
|
+
export declare class CacheService implements OnModuleInit {
|
|
12
14
|
private readonly cache;
|
|
15
|
+
private readonly namespace;
|
|
16
|
+
private readonly configService;
|
|
17
|
+
/** Logger instance for tracking cache operations and debugging. */
|
|
13
18
|
private readonly logger;
|
|
14
19
|
/**
|
|
15
20
|
* Constructor for the CacheService.
|
|
16
|
-
*
|
|
21
|
+
*
|
|
22
|
+
* @param {Cache} cache - The cache manager instance injected by NestJS.
|
|
23
|
+
* @param {string} namespace - The namespace for cache keys to avoid collisions.
|
|
24
|
+
* @param {ConfigService} configService - Service for accessing configuration variables.
|
|
25
|
+
*/
|
|
26
|
+
constructor(cache: Cache, namespace: string, configService: ConfigService);
|
|
27
|
+
/**
|
|
28
|
+
* Lifecycle hook that is called when the module is initialized.
|
|
29
|
+
*
|
|
30
|
+
* This method ensures that cache stores are set up when the module is loaded.
|
|
31
|
+
*
|
|
32
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
33
|
+
*/
|
|
34
|
+
onModuleInit(): Promise<void>;
|
|
35
|
+
/**
|
|
36
|
+
* Sets up the cache stores, attempting to connect to Redis and falling back to memory if necessary.
|
|
37
|
+
*
|
|
38
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
17
39
|
*/
|
|
18
|
-
|
|
40
|
+
private setupStores;
|
|
19
41
|
/**
|
|
20
42
|
* Fetches the value associated with the given key from the cache.
|
|
21
43
|
*
|
|
22
|
-
* @param key The cache key to retrieve the value.
|
|
23
|
-
* @returns The cached value, or null if the key does not exist
|
|
44
|
+
* @param {string} key - The cache key to retrieve the value.
|
|
45
|
+
* @returns {Promise<T | null>} - The cached value, or null if the key does not exist.
|
|
24
46
|
*/
|
|
25
47
|
fetch<T>(key: string): Promise<T | null>;
|
|
26
48
|
/**
|
|
27
|
-
* Stores a value in the cache under the specified key.
|
|
28
|
-
*
|
|
29
|
-
* This method can optionally set an expiration time for the cached value.
|
|
49
|
+
* Stores a value in the cache under the specified key, with optional expiration.
|
|
30
50
|
*
|
|
31
|
-
* @param key The cache key under which to store the value.
|
|
32
|
-
* @param value The value to be stored in the cache.
|
|
33
|
-
* @param expiration Optional expiration time in seconds for the cached value.
|
|
34
|
-
* @returns
|
|
51
|
+
* @param {string} key - The cache key under which to store the value.
|
|
52
|
+
* @param {T} value - The value to be stored in the cache.
|
|
53
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
54
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
35
55
|
*/
|
|
36
56
|
store<T>(key: string, value: T, expiration?: number): Promise<void>;
|
|
37
57
|
/**
|
|
38
58
|
* Removes the cached value associated with the given key.
|
|
39
59
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @param key The cache key to remove from the cache.
|
|
44
|
-
* @returns A promise that resolves once the cache operation completes.
|
|
60
|
+
* @param {string} key - The cache key to remove from the cache.
|
|
61
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
45
62
|
*/
|
|
46
63
|
remove(key: string): Promise<void>;
|
|
47
64
|
/**
|
|
48
|
-
* Clears all cached values in the cache.
|
|
49
|
-
*
|
|
50
|
-
* This operation removes all keys and values stored in the cache, effectively
|
|
51
|
-
* resetting the cache state.
|
|
65
|
+
* Clears all cached values in the cache, effectively resetting the cache state.
|
|
52
66
|
*
|
|
53
|
-
* @returns
|
|
67
|
+
* @returns {Promise<void>} - Resolves once the cache has been cleared.
|
|
54
68
|
*/
|
|
55
69
|
clearAll(): Promise<void>;
|
|
56
70
|
/**
|
|
57
|
-
* Retrieves the value from the cache, or executes the provided function
|
|
58
|
-
* to fetch and store the value if it is not already cached.
|
|
59
|
-
*
|
|
60
|
-
* This method helps optimize cache utilization by checking if the value is
|
|
61
|
-
* already cached before attempting to compute it. If the cache does not
|
|
62
|
-
* contain the value, the provided `fetcher` function is invoked, and the
|
|
63
|
-
* result is stored for future use.
|
|
71
|
+
* Retrieves the value from the cache, or executes the provided function to fetch and store the value if not cached.
|
|
64
72
|
*
|
|
65
|
-
* @param key The cache key to check or store the value.
|
|
66
|
-
* @param fetcher
|
|
67
|
-
* @param expiration Optional expiration time in seconds for the cached value.
|
|
68
|
-
* @returns The cached value or the newly computed value.
|
|
73
|
+
* @param {string} key - The cache key to check or store the value.
|
|
74
|
+
* @param {() => Promise<T>} fetcher - Function that computes the value if it's not cached.
|
|
75
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
76
|
+
* @returns {Promise<T>} - The cached value or the newly computed value.
|
|
69
77
|
*/
|
|
70
78
|
fetchOrStore<T>(key: string, fetcher: () => Promise<T>, expiration?: number): Promise<T>;
|
|
71
79
|
}
|
|
@@ -23,32 +23,93 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
23
23
|
var CacheService_1;
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
25
|
exports.CacheService = void 0;
|
|
26
|
+
const identity_1 = require("@azure/identity");
|
|
27
|
+
const redis_1 = require("@keyv/redis");
|
|
26
28
|
const cache_manager_1 = require("@nestjs/cache-manager");
|
|
27
29
|
const common_1 = require("@nestjs/common");
|
|
30
|
+
const config_1 = require("@nestjs/config");
|
|
31
|
+
const cacheable_1 = require("cacheable");
|
|
32
|
+
const env_1 = require("../../env");
|
|
28
33
|
/**
|
|
29
|
-
*
|
|
34
|
+
* CacheService
|
|
30
35
|
*
|
|
31
|
-
* This
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
+
* This service manages caching operations in a centralized manner, providing methods for
|
|
37
|
+
* retrieving, storing, and deleting cached values. It handles cache misses by executing a
|
|
38
|
+
* fallback function and asynchronously storing the result. The service supports both Redis
|
|
39
|
+
* and in-memory caching, using Redis for persistent storage and falling back to in-memory
|
|
40
|
+
* caching if Redis is unavailable.
|
|
36
41
|
*/
|
|
37
42
|
let CacheService = CacheService_1 = class CacheService {
|
|
38
43
|
/**
|
|
39
44
|
* Constructor for the CacheService.
|
|
40
|
-
*
|
|
45
|
+
*
|
|
46
|
+
* @param {Cache} cache - The cache manager instance injected by NestJS.
|
|
47
|
+
* @param {string} namespace - The namespace for cache keys to avoid collisions.
|
|
48
|
+
* @param {ConfigService} configService - Service for accessing configuration variables.
|
|
41
49
|
*/
|
|
42
|
-
constructor(cache) {
|
|
50
|
+
constructor(cache, namespace, configService) {
|
|
43
51
|
this.cache = cache;
|
|
44
|
-
|
|
52
|
+
this.namespace = namespace;
|
|
53
|
+
this.configService = configService;
|
|
54
|
+
/** Logger instance for tracking cache operations and debugging. */
|
|
45
55
|
this.logger = new common_1.Logger(CacheService_1.name);
|
|
46
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* Lifecycle hook that is called when the module is initialized.
|
|
59
|
+
*
|
|
60
|
+
* This method ensures that cache stores are set up when the module is loaded.
|
|
61
|
+
*
|
|
62
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
63
|
+
*/
|
|
64
|
+
onModuleInit() {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
+
yield this.setupStores();
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Sets up the cache stores, attempting to connect to Redis and falling back to memory if necessary.
|
|
71
|
+
*
|
|
72
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
73
|
+
*/
|
|
74
|
+
setupStores() {
|
|
75
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
76
|
+
// Initialize in-memory cache store using CacheableMemory
|
|
77
|
+
const memoryStore = new redis_1.Keyv({ store: new cacheable_1.CacheableMemory(), namespace: this.namespace });
|
|
78
|
+
try {
|
|
79
|
+
// Configure Redis store using environment variables
|
|
80
|
+
const host = this.configService.getOrThrow(env_1.ENV_REDIS_HOST);
|
|
81
|
+
const port = this.configService.getOrThrow(env_1.ENV_REDIS_PORT);
|
|
82
|
+
const user = this.configService.getOrThrow(env_1.ENV_REDIS_USER);
|
|
83
|
+
// Obtain a token credential for Redis access
|
|
84
|
+
const credential = new identity_1.WorkloadIdentityCredential();
|
|
85
|
+
const redisScope = 'https://redis.azure.com/.default';
|
|
86
|
+
this.logger.log('Obtaining new Redis access token...');
|
|
87
|
+
const accessToken = yield credential.getToken(redisScope);
|
|
88
|
+
const { token: password, refreshAfterTimestamp } = accessToken;
|
|
89
|
+
this.logger.log(`New token received. Refresh at: ${new Date(refreshAfterTimestamp).toISOString()}`);
|
|
90
|
+
// Initialize Redis store using Keyv and Redis
|
|
91
|
+
const redisStore = (0, redis_1.createKeyv)({ username: user, password, socket: { host, port, tls: true, timeout: 5000 } }, { namespace: this.namespace });
|
|
92
|
+
// Check Redis server availability
|
|
93
|
+
yield redisStore.get('ping');
|
|
94
|
+
this.logger.log('Redis is available. Using Redis store for caching.');
|
|
95
|
+
const now = Date.now();
|
|
96
|
+
const refreshDelay = Math.max(refreshAfterTimestamp - now, 0); // Ensure non-negative delay
|
|
97
|
+
this.logger.log(`Next token refresh scheduled in ${refreshDelay / 1000 / 60} minutes.`);
|
|
98
|
+
setTimeout(() => __awaiter(this, void 0, void 0, function* () { return yield this.setupStores(); }), refreshDelay);
|
|
99
|
+
this.cache.stores = [redisStore, memoryStore];
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
// Log error and fall back to memory store if Redis is unavailable
|
|
103
|
+
this.logger.error('Redis is unavailable, falling back to memory store: ' + error);
|
|
104
|
+
this.cache.stores = [memoryStore];
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
}
|
|
47
108
|
/**
|
|
48
109
|
* Fetches the value associated with the given key from the cache.
|
|
49
110
|
*
|
|
50
|
-
* @param key The cache key to retrieve the value.
|
|
51
|
-
* @returns The cached value, or null if the key does not exist
|
|
111
|
+
* @param {string} key - The cache key to retrieve the value.
|
|
112
|
+
* @returns {Promise<T | null>} - The cached value, or null if the key does not exist.
|
|
52
113
|
*/
|
|
53
114
|
fetch(key) {
|
|
54
115
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -56,14 +117,12 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
56
117
|
});
|
|
57
118
|
}
|
|
58
119
|
/**
|
|
59
|
-
* Stores a value in the cache under the specified key.
|
|
120
|
+
* Stores a value in the cache under the specified key, with optional expiration.
|
|
60
121
|
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @param
|
|
64
|
-
* @
|
|
65
|
-
* @param expiration Optional expiration time in seconds for the cached value.
|
|
66
|
-
* @returns A promise that resolves once the cache operation completes.
|
|
122
|
+
* @param {string} key - The cache key under which to store the value.
|
|
123
|
+
* @param {T} value - The value to be stored in the cache.
|
|
124
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
125
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
67
126
|
*/
|
|
68
127
|
store(key, value, expiration) {
|
|
69
128
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -73,11 +132,8 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
73
132
|
/**
|
|
74
133
|
* Removes the cached value associated with the given key.
|
|
75
134
|
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
78
|
-
*
|
|
79
|
-
* @param key The cache key to remove from the cache.
|
|
80
|
-
* @returns A promise that resolves once the cache operation completes.
|
|
135
|
+
* @param {string} key - The cache key to remove from the cache.
|
|
136
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
81
137
|
*/
|
|
82
138
|
remove(key) {
|
|
83
139
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -85,12 +141,9 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
85
141
|
});
|
|
86
142
|
}
|
|
87
143
|
/**
|
|
88
|
-
* Clears all cached values in the cache.
|
|
89
|
-
*
|
|
90
|
-
* This operation removes all keys and values stored in the cache, effectively
|
|
91
|
-
* resetting the cache state.
|
|
144
|
+
* Clears all cached values in the cache, effectively resetting the cache state.
|
|
92
145
|
*
|
|
93
|
-
* @returns
|
|
146
|
+
* @returns {Promise<void>} - Resolves once the cache has been cleared.
|
|
94
147
|
*/
|
|
95
148
|
clearAll() {
|
|
96
149
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -98,35 +151,16 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
98
151
|
});
|
|
99
152
|
}
|
|
100
153
|
/**
|
|
101
|
-
* Retrieves the value from the cache, or executes the provided function
|
|
102
|
-
* to fetch and store the value if it is not already cached.
|
|
154
|
+
* Retrieves the value from the cache, or executes the provided function to fetch and store the value if not cached.
|
|
103
155
|
*
|
|
104
|
-
*
|
|
105
|
-
*
|
|
106
|
-
*
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
* @param key The cache key to check or store the value.
|
|
110
|
-
* @param fetcher A function that computes the value if it's not cached.
|
|
111
|
-
* @param expiration Optional expiration time in seconds for the cached value.
|
|
112
|
-
* @returns The cached value or the newly computed value.
|
|
156
|
+
* @param {string} key - The cache key to check or store the value.
|
|
157
|
+
* @param {() => Promise<T>} fetcher - Function that computes the value if it's not cached.
|
|
158
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
159
|
+
* @returns {Promise<T>} - The cached value or the newly computed value.
|
|
113
160
|
*/
|
|
114
161
|
fetchOrStore(key, fetcher, expiration) {
|
|
115
162
|
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
this.
|
|
117
|
-
const cachedValue = yield this.fetch(key);
|
|
118
|
-
if (cachedValue) {
|
|
119
|
-
this.logger.log(`Cache hit for key: ${key}`);
|
|
120
|
-
return cachedValue;
|
|
121
|
-
}
|
|
122
|
-
this.logger.log(`Cache miss for key: ${key}. Fetching value.`);
|
|
123
|
-
const value = yield fetcher();
|
|
124
|
-
this.logger.log(`Storing value for key: ${key} with expiration: ${expiration !== null && expiration !== void 0 ? expiration : 'default'}`);
|
|
125
|
-
// Store the value in the background without blocking
|
|
126
|
-
this.store(key, value, expiration).catch((err) => {
|
|
127
|
-
this.logger.error(`Failed to store value for key: ${key}`, err);
|
|
128
|
-
});
|
|
129
|
-
return value;
|
|
163
|
+
return this.cache.wrap(key, fetcher, { ttl: expiration });
|
|
130
164
|
});
|
|
131
165
|
}
|
|
132
166
|
};
|
|
@@ -134,5 +168,6 @@ exports.CacheService = CacheService;
|
|
|
134
168
|
exports.CacheService = CacheService = CacheService_1 = __decorate([
|
|
135
169
|
(0, common_1.Injectable)(),
|
|
136
170
|
__param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
|
137
|
-
|
|
171
|
+
__param(1, (0, common_1.Inject)('CACHE_NAMESPACE')),
|
|
172
|
+
__metadata("design:paramtypes", [Object, String, config_1.ConfigService])
|
|
138
173
|
], CacheService);
|
|
@@ -33,7 +33,7 @@ let ContentModule = ContentModule_1 = class ContentModule {
|
|
|
33
33
|
*/
|
|
34
34
|
static register(resource) {
|
|
35
35
|
return {
|
|
36
|
-
global:
|
|
36
|
+
global: false,
|
|
37
37
|
module: ContentModule_1,
|
|
38
38
|
providers: [content_service_1.ContentService, { provide: 'CONTENT_RESOURCE', useValue: resource }],
|
|
39
39
|
exports: [content_service_1.ContentService],
|