@vibe-flats/booking-engine-common-server 1.0.117 → 1.0.118
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.
|
@@ -26,20 +26,15 @@
|
|
|
26
26
|
* - MONGO_URL: MongoDB connection string base
|
|
27
27
|
* - MONGO_URL_PARAMS: Additional connection parameters (optional)
|
|
28
28
|
*
|
|
29
|
-
*
|
|
30
29
|
* When using this, need to stop the usage of gReq and pass the req as a param so it passes to withCache fetchFn
|
|
31
30
|
*/
|
|
32
|
-
interface CacheConfig {
|
|
33
|
-
defaultTTL: number;
|
|
34
|
-
keyPrefix: string;
|
|
35
|
-
mongoUri?: string;
|
|
36
|
-
}
|
|
37
31
|
declare class CacheManager {
|
|
38
32
|
private cache;
|
|
39
|
-
private config;
|
|
40
33
|
private isEnabled;
|
|
41
34
|
private collection;
|
|
42
|
-
|
|
35
|
+
private defaultTTL;
|
|
36
|
+
private keyPrefix;
|
|
37
|
+
constructor();
|
|
43
38
|
private initializeCache;
|
|
44
39
|
get<T>(key: string): Promise<T | null>;
|
|
45
40
|
set<T>(key: string, value: T, ttl?: number): Promise<boolean>;
|
package/build/src/utils/cache.js
CHANGED
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
* - MONGO_URL: MongoDB connection string base
|
|
28
28
|
* - MONGO_URL_PARAMS: Additional connection parameters (optional)
|
|
29
29
|
*
|
|
30
|
-
*
|
|
31
30
|
* When using this, need to stop the usage of gReq and pass the req as a param so it passes to withCache fetchFn
|
|
32
31
|
*/
|
|
33
32
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
@@ -47,17 +46,18 @@ exports.withVibeMongoCache = exports.vibeMongoCacheManager = void 0;
|
|
|
47
46
|
const keyv_1 = __importDefault(require("keyv"));
|
|
48
47
|
const mongo_1 = __importDefault(require("@keyv/mongo"));
|
|
49
48
|
class CacheManager {
|
|
50
|
-
constructor(
|
|
49
|
+
constructor() {
|
|
51
50
|
this.cache = null;
|
|
52
51
|
this.isEnabled = false;
|
|
53
52
|
this.collection = 'cache';
|
|
54
|
-
this.
|
|
53
|
+
this.defaultTTL = 600000; // 10 minutes in milliseconds
|
|
54
|
+
this.keyPrefix = `${process.env.NODE_ENV || 'development'}-${process.env.npm_package_name || 'no-process.env.npm_package_name'}`;
|
|
55
55
|
this.initializeCache();
|
|
56
56
|
}
|
|
57
57
|
initializeCache() {
|
|
58
58
|
try {
|
|
59
59
|
// Use provided MongoDB URI or build from environment variables
|
|
60
|
-
const mongoUri =
|
|
60
|
+
const mongoUri = `${process.env.MONGO_URL}${this.collection}${process.env.MONGO_URL_PARAMS || ''}`;
|
|
61
61
|
if (!mongoUri || !process.env.MONGO_URL) {
|
|
62
62
|
console.warn('MongoDB URL not provided. Cache will be disabled.');
|
|
63
63
|
return;
|
|
@@ -68,8 +68,8 @@ class CacheManager {
|
|
|
68
68
|
});
|
|
69
69
|
this.cache = new keyv_1.default({
|
|
70
70
|
store,
|
|
71
|
-
ttl: this.
|
|
72
|
-
namespace: this.
|
|
71
|
+
ttl: this.defaultTTL,
|
|
72
|
+
namespace: this.keyPrefix
|
|
73
73
|
});
|
|
74
74
|
// Handle connection events
|
|
75
75
|
this.cache.on('error', error => {
|
|
@@ -105,7 +105,7 @@ class CacheManager {
|
|
|
105
105
|
return false;
|
|
106
106
|
}
|
|
107
107
|
try {
|
|
108
|
-
const timeToLive = ttl ? ttl * 1000 : this.
|
|
108
|
+
const timeToLive = ttl ? ttl * 1000 : this.defaultTTL; // Convert seconds to milliseconds
|
|
109
109
|
yield this.cache.set(key, value, timeToLive);
|
|
110
110
|
return true;
|
|
111
111
|
}
|
|
@@ -139,7 +139,7 @@ class CacheManager {
|
|
|
139
139
|
// Keyv's clear() method respects the namespace/prefix
|
|
140
140
|
// So this will only clear items with our prefix
|
|
141
141
|
yield this.cache.clear();
|
|
142
|
-
console.log(`Cleared all cache entries with prefix: ${this.
|
|
142
|
+
console.log(`Cleared all cache entries with prefix: ${this.keyPrefix}`);
|
|
143
143
|
return true;
|
|
144
144
|
}
|
|
145
145
|
catch (error) {
|
|
@@ -150,10 +150,7 @@ class CacheManager {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
// Create a singleton cache manager instance
|
|
153
|
-
exports.vibeMongoCacheManager = new CacheManager(
|
|
154
|
-
defaultTTL: 600000, // 10 minutes in milliseconds
|
|
155
|
-
keyPrefix: process.env.npm_package_name || 'no-process.env.npm_package_name'
|
|
156
|
-
});
|
|
153
|
+
exports.vibeMongoCacheManager = new CacheManager();
|
|
157
154
|
// Helper function for caching with fallback (exactly like your example)
|
|
158
155
|
const withVibeMongoCache = (key_1, fetchFn_1, ...args_1) => __awaiter(void 0, [key_1, fetchFn_1, ...args_1], void 0, function* (key, fetchFn, ttl = 600) {
|
|
159
156
|
let result = yield exports.vibeMongoCacheManager.get(key);
|