@vibe-flats/booking-engine-common-server 1.0.116 → 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.
- package/build/src/utils/cache.d.ts +6 -11
- package/build/src/utils/cache.js +15 -18
- package/package.json +1 -1
|
@@ -26,26 +26,21 @@
|
|
|
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>;
|
|
46
41
|
del(key: string): Promise<boolean>;
|
|
47
42
|
clear(): Promise<boolean>;
|
|
48
43
|
}
|
|
49
|
-
export declare const
|
|
50
|
-
export declare const
|
|
51
|
-
export default
|
|
44
|
+
export declare const vibeMongoCacheManager: CacheManager;
|
|
45
|
+
export declare const withVibeMongoCache: <T>(key: string, fetchFn: () => Promise<T>, ttl?: number) => Promise<T>;
|
|
46
|
+
export default vibeMongoCacheManager;
|
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) {
|
|
@@ -43,21 +42,22 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
43
42
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
44
43
|
};
|
|
45
44
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
46
|
-
exports.
|
|
45
|
+
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,23 +150,20 @@ class CacheManager {
|
|
|
150
150
|
}
|
|
151
151
|
}
|
|
152
152
|
// Create a singleton cache manager instance
|
|
153
|
-
exports.
|
|
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
|
-
const
|
|
159
|
-
let result = yield exports.
|
|
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) {
|
|
156
|
+
let result = yield exports.vibeMongoCacheManager.get(key);
|
|
160
157
|
if (result) {
|
|
161
158
|
console.log(`Cache hit for key: ${key}`);
|
|
162
159
|
return result;
|
|
163
160
|
}
|
|
164
161
|
console.log(`Cache miss for key: ${key}`);
|
|
165
162
|
result = yield fetchFn();
|
|
166
|
-
yield exports.
|
|
163
|
+
yield exports.vibeMongoCacheManager.set(key, result, ttl);
|
|
167
164
|
console.log(`Cached data for key: ${key}`);
|
|
168
165
|
return result;
|
|
169
166
|
});
|
|
170
|
-
exports.
|
|
167
|
+
exports.withVibeMongoCache = withVibeMongoCache;
|
|
171
168
|
// Alternative export for convenience
|
|
172
|
-
exports.default = exports.
|
|
169
|
+
exports.default = exports.vibeMongoCacheManager;
|