@mulingai-npm/redis 1.2.10 → 1.3.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.
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MulingstreamListenerManager = void 0;
|
|
4
4
|
const uuid_1 = require("uuid");
|
|
5
|
+
const EXPIRATION = 24 * 60 * 60; // 24 hours in seconds
|
|
5
6
|
class MulingstreamListenerManager {
|
|
6
7
|
constructor(redisClient) {
|
|
7
8
|
this.redisClient = redisClient;
|
|
@@ -38,16 +39,18 @@ class MulingstreamListenerManager {
|
|
|
38
39
|
language: (_c = listenerData.language) !== null && _c !== void 0 ? _c : '',
|
|
39
40
|
isActive: 'true'
|
|
40
41
|
});
|
|
42
|
+
// expire listener
|
|
43
|
+
await this.redisClient.expire(`listener:${listenerId}`, EXPIRATION);
|
|
41
44
|
return listenerId;
|
|
42
45
|
}
|
|
43
46
|
async getAllListeners() {
|
|
44
|
-
//
|
|
45
|
-
//
|
|
47
|
+
// get all keys that match 'listener:*'
|
|
48
|
+
// TODO: if we have many keys, consider using SCAN instead of KEYS to avoid performance issues.
|
|
46
49
|
const keys = await this.redisClient.keys('listener:*');
|
|
47
50
|
if (!keys || keys.length === 0) {
|
|
48
51
|
return [];
|
|
49
52
|
}
|
|
50
|
-
//
|
|
53
|
+
// fetch each hash with HGETALL
|
|
51
54
|
const listeners = [];
|
|
52
55
|
for (const key of keys) {
|
|
53
56
|
const data = await this.redisClient.hgetall(key);
|
|
@@ -56,18 +59,22 @@ class MulingstreamListenerManager {
|
|
|
56
59
|
return listeners;
|
|
57
60
|
}
|
|
58
61
|
async getListenersByRoom(roomId) {
|
|
59
|
-
//
|
|
62
|
+
// get the set of listener IDs
|
|
60
63
|
const listenerIds = await this.redisClient.smembers(`room:${roomId}:listeners`);
|
|
61
64
|
if (!listenerIds || listenerIds.length === 0) {
|
|
62
65
|
return [];
|
|
63
66
|
}
|
|
64
|
-
// 2) For each ID, fetch the hash from Redis
|
|
65
67
|
const listeners = [];
|
|
66
68
|
for (const id of listenerIds) {
|
|
69
|
+
// get hash data
|
|
67
70
|
const data = await this.redisClient.hgetall(`listener:${id}`);
|
|
68
|
-
if
|
|
69
|
-
|
|
71
|
+
// if the hash doesn't exist, it may have expired, so remove from the set
|
|
72
|
+
if (!data || Object.keys(data).length === 0) {
|
|
73
|
+
await this.redisClient.srem(`room:${roomId}:listeners`, id);
|
|
74
|
+
continue;
|
|
70
75
|
}
|
|
76
|
+
// otherwise, parse and keep it
|
|
77
|
+
listeners.push(this.parseHashData(data));
|
|
71
78
|
}
|
|
72
79
|
return listeners;
|
|
73
80
|
}
|
package/dist/redis-client.d.ts
CHANGED
package/dist/redis-client.js
CHANGED