itlab-internal-services 2.13.3 → 2.13.5
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 +4 -5
- package/dist/modules/cache/cache.module.js +6 -7
- package/dist/modules/cache/cache.service.d.ts +3 -5
- package/dist/modules/cache/cache.service.js +14 -16
- 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;
|
|
@@ -3,9 +3,8 @@ import { DynamicModule } from '@nestjs/common';
|
|
|
3
3
|
* CacheModule
|
|
4
4
|
*
|
|
5
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
|
|
7
|
-
*
|
|
8
|
-
* `CacheableMemory` for the in-memory store. Designed to be imported globally, it offers a `CacheService`
|
|
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`
|
|
9
8
|
* to manage cache interactions efficiently.
|
|
10
9
|
*/
|
|
11
10
|
export declare class CacheModule {
|
|
@@ -13,8 +12,8 @@ export declare class CacheModule {
|
|
|
13
12
|
* Configures the CacheModule globally with the necessary providers and imports.
|
|
14
13
|
*
|
|
15
14
|
* 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
|
|
17
|
-
* It registers the cache
|
|
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
|
|
18
17
|
* globally for caching operations.
|
|
19
18
|
*
|
|
20
19
|
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
@@ -15,9 +15,8 @@ const cache_service_1 = require("./cache.service");
|
|
|
15
15
|
* CacheModule
|
|
16
16
|
*
|
|
17
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
|
|
19
|
-
*
|
|
20
|
-
* `CacheableMemory` for the in-memory store. Designed to be imported globally, it offers a `CacheService`
|
|
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`
|
|
21
20
|
* to manage cache interactions efficiently.
|
|
22
21
|
*/
|
|
23
22
|
let CacheModule = CacheModule_1 = class CacheModule {
|
|
@@ -25,8 +24,8 @@ let CacheModule = CacheModule_1 = class CacheModule {
|
|
|
25
24
|
* Configures the CacheModule globally with the necessary providers and imports.
|
|
26
25
|
*
|
|
27
26
|
* 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
|
|
29
|
-
* It registers the cache
|
|
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
|
|
30
29
|
* globally for caching operations.
|
|
31
30
|
*
|
|
32
31
|
* @param {string} namespace - The namespace used for cache keys to avoid key collisions.
|
|
@@ -36,7 +35,7 @@ let CacheModule = CacheModule_1 = class CacheModule {
|
|
|
36
35
|
return {
|
|
37
36
|
global: true,
|
|
38
37
|
module: CacheModule_1,
|
|
39
|
-
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace })],
|
|
38
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace, ttl: 300000 })], // Default TTL = 5 minutes
|
|
40
39
|
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
41
40
|
exports: [cache_service_1.CacheService],
|
|
42
41
|
};
|
|
@@ -55,7 +54,7 @@ let CacheModule = CacheModule_1 = class CacheModule {
|
|
|
55
54
|
return {
|
|
56
55
|
global: false,
|
|
57
56
|
module: CacheModule_1,
|
|
58
|
-
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace })],
|
|
57
|
+
imports: [cache_manager_1.CacheModule.register({ isGlobal: true, namespace, ttl: 300000 })], // Default TTL = 5 minutes
|
|
59
58
|
providers: [cache_service_1.CacheService, { provide: 'CACHE_NAMESPACE', useValue: namespace }],
|
|
60
59
|
exports: [cache_service_1.CacheService],
|
|
61
60
|
};
|
|
@@ -6,9 +6,7 @@ import { Cache } from 'cache-manager';
|
|
|
6
6
|
*
|
|
7
7
|
* This service manages caching operations in a centralized manner, providing methods for
|
|
8
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
|
|
10
|
-
* and in-memory caching, using Redis for persistent storage and falling back to in-memory
|
|
11
|
-
* caching if Redis is unavailable.
|
|
9
|
+
* fallback function and asynchronously storing the result. The service supports Redis caching.
|
|
12
10
|
*/
|
|
13
11
|
export declare class CacheService implements OnModuleInit {
|
|
14
12
|
private readonly cache;
|
|
@@ -33,11 +31,11 @@ export declare class CacheService implements OnModuleInit {
|
|
|
33
31
|
*/
|
|
34
32
|
onModuleInit(): Promise<void>;
|
|
35
33
|
/**
|
|
36
|
-
* Sets up the cache
|
|
34
|
+
* Sets up the cache store, attempting to connect to Redis.
|
|
37
35
|
*
|
|
38
36
|
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
39
37
|
*/
|
|
40
|
-
private
|
|
38
|
+
private setupStore;
|
|
41
39
|
/**
|
|
42
40
|
* Fetches the value associated with the given key from the cache.
|
|
43
41
|
*
|
|
@@ -28,16 +28,13 @@ const redis_1 = require("@keyv/redis");
|
|
|
28
28
|
const cache_manager_1 = require("@nestjs/cache-manager");
|
|
29
29
|
const common_1 = require("@nestjs/common");
|
|
30
30
|
const config_1 = require("@nestjs/config");
|
|
31
|
-
const cacheable_1 = require("cacheable");
|
|
32
31
|
const env_1 = require("../../env");
|
|
33
32
|
/**
|
|
34
33
|
* CacheService
|
|
35
34
|
*
|
|
36
35
|
* This service manages caching operations in a centralized manner, providing methods for
|
|
37
36
|
* retrieving, storing, and deleting cached values. It handles cache misses by executing a
|
|
38
|
-
* fallback function and asynchronously storing the result. The service supports
|
|
39
|
-
* and in-memory caching, using Redis for persistent storage and falling back to in-memory
|
|
40
|
-
* caching if Redis is unavailable.
|
|
37
|
+
* fallback function and asynchronously storing the result. The service supports Redis caching.
|
|
41
38
|
*/
|
|
42
39
|
let CacheService = CacheService_1 = class CacheService {
|
|
43
40
|
/**
|
|
@@ -63,18 +60,16 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
63
60
|
*/
|
|
64
61
|
onModuleInit() {
|
|
65
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
66
|
-
yield this.
|
|
63
|
+
yield this.setupStore();
|
|
67
64
|
});
|
|
68
65
|
}
|
|
69
66
|
/**
|
|
70
|
-
* Sets up the cache
|
|
67
|
+
* Sets up the cache store, attempting to connect to Redis.
|
|
71
68
|
*
|
|
72
69
|
* @returns {Promise<void>} - Resolves when the cache stores are initialized.
|
|
73
70
|
*/
|
|
74
|
-
|
|
71
|
+
setupStore() {
|
|
75
72
|
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
73
|
try {
|
|
79
74
|
// Configure Redis store using environment variables
|
|
80
75
|
const host = this.configService.getOrThrow(env_1.ENV_REDIS_HOST);
|
|
@@ -88,20 +83,23 @@ let CacheService = CacheService_1 = class CacheService {
|
|
|
88
83
|
const { token: password, refreshAfterTimestamp } = accessToken;
|
|
89
84
|
this.logger.log(`New token received. Refresh at: ${new Date(refreshAfterTimestamp).toISOString()}`);
|
|
90
85
|
// 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 });
|
|
86
|
+
const redisStore = (0, redis_1.createKeyv)({ username: user, password, socket: { host, port, tls: true, timeout: 5000 }, pingInterval: 60000 }, { namespace: this.namespace });
|
|
92
87
|
// Check Redis server availability
|
|
93
88
|
yield redisStore.get('ping');
|
|
94
89
|
this.logger.log('Redis is available. Using Redis store for caching.');
|
|
95
90
|
const now = Date.now();
|
|
96
91
|
const refreshDelay = Math.max(refreshAfterTimestamp - now, 0); // Ensure non-negative delay
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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];
|
|
100
99
|
}
|
|
101
100
|
catch (error) {
|
|
102
|
-
|
|
103
|
-
this.
|
|
104
|
-
this.cache.stores = [memoryStore];
|
|
101
|
+
this.logger.error('Redis is unavailable: ' + error);
|
|
102
|
+
this.cache.stores = [];
|
|
105
103
|
}
|
|
106
104
|
});
|
|
107
105
|
}
|
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.5",
|
|
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",
|