@things-factory/cache-service 7.0.0

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.
Files changed (50) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/assets/images/hatiolab-logo.png +0 -0
  3. package/config/config.development.js +17 -0
  4. package/config/config.production.js +17 -0
  5. package/dist-server/controllers/cache-service-database.d.ts +33 -0
  6. package/dist-server/controllers/cache-service-database.js +76 -0
  7. package/dist-server/controllers/cache-service-database.js.map +1 -0
  8. package/dist-server/controllers/cache-service-redis.d.ts +34 -0
  9. package/dist-server/controllers/cache-service-redis.js +65 -0
  10. package/dist-server/controllers/cache-service-redis.js.map +1 -0
  11. package/dist-server/controllers/cache-service.d.ts +2 -0
  12. package/dist-server/controllers/cache-service.js +11 -0
  13. package/dist-server/controllers/cache-service.js.map +1 -0
  14. package/dist-server/controllers/types.d.ts +24 -0
  15. package/dist-server/controllers/types.js +3 -0
  16. package/dist-server/controllers/types.js.map +1 -0
  17. package/dist-server/index.d.ts +2 -0
  18. package/dist-server/index.js +6 -0
  19. package/dist-server/index.js.map +1 -0
  20. package/dist-server/service/cache-store/cache-store.d.ts +8 -0
  21. package/dist-server/service/cache-store/cache-store.js +39 -0
  22. package/dist-server/service/cache-store/cache-store.js.map +1 -0
  23. package/dist-server/service/cache-store/index.d.ts +2 -0
  24. package/dist-server/service/cache-store/index.js +6 -0
  25. package/dist-server/service/cache-store/index.js.map +1 -0
  26. package/dist-server/service/index.d.ts +1 -0
  27. package/dist-server/service/index.js +6 -0
  28. package/dist-server/service/index.js.map +1 -0
  29. package/dist-server/tsconfig.tsbuildinfo +1 -0
  30. package/helps/cache-service/cache-service.ja.md +97 -0
  31. package/helps/cache-service/cache-service.ko.md +35 -0
  32. package/helps/cache-service/cache-service.md +97 -0
  33. package/helps/cache-service/cache-service.ms.md +99 -0
  34. package/helps/cache-service/cache-service.zh.md +97 -0
  35. package/helps/config/cache.ja.md +97 -0
  36. package/helps/config/cache.ko.md +97 -0
  37. package/helps/config/cache.md +97 -0
  38. package/helps/config/cache.ms.md +99 -0
  39. package/helps/config/cache.zh.md +97 -0
  40. package/package.json +34 -0
  41. package/server/controllers/cache-service-database.ts +78 -0
  42. package/server/controllers/cache-service-redis.ts +73 -0
  43. package/server/controllers/cache-service.ts +12 -0
  44. package/server/controllers/types.ts +25 -0
  45. package/server/index.ts +3 -0
  46. package/server/service/cache-store/cache-store.ts +25 -0
  47. package/server/service/cache-store/index.ts +3 -0
  48. package/server/service/index.ts +3 -0
  49. package/server/tsconfig.json +10 -0
  50. package/things-factory.config.js +1 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6
