@things-factory/cache-service 8.0.0-alpha.2 → 8.0.0-alpha.20
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-server/controllers/cache-service-database.d.ts +1 -0
- package/dist-server/controllers/cache-service-database.js +11 -0
- package/dist-server/controllers/cache-service-database.js.map +1 -1
- package/dist-server/controllers/cache-service-redis.d.ts +1 -0
- package/dist-server/controllers/cache-service-redis.js +12 -0
- package/dist-server/controllers/cache-service-redis.js.map +1 -1
- package/dist-server/controllers/cache-service.js +18 -0
- package/dist-server/controllers/cache-service.js.map +1 -1
- package/dist-server/controllers/types.d.ts +6 -0
- package/dist-server/controllers/types.js.map +1 -1
- package/dist-server/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/server/controllers/cache-service-database.ts +14 -0
- package/server/controllers/cache-service-redis.ts +15 -0
- package/server/controllers/cache-service.ts +21 -0
- package/server/controllers/types.ts +7 -0
|
@@ -30,4 +30,5 @@ export declare class CacheServiceDatabase implements ICacheService {
|
|
|
30
30
|
* @returns A promise that resolves when the entry is successfully stored.
|
|
31
31
|
*/
|
|
32
32
|
setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
|
|
33
|
+
clearStaleCache(): Promise<number>;
|
|
33
34
|
}
|
|
@@ -71,6 +71,17 @@ class CacheServiceDatabase {
|
|
|
71
71
|
await cacheRepository.save(cacheEntry);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
74
|
+
async clearStaleCache() {
|
|
75
|
+
const cacheRepository = (0, shell_1.getRepository)(cache_store_1.CacheStore);
|
|
76
|
+
const currentTime = new Date();
|
|
77
|
+
const staleEntries = await cacheRepository
|
|
78
|
+
.createQueryBuilder()
|
|
79
|
+
.delete()
|
|
80
|
+
.from(cache_store_1.CacheStore)
|
|
81
|
+
.where('wroteAt + ttl * 1000 < :currentTime', { currentTime: currentTime.getTime() })
|
|
82
|
+
.execute();
|
|
83
|
+
return staleEntries.affected || 0;
|
|
84
|
+
}
|
|
74
85
|
}
|
|
75
86
|
exports.CacheServiceDatabase = CacheServiceDatabase;
|
|
76
87
|
//# sourceMappingURL=cache-service-database.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-service-database.js","sourceRoot":"","sources":["../../server/controllers/cache-service-database.ts"],"names":[],"mappings":";;;;AAAA,iDAAqD;AACrD,oEAA+D;AAE/D,sEAAoC;AAEpC;;;GAGG;AACH,MAAa,oBAAoB;IAC/B;;;;;;OAMG;IACK,gBAAgB,CAAC,QAAgB;QACvC,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAA,qBAAU,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAU,EAAC,EAAE,CAAC,CAAA;IACzD,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,YAAY,CAAC,EAAU,EAAE,QAAgB;QAC7C,MAAM,eAAe,GAAG,IAAA,qBAAa,EAAC,wBAAU,CAAC,CAAA;QAEjD,MAAM,UAAU,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC;YAC/C,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,EAAE;YAChE,KAAK,EAAE,EAAE,OAAO,EAAE,MAAM,EAAE;SAC3B,CAAC,CAAA;QAEF,oDAAoD;QACpD,IAAI,UAAU,IAAI,UAAU,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC,GAAG,GAAG,IAAI,CAAC,GAAG,IAAI,IAAI,EAAE,EAAE,CAAC;YAChH,OAAO,UAAU,CAAA;QACnB,CAAC;QAED,OAAO,IAAI,CAAA;IACb,CAAC;IAED;;;;;;;;OAQG;IACH,KAAK,CAAC,UAAU,CAAC,EAAU,EAAE,QAAgB,EAAE,KAAU,EAAE,GAAW;QACpE,MAAM,GAAG,GAAG,IAAI,CAAC,gBAAgB,CAAC,QAAQ,CAAC,CAAA;QAC3C,MAAM,OAAO,GAAG,IAAI,IAAI,EAAE,CAAA;QAE1B,MAAM,eAAe,GAAG,IAAA,qBAAa,EAAC,wBAAU,CAAC,CAAA;QACjD,IAAI,UAAU,GAAG,MAAM,eAAe,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,EAAE,CAAC,CAAA;QAExF,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,2BAA2B;YAC3B,UAAU,GAAG,IAAI,wBAAU,EAAE,CAAA;YAC7B,UAAU,CAAC,OAAO,GAAG,EAAE,CAAA;YACvB,UAAU,CAAC,OAAO,GAAG,GAAG,CAAA;YACxB,UAAU,CAAC,KAAK,GAAG,KAAK,CAAA;YACxB,UAAU,CAAC,GAAG,GAAG,GAAG,CAAA;YACpB,UAAU,CAAC,OAAO,GAAG,OAAO,CAAA;YAC5B,MAAM,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,kCAAkC;YAClC,UAAU,CAAC,KAAK,GAAG,KAAK,CAAA;YACxB,UAAU,CAAC,GAAG,GAAG,GAAG,CAAA;YACpB,UAAU,CAAC,OAAO,GAAG,OAAO,CAAA;YAC5B,MAAM,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;CACF;
|
|
1
|
+
{"version":3,"file":"cache-service-database.js","sourceRoot":"","sources":["../../server/controllers/cache-service-database.ts"],"names":[],"mappings":";;;;AAAA,iDAAqD;AACrD,oEAA+D;AAE/D,sEAAoC;AAEpC;;;GAGG;AACH,MAAa,oBAAoB;IAC/B;;;;;;OAMG;IACK,gBAAgB,CAAC,QAAgB;QACvC,OAAO,QAAQ,CAAC,CAAC,CAAC,IAAA,qBAAU,EAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,IAAA,qBAAU,EAAC,EA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GAAG,GAAG,CAAA;YACxB,UAAU,CAAC,KAAK,GAAG,KAAK,CAAA;YACxB,UAAU,CAAC,GAAG,GAAG,GAAG,CAAA;YACpB,UAAU,CAAC,OAAO,GAAG,OAAO,CAAA;YAC5B,MAAM,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACxC,CAAC;aAAM,CAAC;YACN,kCAAkC;YAClC,UAAU,CAAC,KAAK,GAAG,KAAK,CAAA;YACxB,UAAU,CAAC,GAAG,GAAG,GAAG,CAAA;YACpB,UAAU,CAAC,OAAO,GAAG,OAAO,CAAA;YAC5B,MAAM,eAAe,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACxC,CAAC;IACH,CAAC;IAED,KAAK,CAAC,eAAe;QACnB,MAAM,eAAe,GAAG,IAAA,qBAAa,EAAC,wBAAU,CAAC,CAAA;QACjD,MAAM,WAAW,GAAG,IAAI,IAAI,EAAE,CAAA;QAE9B,MAAM,YAAY,GAAG,MAAM,eAAe;aACvC,kBAAkB,EAAE;aACpB,MAAM,EAAE;aACR,IAAI,CAAC,wBAAU,CAAC;aAChB,KAAK,CAAC,qCAAqC,EAAE,EAAE,WAAW,EAAE,WAAW,CAAC,OAAO,EAAE,EAAE,CAAC;aACpF,OAAO,EAAE,CAAA;QAEZ,OAAO,YAAY,CAAC,QAAQ,IAAI,CAAC,CAAA;IACnC,CAAC;CACF;AAlFD,oDAkFC","sourcesContent":["import { getRepository } from '@things-factory/shell'\nimport { CacheStore } from '../service/cache-store/cache-store'\nimport { ICacheService } from './types'\nimport objectHash from 'object-hash'\n\n/**\n * CacheService class implements ICacheService interface\n * It provides methods to interact with the cache.\n */\nexport class CacheServiceDatabase implements ICacheService {\n /**\n * Generates a unique cache key using the provided id and object parameters.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns The generated cache key.\n */\n private generateCacheKey(objParam: object): string {\n return objParam ? objectHash(objParam) : objectHash({})\n }\n\n /**\n * Retrieves an entry from the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns A promise that resolves to the cached entry or null if not found or expired.\n */\n async getFromCache(id: string, objParam: object): Promise<any> {\n const cacheRepository = getRepository(CacheStore)\n\n const cacheEntry = await cacheRepository.findOne({\n where: { cacheId: id, hashKey: this.generateCacheKey(objParam) },\n order: { wroteAt: 'DESC' }\n })\n\n // Check if the cache entry is found and still valid\n if (cacheEntry && cacheEntry.ttl && new Date(cacheEntry.wroteAt.getTime() + cacheEntry.ttl * 1000) > new Date()) {\n return cacheEntry\n }\n\n return null\n }\n\n /**\n * Stores an entry in the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @param value - The value to be cached.\n * @param ttl - Time to live in seconds. After this period, the cache entry expires.\n * @returns A promise that resolves when the entry is successfully stored.\n */\n async setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void> {\n const key = this.generateCacheKey(objParam)\n const wroteAt = new Date()\n\n const cacheRepository = getRepository(CacheStore)\n let cacheEntry = await cacheRepository.findOne({ where: { cacheId: id, hashKey: key } })\n\n if (!cacheEntry) {\n // Create a new cache entry\n cacheEntry = new CacheStore()\n cacheEntry.cacheId = id\n cacheEntry.hashKey = key\n cacheEntry.value = value\n cacheEntry.ttl = ttl\n cacheEntry.wroteAt = wroteAt\n await cacheRepository.save(cacheEntry)\n } else {\n // Update the existing cache entry\n cacheEntry.value = value\n cacheEntry.ttl = ttl\n cacheEntry.wroteAt = wroteAt\n await cacheRepository.save(cacheEntry)\n }\n }\n\n async clearStaleCache(): Promise<number> {\n const cacheRepository = getRepository(CacheStore)\n const currentTime = new Date()\n\n const staleEntries = await cacheRepository\n .createQueryBuilder()\n .delete()\n .from(CacheStore)\n .where('wroteAt + ttl * 1000 < :currentTime', { currentTime: currentTime.getTime() })\n .execute()\n\n return staleEntries.affected || 0\n }\n}\n"]}
|
|
@@ -31,4 +31,5 @@ export declare class CacheServiceRedis implements ICacheService {
|
|
|
31
31
|
* @returns A promise that resolves when the entry is successfully stored.
|
|
32
32
|
*/
|
|
33
33
|
setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
|
|
34
|
+
clearStaleCache(): Promise<number>;
|
|
34
35
|
}
|
|
@@ -60,6 +60,18 @@ class CacheServiceRedis {
|
|
|
60
60
|
await this.setAsync(key, cacheValue);
|
|
61
61
|
await this.expireAsync(key, ttl);
|
|
62
62
|
}
|
|
63
|
+
async clearStaleCache() {
|
|
64
|
+
const keys = await this.client.keysAsync('*');
|
|
65
|
+
let cleared = 0;
|
|
66
|
+
for (const key of keys) {
|
|
67
|
+
const ttl = await this.client.ttlAsync(key);
|
|
68
|
+
if (ttl < 0) {
|
|
69
|
+
await this.client.delAsync(key);
|
|
70
|
+
cleared++;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return cleared;
|
|
74
|
+
}
|
|
63
75
|
}
|
|
64
76
|
exports.CacheServiceRedis = CacheServiceRedis;
|
|
65
77
|
//# sourceMappingURL=cache-service-redis.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-service-redis.js","sourceRoot":"","sources":["../../server/controllers/cache-service-redis.ts"],"names":[],"mappings":";;;;AACA,sEAAoC;AACpC,iCAAoC;AACpC,+BAAgC;AAChC,6CAA4C;AAE5C,MAAM,WAAW,GAAG,YAAM,CAAC,GAAG,CAAC,OAAO,EAAE;IACtC,UAAU,EAAE,OAAO;IACnB,IAAI,EAAE,WAAW;IACjB,IAAI,EAAE,IAAI;CACX,CAAC,CAAA;AAEF,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,WAAW,IAAI,EAAE,CAAA;AAExC,MAAa,iBAAiB;IAM5B;QACE,IAAI,CAAC,MAAM,GAAG,IAAA,oBAAY,EAAC;YACzB,GAAG,EAAE,WAAW,IAAI,IAAI,IAAI,EAAE;SAC/B,CAAC,CAAA;QAEF,IAAI,CAAC,QAAQ,GAAG,IAAA,gB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AC/B,OAAO,EAAE,CAAA;YACX,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAA;IAChB,CAAC;CACF;AAzED,8CAyEC","sourcesContent":["import { ICacheService } from './types'\nimport objectHash from 'object-hash'\nimport { createClient } from 'redis'\nimport { promisify } from 'util'\nimport { config } from '@things-factory/env'\n\nconst cacheConfig = config.get('cache', {\n middleware: 'redis',\n host: 'localhost',\n port: 6379\n})\n\nconst { host, port } = cacheConfig || {}\n\nexport class CacheServiceRedis implements ICacheService {\n private client\n private getAsync: (key: string) => Promise<string | null>\n private setAsync: (key: string, value: string) => Promise<void>\n private expireAsync: (key: string, seconds: number) => Promise<void>\n\n constructor() {\n this.client = createClient({\n url: `redis://${host}:${port}`\n })\n\n this.getAsync = promisify(this.client.get).bind(this.client)\n this.setAsync = promisify(this.client.set).bind(this.client)\n this.expireAsync = promisify(this.client.expire).bind(this.client)\n\n this.client.connect().catch(console.error)\n }\n\n /**\n * Generates a unique cache key using the provided id and object parameters.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns The generated cache key.\n */\n private generateCacheKey(id: string, objParam: object): string {\n const hash = objectHash(objParam)\n return `${id}_${hash}`\n }\n\n /**\n * Retrieves an entry from the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns A promise that resolves to the cached entry or null if not found or expired.\n */\n async getFromCache(id: string, objParam: object): Promise<any> {\n const key = this.generateCacheKey(id, objParam)\n const cacheEntry = await this.getAsync(key)\n return cacheEntry ? JSON.parse(cacheEntry) : null\n }\n\n /**\n * Stores an entry in the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @param value - The value to be cached.\n * @param ttl - Time to live in seconds. After this period, the cache entry expires.\n * @returns A promise that resolves when the entry is successfully stored.\n */\n async setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void> {\n const key = this.generateCacheKey(id, objParam)\n const cacheValue = JSON.stringify(value)\n await this.setAsync(key, cacheValue)\n await this.expireAsync(key, ttl)\n }\n\n async clearStaleCache(): Promise<number> {\n const keys = await this.client.keysAsync('*')\n let cleared = 0\n\n for (const key of keys) {\n const ttl = await this.client.ttlAsync(key)\n if (ttl < 0) {\n await this.client.delAsync(key)\n cleared++\n }\n }\n\n return cleared\n }\n}\n"]}
|
|
@@ -8,4 +8,22 @@ const cacheConfig = env_1.config.get('cache', {
|
|
|
8
8
|
const { CacheServiceDatabase } = require('./cache-service-database');
|
|
9
9
|
const { CacheServiceRedis } = require('./cache-service-redis');
|
|
10
10
|
exports.cacheService = cacheConfig.middleware == 'redis' ? new CacheServiceRedis() : new CacheServiceDatabase();
|
|
11
|
+
setTimeout(async () => {
|
|
12
|
+
try {
|
|
13
|
+
const count = await exports.cacheService.clearStaleCache();
|
|
14
|
+
console.info('Successfully cleared: ', count, ' stale cache entries');
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
console.error('An error occurred while clearing stale cache:', err);
|
|
18
|
+
}
|
|
19
|
+
}, 60 * 1000);
|
|
20
|
+
setInterval(async () => {
|
|
21
|
+
try {
|
|
22
|
+
const count = await exports.cacheService.clearStaleCache();
|
|
23
|
+
console.info('Successfully cleared: ', count, ' stale cache entries');
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
console.error('An error occurred while clearing stale cache:', err);
|
|
27
|
+
}
|
|
28
|
+
}, 24 * 60 * 60 * 1000);
|
|
11
29
|
//# sourceMappingURL=cache-service.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-service.js","sourceRoot":"","sources":["../../server/controllers/cache-service.ts"],"names":[],"mappings":";;;AACA,6CAA4C;AAE5C,MAAM,WAAW,GAAG,YAAM,CAAC,GAAG,CAAC,OAAO,EAAE;IACtC,UAAU,EAAE,UAAU;CACvB,CAAC,CAAA;AAEF,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAA;AACpE,MAAM,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;AAEjD,QAAA,YAAY,GACvB,WAAW,CAAC,UAAU,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,iBAAiB,EAAE,CAAC,CAAC,CAAC,IAAI,oBAAoB,EAAE,CAAA","sourcesContent":["import { ICacheService } from './types'\nimport { config } from '@things-factory/env'\n\nconst cacheConfig = config.get('cache', {\n middleware: 'database'\n})\n\nconst { CacheServiceDatabase } = require('./cache-service-database')\nconst { CacheServiceRedis } = require('./cache-service-redis')\n\nexport const cacheService: ICacheService =\n cacheConfig.middleware == 'redis' ? new CacheServiceRedis() : new CacheServiceDatabase()\n"]}
|
|
1
|
+
{"version":3,"file":"cache-service.js","sourceRoot":"","sources":["../../server/controllers/cache-service.ts"],"names":[],"mappings":";;;AACA,6CAA4C;AAE5C,MAAM,WAAW,GAAG,YAAM,CAAC,GAAG,CAAC,OAAO,EAAE;IACtC,UAAU,EAAE,UAAU;CACvB,CAAC,CAAA;AAEF,MAAM,EAAE,oBAAoB,EAAE,GAAG,OAAO,CAAC,0BAA0B,CAAC,CAAA;AACpE,MAAM,EAAE,iBAAiB,EAAE,GAAG,OAAO,CAAC,uBAAuB,CAAC,CAAA;AAEjD,QAAA,YAAY,GACvB,WAAW,CAAC,UAAU,IAAI,OAAO,CAAC,CAAC,CAAC,IAAI,iBAAiB,EAAE,CAAC,CAAC,CAAC,IAAI,oBAAoB,EAAE,CAAA;AAE1F,UAAU,CAAC,KAAK,IAAI,EAAE;IACpB,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,oBAAY,CAAC,eAAe,EAAE,CAAA;QAClD,OAAO,CAAC,IAAI,CAAC,wBAAwB,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAA;IACvE,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,+CAA+C,EAAE,GAAG,CAAC,CAAA;IACrE,CAAC;AACH,CAAC,EAAE,EAAE,GAAG,IAAI,CAAC,CAAA;AAEb,WAAW,CACT,KAAK,IAAI,EAAE;IACT,IAAI,CAAC;QACH,MAAM,KAAK,GAAG,MAAM,oBAAY,CAAC,eAAe,EAAE,CAAA;QAClD,OAAO,CAAC,IAAI,CAAC,wBAAwB,EAAE,KAAK,EAAE,sBAAsB,CAAC,CAAA;IACvE,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO,CAAC,KAAK,CAAC,+CAA+C,EAAE,GAAG,CAAC,CAAA;IACrE,CAAC;AACH,CAAC,EACD,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CACpB,CAAA","sourcesContent":["import { ICacheService } from './types'\nimport { config } from '@things-factory/env'\n\nconst cacheConfig = config.get('cache', {\n middleware: 'database'\n})\n\nconst { CacheServiceDatabase } = require('./cache-service-database')\nconst { CacheServiceRedis } = require('./cache-service-redis')\n\nexport const cacheService: ICacheService =\n cacheConfig.middleware == 'redis' ? new CacheServiceRedis() : new CacheServiceDatabase()\n\nsetTimeout(async () => {\n try {\n const count = await cacheService.clearStaleCache()\n console.info('Successfully cleared: ', count, ' stale cache entries')\n } catch (err) {\n console.error('An error occurred while clearing stale cache:', err)\n }\n}, 60 * 1000)\n\nsetInterval(\n async () => {\n try {\n const count = await cacheService.clearStaleCache()\n console.info('Successfully cleared: ', count, ' stale cache entries')\n } catch (err) {\n console.error('An error occurred while clearing stale cache:', err)\n }\n },\n 24 * 60 * 60 * 1000\n)\n"]}
|
|
@@ -21,4 +21,10 @@ export interface ICacheService {
|
|
|
21
21
|
* @returns A promise that resolves when the value is successfully stored.
|
|
22
22
|
*/
|
|
23
23
|
setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Clears stale (expired) entries from the cache.
|
|
26
|
+
*
|
|
27
|
+
* @returns A promise that resolves to the number of cleared cache entries.
|
|
28
|
+
*/
|
|
29
|
+
clearStaleCache(): Promise<number>;
|
|
24
30
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../server/controllers/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Interface representing a cache service.\n * Provides methods to get and set values in a cache.\n */\nexport interface ICacheService {\n /**\n * Retrieves a value from the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns A promise that resolves to the cached value, or null if the key is not found.\n */\n getFromCache(id: string, objParam: object): Promise<any>\n\n /**\n * Stores a value in the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @param value - The value to be cached.\n * @param ttl - Time to live in seconds. After this period, the cache entry expires.\n * @returns A promise that resolves when the value is successfully stored.\n */\n setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>\n}\n"]}
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../server/controllers/types.ts"],"names":[],"mappings":"","sourcesContent":["/**\n * Interface representing a cache service.\n * Provides methods to get and set values in a cache.\n */\nexport interface ICacheService {\n /**\n * Retrieves a value from the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @returns A promise that resolves to the cached value, or null if the key is not found.\n */\n getFromCache(id: string, objParam: object): Promise<any>\n\n /**\n * Stores a value in the cache.\n *\n * @param id - The unique identifier for the cache entry.\n * @param objParam - The object parameters that are part of the cache key.\n * @param value - The value to be cached.\n * @param ttl - Time to live in seconds. After this period, the cache entry expires.\n * @returns A promise that resolves when the value is successfully stored.\n */\n setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>\n\n /**\n * Clears stale (expired) entries from the cache.\n *\n * @returns A promise that resolves to the number of cleared cache entries.\n */\n clearStaleCache(): Promise<number>\n}\n"]}
|