@tmlmobilidade/go-interfaces-cachedb 20260911.937.57 → 20260911.1325.23

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.
@@ -1,5 +1,6 @@
1
1
  import { type RedisClientType } from '@tmlmobilidade/go-clients-redis';
2
2
  import { type CacheDbKey } from './keys.js';
3
+ import { CachedData } from './types.js';
3
4
  declare class CacheDbClass {
4
5
  private static _instance;
5
6
  private client;
@@ -35,6 +36,14 @@ declare class CacheDbClass {
35
36
  * @throws Will throw an error if the retrieval process fails.
36
37
  */
37
38
  get(key: CacheDbKey): Promise<null | string>;
39
+ /**
40
+ * Retrieves a cache entry by its key.
41
+ * @param key The key of the cache entry to retrieve.
42
+ * @returns A promise that resolves with the cache entry value,
43
+ * or `null` if not found.
44
+ * @throws Will throw an error if the retrieval process fails.
45
+ */
46
+ getNew<T>(key: CacheDbKey): Promise<CachedData<T> | null>;
38
47
  /**
39
48
  * Scans cache keys by pattern.
40
49
  * @param pattern The redis pattern to match.
@@ -48,6 +57,13 @@ declare class CacheDbClass {
48
57
  * @param ttl Optional time-to-live (TTL) in seconds. Omit when not needed.
49
58
  */
50
59
  set(key: CacheDbKey, value: string, ttl?: number): Promise<void>;
60
+ /**
61
+ * Saves a cache entry with an optional time-to-live (TTL).
62
+ * @param key The key of the cache entry to save.
63
+ * @param value The value of the cache entry to save. Must be a string.
64
+ * @param ttl Optional time-to-live (TTL) in seconds. Omit when not needed.
65
+ */
66
+ setNew<T>(key: CacheDbKey, value: T, ttl?: number): Promise<void>;
51
67
  protected connectToClient(): Promise<RedisClientType>;
52
68
  /**
53
69
  * Initializes the Redis client.
package/dist/interface.js CHANGED
@@ -1,5 +1,6 @@
1
1
  /* * */
2
2
  import { RedisDatabaseClient } from '@tmlmobilidade/go-clients-redis';
3
+ import { Dates } from '@tmlmobilidade/go-utils-dates';
3
4
  import { asyncSingletonProxy } from '@tmlmobilidade/utils';
4
5
  /* * */
5
6
  class CacheDbClass {
@@ -70,6 +71,19 @@ class CacheDbClass {
70
71
  return null;
71
72
  return result;
72
73
  }
74
+ /**
75
+ * Retrieves a cache entry by its key.
76
+ * @param key The key of the cache entry to retrieve.
77
+ * @returns A promise that resolves with the cache entry value,
78
+ * or `null` if not found.
79
+ * @throws Will throw an error if the retrieval process fails.
80
+ */
81
+ async getNew(key) {
82
+ const result = await this.client.get(key);
83
+ if (typeof result !== 'string')
84
+ return null;
85
+ return JSON.parse(result);
86
+ }
73
87
  /**
74
88
  * Scans cache keys by pattern.
75
89
  * @param pattern The redis pattern to match.
@@ -101,6 +115,24 @@ class CacheDbClass {
101
115
  else
102
116
  await this.client.set(key, value);
103
117
  }
118
+ /**
119
+ * Saves a cache entry with an optional time-to-live (TTL).
120
+ * @param key The key of the cache entry to save.
121
+ * @param value The value of the cache entry to save. Must be a string.
122
+ * @param ttl Optional time-to-live (TTL) in seconds. Omit when not needed.
123
+ */
124
+ async setNew(key, value, ttl) {
125
+ // Add the value to the CachedData object
126
+ const cachedData = {
127
+ data: value,
128
+ timestamp: Dates.now('utc').unix_milliseconds,
129
+ };
130
+ // Set cache with optional TTL
131
+ if (ttl)
132
+ await this.client.set(key, JSON.stringify(cachedData), { expiration: { type: 'EX', value: ttl } });
133
+ else
134
+ await this.client.set(key, JSON.stringify(cachedData));
135
+ }
104
136
  connectToClient() {
105
137
  return RedisDatabaseClient.getClient({ prefix: 'CACHEDB' });
106
138
  }
@@ -0,0 +1,5 @@
1
+ import { type UnixMilliseconds } from '@tmlmobilidade/go-types-shared';
2
+ export interface CachedData<T> {
3
+ data: T;
4
+ timestamp: UnixMilliseconds;
5
+ }
package/dist/types.js ADDED
@@ -0,0 +1,2 @@
1
+ /* * */
2
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmlmobilidade/go-interfaces-cachedb",
3
- "version": "20260911.937.57",
3
+ "version": "20260911.1325.23",
4
4
  "author": {
5
5
  "email": "iso@tmlmobilidade.pt",
6
6
  "name": "TML-ISO"
@@ -37,6 +37,8 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@tmlmobilidade/go-clients-redis": "*",
40
+ "@tmlmobilidade/go-types-shared": "*",
41
+ "@tmlmobilidade/go-utils-dates": "*",
40
42
  "@tmlmobilidade/utils": "*"
41
43
  },
42
44
  "devDependencies": {