+ and adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7
+
8
+ <!-- ## [Unreleased] -->
Binary file
@@ -0,0 +1,17 @@
1
+ module.exports = {
2
+ /*
3
+ * cache configuration
4
+ * ie.
5
+ * cache: {
6
+ * middleware: 'redis', // database, redis
7
+ * host: 'redis',
8
+ * port: 6379,
9
+ * options: {
10
+ * ...
11
+ * }
12
+ * }
13
+ */
14
+ cache: {
15
+ middleware: 'database' /* 'database' | 'redis' */
16
+ }
17
+ }
@@ -0,0 +1,17 @@
1
+ module.exports = {
2
+ /*
3
+ * cache configuration
4
+ * ie.
5
+ * cache: {
6
+ * middleware: 'redis', // database, redis
7
+ * host: 'redis',
8
+ * port: 6379,
9
+ * options: {
10
+ * ...
11
+ * }
12
+ * }
13
+ */
14
+ cache: {
15
+ middleware: 'database' /* 'database' | 'redis' */
16
+ }
17
+ }
@@ -0,0 +1,33 @@
1
+ import { ICacheService } from './types';
2
+ /**
3
+ * CacheService class implements ICacheService interface
4
+ * It provides methods to interact with the cache.
5
+ */
6
+ export declare class CacheServiceDatabase implements ICacheService {
7
+ /**
8
+ * Generates a unique cache key using the provided id and object parameters.
9
+ *
10
+ * @param id - The unique identifier for the cache entry.
11
+ * @param objParam - The object parameters that are part of the cache key.
12
+ * @returns The generated cache key.
13
+ */
14
+ private generateCacheKey;
15
+ /**
16
+ * Retrieves an entry from the cache.
17
+ *
18
+ * @param id - The unique identifier for the cache entry.
19
+ * @param objParam - The object parameters that are part of the cache key.
20
+ * @returns A promise that resolves to the cached entry or null if not found or expired.
21
+ */
22
+ getFromCache(id: string, objParam: object): Promise<any>;
23
+ /**
24
+ * Stores an entry in the cache.
25
+ *
26
+ * @param id - The unique identifier for the cache entry.
27
+ * @param objParam - The object parameters that are part of the cache key.
28
+ * @param value - The value to be cached.
29
+ * @param ttl - Time to live in seconds. After this period, the cache entry expires.
30
+ * @returns A promise that resolves when the entry is successfully stored.
31
+ */
32
+ setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
33
+ }
@@ -0,0 +1,76 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheServiceDatabase = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const shell_1 = require("@things-factory/shell");
6
+ const cache_store_1 = require("../service/cache-store/cache-store");
7
+ const object_hash_1 = tslib_1.__importDefault(require("object-hash"));
8
+ /**
9
+ * CacheService class implements ICacheService interface
10
+ * It provides methods to interact with the cache.
11
+ */
12
+ class CacheServiceDatabase {
13
+ /**
14
+ * Generates a unique cache key using the provided id and object parameters.
15
+ *
16
+ * @param id - The unique identifier for the cache entry.
17
+ * @param objParam - The object parameters that are part of the cache key.
18
+ * @returns The generated cache key.
19
+ */
20
+ generateCacheKey(objParam) {
21
+ return objParam ? (0, object_hash_1.default)(objParam) : (0, object_hash_1.default)({});
22
+ }
23
+ /**
24
+ * Retrieves an entry from the cache.
25
+ *
26
+ * @param id - The unique identifier for the cache entry.
27
+ * @param objParam - The object parameters that are part of the cache key.
28
+ * @returns A promise that resolves to the cached entry or null if not found or expired.
29
+ */
30
+ async getFromCache(id, objParam) {
31
+ const cacheRepository = (0, shell_1.getRepository)(cache_store_1.CacheStore);
32
+ const cacheEntry = await cacheRepository.findOne({
33
+ where: { cacheId: id, hashKey: this.generateCacheKey(objParam) },
34
+ order: { wroteAt: 'DESC' }
35
+ });
36
+ // Check if the cache entry is found and still valid
37
+ if (cacheEntry && cacheEntry.ttl && new Date(cacheEntry.wroteAt.getTime() + cacheEntry.ttl * 1000) > new Date()) {
38
+ return cacheEntry;
39
+ }
40
+ return null;
41
+ }
42
+ /**
43
+ * Stores an entry in the cache.
44
+ *
45
+ * @param id - The unique identifier for the cache entry.
46
+ * @param objParam - The object parameters that are part of the cache key.
47
+ * @param value - The value to be cached.
48
+ * @param ttl - Time to live in seconds. After this period, the cache entry expires.
49
+ * @returns A promise that resolves when the entry is successfully stored.
50
+ */
51
+ async setInCache(id, objParam, value, ttl) {
52
+ const key = this.generateCacheKey(objParam);
53
+ const wroteAt = new Date();
54
+ const cacheRepository = (0, shell_1.getRepository)(cache_store_1.CacheStore);
55
+ let cacheEntry = await cacheRepository.findOne({ where: { cacheId: id, hashKey: key } });
56
+ if (!cacheEntry) {
57
+ // Create a new cache entry
58
+ cacheEntry = new cache_store_1.CacheStore();
59
+ cacheEntry.cacheId = id;
60
+ cacheEntry.hashKey = key;
61
+ cacheEntry.value = value;
62
+ cacheEntry.ttl = ttl;
63
+ cacheEntry.wroteAt = wroteAt;
64
+ await cacheRepository.save(cacheEntry);
65
+ }
66
+ else {
67
+ // Update the existing cache entry
68
+ cacheEntry.value = value;
69
+ cacheEntry.ttl = ttl;
70
+ cacheEntry.wroteAt = wroteAt;
71
+ await cacheRepository.save(cacheEntry);
72
+ }
73
+ }
74
+ }
75
+ exports.CacheServiceDatabase = CacheServiceDatabase;
76
+ //# sourceMappingURL=cache-service-database.js.map
@@ -0,0 +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;AAE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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;AApED,oDAoEC","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"]}
@@ -0,0 +1,34 @@
1
+ import { ICacheService } from './types';
2
+ export declare class CacheServiceRedis implements ICacheService {
3
+ private client;
4
+ private getAsync;
5
+ private setAsync;
6
+ private expireAsync;
7
+ constructor();
8
+ /**
9
+ * Generates a unique cache key using the provided id and object parameters.
10
+ *
11
+ * @param id - The unique identifier for the cache entry.
12
+ * @param objParam - The object parameters that are part of the cache key.
13
+ * @returns The generated cache key.
14
+ */
15
+ private generateCacheKey;
16
+ /**
17
+ * Retrieves an entry from the cache.
18
+ *
19
+ * @param id - The unique identifier for the cache entry.
20
+ * @param objParam - The object parameters that are part of the cache key.
21
+ * @returns A promise that resolves to the cached entry or null if not found or expired.
22
+ */
23
+ getFromCache(id: string, objParam: object): Promise<any>;
24
+ /**
25
+ * Stores an entry in the cache.
26
+ *
27
+ * @param id - The unique identifier for the cache entry.
28
+ * @param objParam - The object parameters that are part of the cache key.
29
+ * @param value - The value to be cached.
30
+ * @param ttl - Time to live in seconds. After this period, the cache entry expires.
31
+ * @returns A promise that resolves when the entry is successfully stored.
32
+ */
33
+ setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
34
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheServiceRedis = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const object_hash_1 = tslib_1.__importDefault(require("object-hash"));
6
+ const redis_1 = require("redis");
7
+ const util_1 = require("util");
8
+ const env_1 = require("@things-factory/env");
9
+ const cacheConfig = env_1.config.get('cache', {
10
+ middleware: 'redis',
11
+ host: 'localhost',
12
+ port: 6379
13
+ });
14
+ const { host, port } = cacheConfig || {};
15
+ class CacheServiceRedis {
16
+ constructor() {
17
+ this.client = (0, redis_1.createClient)({
18
+ url: `redis://${host}:${port}`
19
+ });
20
+ this.getAsync = (0, util_1.promisify)(this.client.get).bind(this.client);
21
+ this.setAsync = (0, util_1.promisify)(this.client.set).bind(this.client);
22
+ this.expireAsync = (0, util_1.promisify)(this.client.expire).bind(this.client);
23
+ this.client.connect().catch(console.error);
24
+ }
25
+ /**
26
+ * Generates a unique cache key using the provided id and object parameters.
27
+ *
28
+ * @param id - The unique identifier for the cache entry.
29
+ * @param objParam - The object parameters that are part of the cache key.
30
+ * @returns The generated cache key.
31
+ */
32
+ generateCacheKey(id, objParam) {
33
+ const hash = (0, object_hash_1.default)(objParam);
34
+ return `${id}_${hash}`;
35
+ }
36
+ /**
37
+ * Retrieves an entry from the cache.
38
+ *
39
+ * @param id - The unique identifier for the cache entry.
40
+ * @param objParam - The object parameters that are part of the cache key.
41
+ * @returns A promise that resolves to the cached entry or null if not found or expired.
42
+ */
43
+ async getFromCache(id, objParam) {
44
+ const key = this.generateCacheKey(id, objParam);
45
+ const cacheEntry = await this.getAsync(key);
46
+ return cacheEntry ? JSON.parse(cacheEntry) : null;
47
+ }
48
+ /**
49
+ * Stores an entry in the cache.
50
+ *
51
+ * @param id - The unique identifier for the cache entry.
52
+ * @param objParam - The object parameters that are part of the cache key.
53
+ * @param value - The value to be cached.
54
+ * @param ttl - Time to live in seconds. After this period, the cache entry expires.
55
+ * @returns A promise that resolves when the entry is successfully stored.
56
+ */
57
+ async setInCache(id, objParam, value, ttl) {
58
+ const key = this.generateCacheKey(id, objParam);
59
+ const cacheValue = JSON.stringify(value);
60
+ await this.setAsync(key, cacheValue);
61
+ await this.expireAsync(key, ttl);
62
+ }
63
+ }
64
+ exports.CacheServiceRedis = CacheServiceRedis;
65
+ //# sourceMappingURL=cache-service-redis.js.map
@@ -0,0 +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,gBAAS,EAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAA;QAC5D,IAAI,CAAC,QAAQ,GAAG,IAAA,gBAAS,EAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAA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-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"]}
@@ -0,0 +1,2 @@
1
+ import { ICacheService } from './types';
2
+ export declare const cacheService: ICacheService;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.cacheService = void 0;
4
+ const env_1 = require("@things-factory/env");
5
+ const cacheConfig = env_1.config.get('cache', {
6
+ middleware: 'database'
7
+ });
8
+ const { CacheServiceDatabase } = require('./cache-service-database');
9
+ const { CacheServiceRedis } = require('./cache-service-redis');
10
+ exports.cacheService = cacheConfig.middleware == 'redis' ? new CacheServiceRedis() : new CacheServiceDatabase();
11
+ //# sourceMappingURL=cache-service.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Interface representing a cache service.
3
+ * Provides methods to get and set values in a cache.
4
+ */
5
+ export interface ICacheService {
6
+ /**
7
+ * Retrieves a value from the cache.
8
+ *
9
+ * @param id - The unique identifier for the cache entry.
10
+ * @param objParam - The object parameters that are part of the cache key.
11
+ * @returns A promise that resolves to the cached value, or null if the key is not found.
12
+ */
13
+ getFromCache(id: string, objParam: object): Promise<any>;
14
+ /**
15
+ * Stores a value in the cache.
16
+ *
17
+ * @param id - The unique identifier for the cache entry.
18
+ * @param objParam - The object parameters that are part of the cache key.
19
+ * @param value - The value to be cached.
20
+ * @param ttl - Time to live in seconds. After this period, the cache entry expires.
21
+ * @returns A promise that resolves when the value is successfully stored.
22
+ */
23
+ setInCache(id: string, objParam: object, value: any, ttl: number): Promise<void>;
24
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=types.js.map
@@ -0,0 +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"]}
@@ -0,0 +1,2 @@
1
+ export * from './service';
2
+ export * from './controllers/cache-service';
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const tslib_1 = require("tslib");
4
+ tslib_1.__exportStar(require("./service"), exports);
5
+ tslib_1.__exportStar(require("./controllers/cache-service"), exports);
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../server/index.ts"],"names":[],"mappings":";;;AAAA,oDAAyB;AAEzB,sEAA2C","sourcesContent":["export * from './service'\n\nexport * from './controllers/cache-service'\n"]}
@@ -0,0 +1,8 @@
1
+ export declare class CacheStore {
2
+ readonly id: string;
3
+ cacheId?: string;
4
+ hashKey?: string;
5
+ value?: any;
6
+ ttl?: number;
7
+ wroteAt?: Date;
8
+ }
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CacheStore = void 0;
4
+ const tslib_1 = require("tslib");
5
+ const typeorm_1 = require("typeorm");
6
+ let CacheStore = class CacheStore {
7
+ };
8
+ exports.CacheStore = CacheStore;
9
+ tslib_1.__decorate([
10
+ (0, typeorm_1.PrimaryGeneratedColumn)('uuid'),
11
+ tslib_1.__metadata("design:type", String)
12
+ ], CacheStore.prototype, "id", void 0);
13
+ tslib_1.__decorate([
14
+ (0, typeorm_1.Column)(),
15
+ tslib_1.__metadata("design:type", String)
16
+ ], CacheStore.prototype, "cacheId", void 0);
17
+ tslib_1.__decorate([
18
+ (0, typeorm_1.Column)(),
19
+ tslib_1.__metadata("design:type", String)
20
+ ], CacheStore.prototype, "hashKey", void 0);
21
+ tslib_1.__decorate([
22
+ (0, typeorm_1.Column)('simple-json', { nullable: true, default: null }),
23
+ tslib_1.__metadata("design:type", Object)
24
+ ], CacheStore.prototype, "value", void 0);
25
+ tslib_1.__decorate([
26
+ (0, typeorm_1.Column)({ nullable: true, default: null }),
27
+ tslib_1.__metadata("design:type", Number)
28
+ ], CacheStore.prototype, "ttl", void 0);
29
+ tslib_1.__decorate([
30
+ (0, typeorm_1.Column)({ nullable: true }),
31
+ tslib_1.__metadata("design:type", Date)
32
+ ], CacheStore.prototype, "wroteAt", void 0);
33
+ exports.CacheStore = CacheStore = tslib_1.__decorate([
34
+ (0, typeorm_1.Entity)(),
35
+ (0, typeorm_1.Index)('ix_cache_store_0', (cache) => [cache.cacheId, cache.hashKey], {
36
+ unique: true
37
+ })
38
+ ], CacheStore);
39
+ //# sourceMappingURL=cache-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cache-store.js","sourceRoot":"","sources":["../../../server/service/cache-store/cache-store.ts"],"names":[],"mappings":";;;;AAAA,qCAAuE;AAMhE,IAAM,UAAU,GAAhB,MAAM,UAAU;CAkBtB,CAAA;AAlBY,gCAAU;AAEZ;IADR,IAAA,gCAAsB,EAAC,MAAM,CAAC;;sCACZ;AAGnB;IADC,IAAA,gBAAM,GAAE;;2CACO;AAGhB;IADC,IAAA,gBAAM,GAAE;;2CACO;AAGhB;IADC,IAAA,gBAAM,EAAC,aAAa,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;yCAC9C;AAGX;IADC,IAAA,gBAAM,EAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,CAAC;;uCAC9B;AAGZ;IADC,IAAA,gBAAM,EAAC,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC;sCACjB,IAAI;2CAAA;qBAjBH,UAAU;IAJtB,IAAA,gBAAM,GAAE;IACR,IAAA,eAAK,EAAC,kBAAkB,EAAE,CAAC,KAAiB,EAAE,EAAE,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EAAE;QAChF,MAAM,EAAE,IAAI;KACb,CAAC;GACW,UAAU,CAkBtB","sourcesContent":["import { Entity, Index, Column, PrimaryGeneratedColumn } from 'typeorm'\n\n@Entity()\n@Index('ix_cache_store_0', (cache: CacheStore) => [cache.cacheId, cache.hashKey], {\n unique: true\n})\nexport class CacheStore {\n @PrimaryGeneratedColumn('uuid')\n readonly id: string\n\n @Column()\n cacheId?: string\n\n @Column()\n hashKey?: string\n\n @Column('simple-json', { nullable: true, default: null })\n value?: any\n\n @Column({ nullable: true, default: null })\n ttl?: number\n\n @Column({ nullable: true })\n wroteAt?: Date\n}\n"]}
@@ -0,0 +1,2 @@
1
+ import { CacheStore } from './cache-store';
2
+ export declare const entities: (typeof CacheStore)[];
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.entities = void 0;
4
+ const cache_store_1 = require("./cache-store");
5
+ exports.entities = [cache_store_1.CacheStore];
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../server/service/cache-store/index.ts"],"names":[],"mappings":";;;AAAA,+CAA0C;AAE7B,QAAA,QAAQ,GAAG,CAAC,wBAAU,CAAC,CAAA","sourcesContent":["import { CacheStore } from './cache-store'\n\nexport const entities = [CacheStore]\n"]}
@@ -0,0 +1 @@
1
+ export declare const entities: typeof import("./cache-store/cache-store").CacheStore[];
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.entities = void 0;
4
+ const cache_store_1 = require("./cache-store");
5
+ exports.entities = [...cache_store_1.entities];
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../server/service/index.ts"],"names":[],"mappings":";;;AAAA,+CAA8D;AAEjD,QAAA,QAAQ,GAAG,CAAC,GAAG,sBAAkB,CAAC,CAAA","sourcesContent":["import { entities as CacheStoreEntities } from './cache-store'\n\nexport const entities = [...CacheStoreEntities]\n"]}