itlab-internal-services 2.13.2 → 2.13.4
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/dist/modules/cache/cache-response.interceptor.d.ts +1 -1
- package/dist/modules/cache/cache-response.interceptor.js +3 -1
- package/dist/modules/cache/cache.module.d.ts +23 -13
- package/dist/modules/cache/cache.module.js +33 -62
- package/dist/modules/cache/cache.service.d.ts +45 -39
- package/dist/modules/cache/cache.service.js +87 -54
- package/dist/modules/content/content.module.js +1 -1
- package/package.json +2 -4
|
@@ -17,7 +17,9 @@ const common_1 = require("@nestjs/common");
|
|
|
17
17
|
*
|
|
18
18
|
* @returns {MethodDecorator} The decorator function that applies the cache interceptor to the method.
|
|
19
19
|
*/
|
|
20
|
-
const CacheResponse = () => {
|
|
20
|
+
const CacheResponse = (ttl) => {
|
|
21
|
+
if (ttl)
|
|
22
|
+
return (0, common_1.applyDecorators)((0, cache_manager_1.CacheTTL)(ttl), (0, common_1.UseInterceptors)(cache_manager_1.CacheInterceptor));
|
|
21
23
|
return (0, common_1.UseInterceptors)(cache_manager_1.CacheInterceptor);
|
|
22
24
|
};
|
|
23
25
|
exports.CacheResponse = CacheResponse;
|
|
@@ -1,24 +1,34 @@
|
|
|
1
1
|
import { DynamicModule } from '@nestjs/common';
|
|
2
2
|
/**
|
|
3
|
-
*
|
|
3
|
+
* CacheModule
|
|
4
4
|
*
|
|
5
|
-
* This module
|
|
6
|
-
* It
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* globally and provides a `CacheService` to manage cache interactions.
|
|
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. The module
|
|
7
|
+
* leverages `Keyv` for Redis integration. Designed to be imported globally, it offers a `CacheService`
|
|
8
|
+
* to manage cache interactions efficiently.
|
|
10
9
|
*/
|
|
11
10
|
export declare class CacheModule {
|
|
12
11
|
/**
|
|
13
|
-
* Configures the CacheModule with the necessary providers and imports.
|
|
12
|
+
* Configures the CacheModule globally with the necessary providers and imports.
|
|
14
13
|
*
|
|
15
|
-
* This method sets up the cache stores,
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
14
|
+
* This method sets up the cache stores, attempting to connect to a Redis instance using configuration
|
|
15
|
+
* values from the environment. If Redis is unavailable, it won't cache anything.
|
|
16
|
+
* It registers the cache store with the `NestCacheModule` and makes the `CacheService` available
|
|
17
|
+
* globally for caching operations.
|
|
19
18
|
*
|
|
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.
|
|
19
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
20
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
22
21
|
*/
|
|
23
22
|
static forRoot(namespace: string): DynamicModule;
|
|
23
|
+
/**
|
|
24
|
+
* Configures the CacheModule locally with the necessary providers and imports.
|
|
25
|
+
*
|
|
26
|
+
* This method sets up the cache stores similarly to `forRoot`, but the module is not global.
|
|
27
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
28
|
+
* for caching operations within the importing module.
|
|
29
|
+
*
|
|
30
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
31
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
32
|
+
*/
|
|
33
|
+
static register(namespace: string): DynamicModule;
|
|
24
34
|
}
|
|
@@ -5,87 +5,58 @@ 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
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* globally and provides a `CacheService` to manage cache interactions.
|
|
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. The module
|
|
19
|
+
* leverages `Keyv` for Redis integration. Designed to be imported globally, it offers a `CacheService`
|
|
20
|
+
* to manage cache interactions efficiently.
|
|
37
21
|
*/
|
|
38
22
|
let CacheModule = CacheModule_1 = class CacheModule {
|
|
39
23
|
/**
|
|
40
|
-
* Configures the CacheModule with the necessary providers and imports.
|
|
24
|
+
* Configures the CacheModule globally with the necessary providers and imports.
|
|
41
25
|
*
|
|
42
|
-
* This method sets up the cache stores,
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
26
|
+
* This method sets up the cache stores, attempting to connect to a Redis instance using configuration
|
|
27
|
+
* values from the environment. If Redis is unavailable, it won't cache anything.
|
|
28
|
+
* It registers the cache store with the `NestCacheModule` and makes the `CacheService` available
|
|
29
|
+
* globally for caching operations.
|
|
46
30
|
*
|
|
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.
|
|
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.
|
|
49
33
|
*/
|
|
50
34
|
static forRoot(namespace) {
|
|
51
35
|
return {
|
|
52
36
|
global: true,
|
|
53
37
|
module: CacheModule_1,
|
|
54
|
-
|
|
38
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace, ttl: 300000 })], // Default TTL = 5 minutes
|
|
39
|
+
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
40
|
+
exports: [cache_service_1.CacheService],
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Configures the CacheModule locally with the necessary providers and imports.
|
|
45
|
+
*
|
|
46
|
+
* This method sets up the cache stores similarly to `forRoot`, but the module is not global.
|
|
47
|
+
* It registers the cache stores with the `NestCacheModule` and makes the `CacheService` available
|
|
48
|
+
* for caching operations within the importing module.
|
|
49
|
+
*
|
|
50
|
+
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
51
|
+
* @returns {DynamicModule} - The configured dynamic module, including the cache providers and stores.
|
|
52
|
+
*/
|
|
53
|
+
static register(namespace) {
|
|
54
|
+
return {
|
|
55
|
+
global: false,
|
|
56
|
+
module: CacheModule_1,
|
|
57
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace, ttl: 300000 })], // Default TTL = 5 minutes
|
|
58
|
+
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
55
59
|
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
60
|
};
|
|
90
61
|
}
|
|
91
62
|
};
|
|
@@ -1,71 +1,77 @@
|
|
|
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
|
-
* the result. All cache operations are non-blocking and utilize background
|
|
9
|
-
* processing for setting cache values.
|
|
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 Redis caching.
|
|
10
10
|
*/
|
|
11
|
-
export declare class CacheService {
|
|
11
|
+
export declare class CacheService implements OnModuleInit {
|
|
12
12
|
private readonly cache;
|
|
13
|
+
private readonly namespace;
|
|
14
|
+
private readonly configService;
|
|
15
|
+
/** Logger instance for tracking cache operations and debugging. */
|
|
13
16
|
private readonly logger;
|
|
14
17
|
/**
|
|
15
18
|
* Constructor for the CacheService.
|
|
16
|
-
*
|
|
19
|
+
*
|
|
20
|
+
* @param {Cache} cache - The cache manager instance injected by NestJS.
|
|
21
|
+
* @param {string} namespace - The namespace for cache keys to avoid collisions.
|
|
22
|
+
* @param {ConfigService} configService - Service for accessing configuration variables.
|
|
23
|
+
*/
|
|
24
|
+
constructor(cache: Cache, namespace: string, configService: ConfigService);
|
|
25
|
+
/**
|
|
26
|
+
* Lifecycle hook that is called when the module is initialized.
|
|
27
|
+
*
|
|
28
|
+
* This method ensures that cache stores are set up when the module is loaded.
|
|
29
|
+
*
|
|
30
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
31
|
+
*/
|
|
32
|
+
onModuleInit(): Promise<void>;
|
|
33
|
+
/**
|
|
34
|
+
* Sets up the cache store, attempting to connect to Redis.
|
|
35
|
+
*
|
|
36
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
17
37
|
*/
|
|
18
|
-
|
|
38
|
+
private setupStore;
|
|
19
39
|
/**
|
|
20
40
|
* Fetches the value associated with the given key from the cache.
|
|
21
41
|
*
|
|
22
|
-
* @param key The cache key to retrieve the value.
|
|
23
|
-
* @returns The cached value, or null if the key does not exist
|
|
42
|
+
* @param {string} key - The cache key to retrieve the value.
|
|
43
|
+
* @returns {Promise<T | null>} - The cached value, or null if the key does not exist.
|
|
24
44
|
*/
|
|
25
45
|
fetch<T>(key: string): Promise<T | null>;
|
|
26
46
|
/**
|
|
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.
|
|
47
|
+
* Stores a value in the cache under the specified key, with optional expiration.
|
|
30
48
|
*
|
|
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
|
|
49
|
+
* @param {string} key - The cache key under which to store the value.
|
|
50
|
+
* @param {T} value - The value to be stored in the cache.
|
|
51
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
52
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
35
53
|
*/
|
|
36
54
|
store<T>(key: string, value: T, expiration?: number): Promise<void>;
|
|
37
55
|
/**
|
|
38
56
|
* Removes the cached value associated with the given key.
|
|
39
57
|
*
|
|
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.
|
|
58
|
+
* @param {string} key - The cache key to remove from the cache.
|
|
59
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
45
60
|
*/
|
|
46
61
|
remove(key: string): Promise<void>;
|
|
47
62
|
/**
|
|
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.
|
|
63
|
+
* Clears all cached values in the cache, effectively resetting the cache state.
|
|
52
64
|
*
|
|
53
|
-
* @returns
|
|
65
|
+
* @returns {Promise<void>} - Resolves once the cache has been cleared.
|
|
54
66
|
*/
|
|
55
67
|
clearAll(): Promise<void>;
|
|
56
68
|
/**
|
|
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.
|
|
69
|
+
* Retrieves the value from the cache, or executes the provided function to fetch and store the value if not cached.
|
|
64
70
|
*
|
|
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.
|
|
71
|
+
* @param {string} key - The cache key to check or store the value.
|
|
72
|
+
* @param {() => Promise<T>} fetcher - Function that computes the value if it's not cached.
|
|
73
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
74
|
+
* @returns {Promise<T>} - The cached value or the newly computed value.
|
|
69
75
|
*/
|
|
70
76
|
fetchOrStore<T>(key: string, fetcher: () => Promise<T>, expiration?: number): Promise<T>;
|
|
71
77
|
}
|
|
@@ -23,32 +23,91 @@ 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 env_1 = require("../../env");
|
|
28
32
|
/**
|
|
29
|
-
*
|
|
33
|
+
* CacheService
|
|
30
34
|
*
|
|
31
|
-
* This
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* the result. All cache operations are non-blocking and utilize background
|
|
35
|
-
* processing for setting cache values.
|
|
35
|
+
* This service manages caching operations in a centralized manner, providing methods for
|
|
36
|
+
* retrieving, storing, and deleting cached values. It handles cache misses by executing a
|
|
37
|
+
* fallback function and asynchronously storing the result. The service supports Redis caching.
|
|
36
38
|
*/
|
|
37
39
|
let CacheService = CacheService_1 = class CacheService {
|
|
38
40
|
/**
|
|
39
41
|
* Constructor for the CacheService.
|
|
40
|
-
*
|
|
42
|
+
*
|
|
43
|
+
* @param {Cache} cache - The cache manager instance injected by NestJS.
|
|
44
|
+
* @param {string} namespace - The namespace for cache keys to avoid collisions.
|
|
45
|
+
* @param {ConfigService} configService - Service for accessing configuration variables.
|
|
41
46
|
*/
|
|
42
|
-
constructor(cache) {
|
|
47
|
+
constructor(cache, namespace, configService) {
|
|
43
48
|
this.cache = cache;
|
|
44
|
-
|
|
49
|
+
this.namespace = namespace;
|
|
50
|
+
this.configService = configService;
|
|
51
|
+
/** Logger instance for tracking cache operations and debugging. */
|
|
45
52
|
this.logger = new common_1.Logger(CacheService_1.name);
|
|
46
53
|
}
|
|
54
|
+
/**
|
|
55
|
+
* Lifecycle hook that is called when the module is initialized.
|
|
56
|
+
*
|
|
57
|
+
* This method ensures that cache stores are set up when the module is loaded.
|
|
58
|
+
*
|
|
59
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
60
|
+
*/
|
|
61
|
+
onModuleInit() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
yield this.setupStore();
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Sets up the cache store, attempting to connect to Redis.
|
|
68
|
+
*
|
|
69
|
+
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
70
|
+
*/
|
|
71
|
+
setupStore() {
|
|
72
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
try {
|
|
74
|
+
// Configure Redis store using environment variables
|
|
75
|
+
const host = this.configService.getOrThrow(env_1.ENV_REDIS_HOST);
|
|
76
|
+
const port = this.configService.getOrThrow(env_1.ENV_REDIS_PORT);
|
|
77
|
+
const user = this.configService.getOrThrow(env_1.ENV_REDIS_USER);
|
|
78
|
+
// Obtain a token credential for Redis access
|
|
79
|
+
const credential = new identity_1.WorkloadIdentityCredential();
|
|
80
|
+
const redisScope = 'https://redis.azure.com/.default';
|
|
81
|
+
this.logger.log('Obtaining new Redis access token...');
|
|
82
|
+
const accessToken = yield credential.getToken(redisScope);
|
|
83
|
+
const { token: password, refreshAfterTimestamp } = accessToken;
|
|
84
|
+
this.logger.log(`New token received. Refresh at: ${new Date(refreshAfterTimestamp).toISOString()}`);
|
|
85
|
+
// Initialize Redis store using Keyv and Redis
|
|
86
|
+
const redisStore = (0, redis_1.createKeyv)({ username: user, password, socket: { host, port, tls: true, timeout: 5000 } }, { namespace: this.namespace });
|
|
87
|
+
// Check Redis server availability
|
|
88
|
+
yield redisStore.get('ping');
|
|
89
|
+
this.logger.log('Redis is available. Using Redis store for caching.');
|
|
90
|
+
const now = Date.now();
|
|
91
|
+
const refreshDelay = Math.max(refreshAfterTimestamp - now, 0); // Ensure non-negative delay
|
|
92
|
+
// Calculate total minutes and break it into hours and minutes
|
|
93
|
+
const totalMinutes = refreshDelay / 1000 / 60;
|
|
94
|
+
const hours = Math.floor(totalMinutes / 60).toString(); // Get the whole number of hours
|
|
95
|
+
const minutes = Math.floor(totalMinutes % 60).toString(); // Get the remaining minutes
|
|
96
|
+
this.logger.log(`Next token refresh scheduled in ${hours.padStart(2, '0')}h ${minutes.padStart(2, '0')}m.`);
|
|
97
|
+
setTimeout(() => __awaiter(this, void 0, void 0, function* () { return yield this.setupStore(); }), refreshDelay);
|
|
98
|
+
this.cache.stores = [redisStore];
|
|
99
|
+
}
|
|
100
|
+
catch (error) {
|
|
101
|
+
this.logger.error('Redis is unavailable: ' + error);
|
|
102
|
+
this.cache.stores = [];
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
47
106
|
/**
|
|
48
107
|
* Fetches the value associated with the given key from the cache.
|
|
49
108
|
*
|
|
50
|
-
* @param key The cache key to retrieve the value.
|
|
51
|
-
* @returns The cached value, or null if the key does not exist
|
|
109
|
+
* @param {string} key - The cache key to retrieve the value.
|
|
110
|
+
* @returns {Promise<T | null>} - The cached value, or null if the key does not exist.
|
|
52
111
|
*/
|
|
53
112
|
fetch(key) {
|
|
54
113
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -56,14 +115,12 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
56
115
|
});
|
|
57
116
|
}
|
|
58
117
|
/**
|
|
59
|
-
* Stores a value in the cache under the specified key.
|
|
118
|
+
* Stores a value in the cache under the specified key, with optional expiration.
|
|
60
119
|
*
|
|
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.
|
|
120
|
+
* @param {string} key - The cache key under which to store the value.
|
|
121
|
+
* @param {T} value - The value to be stored in the cache.
|
|
122
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
123
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
67
124
|
*/
|
|
68
125
|
store(key, value, expiration) {
|
|
69
126
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -73,11 +130,8 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
73
130
|
/**
|
|
74
131
|
* Removes the cached value associated with the given key.
|
|
75
132
|
*
|
|
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.
|
|
133
|
+
* @param {string} key - The cache key to remove from the cache.
|
|
134
|
+
* @returns {Promise<void>} - Resolves once the cache operation completes.
|
|
81
135
|
*/
|
|
82
136
|
remove(key) {
|
|
83
137
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -85,12 +139,9 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
85
139
|
});
|
|
86
140
|
}
|
|
87
141
|
/**
|
|
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.
|
|
142
|
+
* Clears all cached values in the cache, effectively resetting the cache state.
|
|
92
143
|
*
|
|
93
|
-
* @returns
|
|
144
|
+
* @returns {Promise<void>} - Resolves once the cache has been cleared.
|
|
94
145
|
*/
|
|
95
146
|
clearAll() {
|
|
96
147
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -98,35 +149,16 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
98
149
|
});
|
|
99
150
|
}
|
|
100
151
|
/**
|
|
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.
|
|
152
|
+
* Retrieves the value from the cache, or executes the provided function to fetch and store the value if not cached.
|
|
103
153
|
*
|
|
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.
|
|
154
|
+
* @param {string} key - The cache key to check or store the value.
|
|
155
|
+
* @param {() => Promise<T>} fetcher - Function that computes the value if it's not cached.
|
|
156
|
+
* @param {number} [expiration] - Optional expiration time in seconds for the cached value.
|
|
157
|
+
* @returns {Promise<T>} - The cached value or the newly computed value.
|
|
113
158
|
*/
|
|
114
159
|
fetchOrStore(key, fetcher, expiration) {
|
|
115
160
|
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;
|
|
161
|
+
return this.cache.wrap(key, fetcher, { ttl: expiration });
|
|
130
162
|
});
|
|
131
163
|
}
|
|
132
164
|
};
|
|
@@ -134,5 +166,6 @@ exports.CacheService = CacheService;
|
|
|
134
166
|
exports.CacheService = CacheService = CacheService_1 = __decorate([
|
|
135
167
|
(0, common_1.Injectable)(),
|
|
136
168
|
__param(0, (0, common_1.Inject)(cache_manager_1.CACHE_MANAGER)),
|
|
137
|
-
|
|
169
|
+
__param(1, (0, common_1.Inject)('CACHE_NAMESPACE')),
|
|
170
|
+
__metadata("design:paramtypes", [Object, String, config_1.ConfigService])
|
|
138
171
|
], 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],
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"email": "timo.scheuermann@sv-informatik.de",
|
|
6
6
|
"url": "https://timos.design"
|
|
7
7
|
},
|
|
8
|
-
"version": "2.13.
|
|
8
|
+
"version": "2.13.4",
|
|
9
9
|
"type": "commonjs",
|
|
10
10
|
"files": [
|
|
11
11
|
"dist"
|
|
@@ -29,9 +29,7 @@
|
|
|
29
29
|
"@nestjs/jwt": "^11.0.0",
|
|
30
30
|
"axios": "^1.8.3",
|
|
31
31
|
"cache-manager": "^6.4.1",
|
|
32
|
-
"
|
|
33
|
-
"itlab-functions": "^0.7.9",
|
|
34
|
-
"keyv": "^5.3.2"
|
|
32
|
+
"itlab-functions": "^0.7.9"
|
|
35
33
|
},
|
|
36
34
|
"devDependencies": {
|
|
37
35
|
"@nestjs/common": "^11.0.11",
|