dt-common-device 3.1.5 → 3.1.6
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/redis.utils.d.ts +39 -0
- package/dist/utils/redis.utils.js +49 -20
- package/package.json +1 -1
|
@@ -1,8 +1,47 @@
|
|
|
1
1
|
export declare class RedisUtils {
|
|
2
2
|
private readonly client;
|
|
3
|
+
/**
|
|
4
|
+
* Get a value from Redis
|
|
5
|
+
* @param key - The key to get
|
|
6
|
+
* @param field - The field to get
|
|
7
|
+
* @returns The value
|
|
8
|
+
*/
|
|
3
9
|
hget(key: string, field: string): Promise<any>;
|
|
10
|
+
/**
|
|
11
|
+
* Set a value in Redis with a TTL
|
|
12
|
+
* @param key - The key to set
|
|
13
|
+
* @param field - The field to set
|
|
14
|
+
* @param value - The value to set (JSON stringified)
|
|
15
|
+
* @param ttl - The TTL in seconds
|
|
16
|
+
* @returns The number of fields set
|
|
17
|
+
*/
|
|
18
|
+
hsetWithTTL(key: string, field: string, value: string, ttl: number): Promise<number>;
|
|
19
|
+
/**
|
|
20
|
+
* Set a value in Redis
|
|
21
|
+
* @param key - The key to set
|
|
22
|
+
* @param field - The field to set
|
|
23
|
+
* @param value - The value to set (JSON stringified)
|
|
24
|
+
* @returns The number of fields set
|
|
25
|
+
*/
|
|
4
26
|
hset(key: string, field: string, value: string): Promise<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Delete a field from a hash
|
|
29
|
+
* @param key - The key to delete from
|
|
30
|
+
* @param fields - The fields to delete
|
|
31
|
+
* @returns The number of fields deleted
|
|
32
|
+
*/
|
|
5
33
|
hdel(key: string, ...fields: string[]): Promise<number>;
|
|
34
|
+
/**
|
|
35
|
+
* Check if a key exists
|
|
36
|
+
* @param key - The key to check
|
|
37
|
+
* @returns 1 if the key exists, 0 otherwise
|
|
38
|
+
*/
|
|
6
39
|
exists(key: string): Promise<number>;
|
|
40
|
+
/**
|
|
41
|
+
* Set an expiration time for a key
|
|
42
|
+
* @param key - The key to set the expiration for
|
|
43
|
+
* @param seconds - The number of seconds until the key expires
|
|
44
|
+
* @returns 1 if the expiration was set, 0 if the key does not exist
|
|
45
|
+
*/
|
|
7
46
|
expire(key: string, seconds: number): Promise<any>;
|
|
8
47
|
}
|
|
@@ -50,6 +50,12 @@ let RedisUtils = (() => {
|
|
|
50
50
|
constructor() {
|
|
51
51
|
this.client = (0, redis_1.getRedisClient)(); // singleton Redis client instance
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* Get a value from Redis
|
|
55
|
+
* @param key - The key to get
|
|
56
|
+
* @param field - The field to get
|
|
57
|
+
* @returns The value
|
|
58
|
+
*/
|
|
53
59
|
async hget(key, field) {
|
|
54
60
|
try {
|
|
55
61
|
const value = await this.client.hget(key, field);
|
|
@@ -63,6 +69,32 @@ let RedisUtils = (() => {
|
|
|
63
69
|
throw error;
|
|
64
70
|
}
|
|
65
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Set a value in Redis with a TTL
|
|
74
|
+
* @param key - The key to set
|
|
75
|
+
* @param field - The field to set
|
|
76
|
+
* @param value - The value to set (JSON stringified)
|
|
77
|
+
* @param ttl - The TTL in seconds
|
|
78
|
+
* @returns The number of fields set
|
|
79
|
+
*/
|
|
80
|
+
async hsetWithTTL(key, field, value, ttl) {
|
|
81
|
+
try {
|
|
82
|
+
await this.hset(key, field, value);
|
|
83
|
+
await this.expire(key, ttl);
|
|
84
|
+
return 1;
|
|
85
|
+
}
|
|
86
|
+
catch (error) {
|
|
87
|
+
console.error(`Error setting value for key ${key}:`, error);
|
|
88
|
+
throw error;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Set a value in Redis
|
|
93
|
+
* @param key - The key to set
|
|
94
|
+
* @param field - The field to set
|
|
95
|
+
* @param value - The value to set (JSON stringified)
|
|
96
|
+
* @returns The number of fields set
|
|
97
|
+
*/
|
|
66
98
|
async hset(key, field, value) {
|
|
67
99
|
try {
|
|
68
100
|
return await this.client.hset(key, field, value);
|
|
@@ -72,29 +104,20 @@ let RedisUtils = (() => {
|
|
|
72
104
|
throw error;
|
|
73
105
|
}
|
|
74
106
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Delete a field from a hash
|
|
109
|
+
* @param key - The key to delete from
|
|
110
|
+
* @param fields - The fields to delete
|
|
111
|
+
* @returns The number of fields deleted
|
|
112
|
+
*/
|
|
78
113
|
async hdel(key, ...fields) {
|
|
79
114
|
return await this.client.hdel(key, ...fields);
|
|
80
115
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
// throw error;
|
|
87
|
-
// }
|
|
88
|
-
// }
|
|
89
|
-
/* async set(key: string, value: string): Promise<boolean> {
|
|
90
|
-
try {
|
|
91
|
-
await this.client.set(key, value);
|
|
92
|
-
return true;
|
|
93
|
-
} catch (error) {
|
|
94
|
-
console.error(`Error setting value for key ${key}:`, error);
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
} */
|
|
116
|
+
/**
|
|
117
|
+
* Check if a key exists
|
|
118
|
+
* @param key - The key to check
|
|
119
|
+
* @returns 1 if the key exists, 0 otherwise
|
|
120
|
+
*/
|
|
98
121
|
async exists(key) {
|
|
99
122
|
try {
|
|
100
123
|
return await this.client.exists(key);
|
|
@@ -104,6 +127,12 @@ let RedisUtils = (() => {
|
|
|
104
127
|
throw error;
|
|
105
128
|
}
|
|
106
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Set an expiration time for a key
|
|
132
|
+
* @param key - The key to set the expiration for
|
|
133
|
+
* @param seconds - The number of seconds until the key expires
|
|
134
|
+
* @returns 1 if the expiration was set, 0 if the key does not exist
|
|
135
|
+
*/
|
|
107
136
|
async expire(key, seconds) {
|
|
108
137
|
try {
|
|
109
138
|
return await this.client.expire(key, seconds);
|