@koloseum/utils 0.2.16 → 0.2.17
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/utils.d.ts +40 -0
- package/dist/utils.js +75 -0
- package/package.json +1 -1
package/dist/utils.d.ts
CHANGED
|
@@ -289,3 +289,43 @@ export declare const Utility: {
|
|
|
289
289
|
*/
|
|
290
290
|
validateSocialMediaHandle: (handle: string) => boolean;
|
|
291
291
|
};
|
|
292
|
+
export declare const Cache: {
|
|
293
|
+
/**
|
|
294
|
+
* Delete cached data from Valkey.
|
|
295
|
+
* @param valkey - The Valkey client instance
|
|
296
|
+
* @param cachePrefix - The cache prefix
|
|
297
|
+
* @param key - The cache key
|
|
298
|
+
*/
|
|
299
|
+
deleteData: (valkey: any, cachePrefix: string, key: string) => Promise<void>;
|
|
300
|
+
/**
|
|
301
|
+
* Generate cache key with consistent formatting
|
|
302
|
+
* @param prefix - The key prefix
|
|
303
|
+
* @param params - Additional parameters to include in the key
|
|
304
|
+
* @returns The formatted cache key
|
|
305
|
+
*/
|
|
306
|
+
generateDataKey: (prefix: string, ...params: (string | number)[]) => string;
|
|
307
|
+
/**
|
|
308
|
+
* Get cached data from Valkey.
|
|
309
|
+
* @param valkey - The Valkey client instance
|
|
310
|
+
* @param cachePrefix - The cache prefix
|
|
311
|
+
* @param key - The cache key
|
|
312
|
+
* @returns The cached data, or `null` if not found
|
|
313
|
+
*/
|
|
314
|
+
getData: <T>(valkey: any, cachePrefix: string, key: string) => Promise<T | null>;
|
|
315
|
+
/**
|
|
316
|
+
* Invalidate cache by pattern
|
|
317
|
+
* @param valkey - The Valkey client instance
|
|
318
|
+
* @param cachePrefix - The cache prefix
|
|
319
|
+
* @param pattern - The pattern to match keys against (e.g., "app-config*")
|
|
320
|
+
*/
|
|
321
|
+
invalidateDataByPattern: (valkey: any, cachePrefix: string, pattern: string) => Promise<void>;
|
|
322
|
+
/**
|
|
323
|
+
* Set data in Valkey cache.
|
|
324
|
+
* @param valkey - The Valkey client instance
|
|
325
|
+
* @param cachePrefix - The cache prefix
|
|
326
|
+
* @param key - The cache key
|
|
327
|
+
* @param data - The data to cache
|
|
328
|
+
* @param ttl - The time to live in seconds; defaults to 1 hour
|
|
329
|
+
*/
|
|
330
|
+
setData: <T>(valkey: any, cachePrefix: string, key: string, data: T, ttl?: number) => Promise<void>;
|
|
331
|
+
};
|
package/dist/utils.js
CHANGED
|
@@ -6,6 +6,8 @@ import validator from "validator";
|
|
|
6
6
|
const parsePgInterval = (await import("postgres-interval")).default;
|
|
7
7
|
const sanitize = (await import("sanitize-html")).default;
|
|
8
8
|
const { trim, escape, isMobilePhone, isURL } = validator;
|
|
9
|
+
// Cache constants
|
|
10
|
+
const DEFAULT_TTL = 3600; // 1 hour in seconds
|
|
9
11
|
const { KenyaAdministrativeDivisions } = (await import("kenya-administrative-divisions")).default;
|
|
10
12
|
/* DUMMY DATA */
|
|
11
13
|
export const Data = {
|
|
@@ -1507,3 +1509,76 @@ export const Utility = {
|
|
|
1507
1509
|
!handle.startsWith("@") &&
|
|
1508
1510
|
Boolean(handle.match(Utility.socialMediaHandleRegex))
|
|
1509
1511
|
};
|
|
1512
|
+
/* CACHE FUNCTIONS */
|
|
1513
|
+
export const Cache = {
|
|
1514
|
+
/**
|
|
1515
|
+
* Delete cached data from Valkey.
|
|
1516
|
+
* @param valkey - The Valkey client instance
|
|
1517
|
+
* @param cachePrefix - The cache prefix
|
|
1518
|
+
* @param key - The cache key
|
|
1519
|
+
*/
|
|
1520
|
+
deleteData: async (valkey, cachePrefix, key) => {
|
|
1521
|
+
try {
|
|
1522
|
+
await valkey.del(`${cachePrefix}${key}`);
|
|
1523
|
+
}
|
|
1524
|
+
catch (error) {
|
|
1525
|
+
console.error("Cache delete error:", error);
|
|
1526
|
+
}
|
|
1527
|
+
},
|
|
1528
|
+
/**
|
|
1529
|
+
* Generate cache key with consistent formatting
|
|
1530
|
+
* @param prefix - The key prefix
|
|
1531
|
+
* @param params - Additional parameters to include in the key
|
|
1532
|
+
* @returns The formatted cache key
|
|
1533
|
+
*/
|
|
1534
|
+
generateDataKey: (prefix, ...params) => `${prefix}:${params.join(":")}`,
|
|
1535
|
+
/**
|
|
1536
|
+
* Get cached data from Valkey.
|
|
1537
|
+
* @param valkey - The Valkey client instance
|
|
1538
|
+
* @param cachePrefix - The cache prefix
|
|
1539
|
+
* @param key - The cache key
|
|
1540
|
+
* @returns The cached data, or `null` if not found
|
|
1541
|
+
*/
|
|
1542
|
+
getData: async (valkey, cachePrefix, key) => {
|
|
1543
|
+
try {
|
|
1544
|
+
const cached = await valkey.get(`${cachePrefix}${key}`);
|
|
1545
|
+
return cached ? JSON.parse(cached) : null;
|
|
1546
|
+
}
|
|
1547
|
+
catch (error) {
|
|
1548
|
+
console.error("Cache get error:", error);
|
|
1549
|
+
return null;
|
|
1550
|
+
}
|
|
1551
|
+
},
|
|
1552
|
+
/**
|
|
1553
|
+
* Invalidate cache by pattern
|
|
1554
|
+
* @param valkey - The Valkey client instance
|
|
1555
|
+
* @param cachePrefix - The cache prefix
|
|
1556
|
+
* @param pattern - The pattern to match keys against (e.g., "app-config*")
|
|
1557
|
+
*/
|
|
1558
|
+
invalidateDataByPattern: async (valkey, cachePrefix, pattern) => {
|
|
1559
|
+
try {
|
|
1560
|
+
const keys = await valkey.keys(`${cachePrefix}${pattern}`);
|
|
1561
|
+
if (keys.length > 0)
|
|
1562
|
+
await valkey.del(...keys);
|
|
1563
|
+
}
|
|
1564
|
+
catch (error) {
|
|
1565
|
+
console.error("Cache pattern invalidation error:", error);
|
|
1566
|
+
}
|
|
1567
|
+
},
|
|
1568
|
+
/**
|
|
1569
|
+
* Set data in Valkey cache.
|
|
1570
|
+
* @param valkey - The Valkey client instance
|
|
1571
|
+
* @param cachePrefix - The cache prefix
|
|
1572
|
+
* @param key - The cache key
|
|
1573
|
+
* @param data - The data to cache
|
|
1574
|
+
* @param ttl - The time to live in seconds; defaults to 1 hour
|
|
1575
|
+
*/
|
|
1576
|
+
setData: async (valkey, cachePrefix, key, data, ttl = DEFAULT_TTL) => {
|
|
1577
|
+
try {
|
|
1578
|
+
await valkey.setex(`${cachePrefix}${key}`, ttl, JSON.stringify(data));
|
|
1579
|
+
}
|
|
1580
|
+
catch (error) {
|
|
1581
|
+
console.error("Cache set error:", error);
|
|
1582
|
+
}
|
|
1583
|
+
}
|
|
1584
|
+
};
|