@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
- // 1) Get all keys that match 'listener:*'
45
- // If you have many keys, consider using SCAN instead of KEYS to avoid performance issues.
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
- // 2) Fetch each hash with HGETALL
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
- // 1) Get the set of listener IDs
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 (data) {
69
- listeners.push(this.parseHashData(data));
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
  }
@@ -18,4 +18,5 @@ export declare class RedisClient {
18
18
  hgetall(key: string): Promise<Record<string, string>>;
19
19
  flushAll(): Promise<string>;
20
20
  flushDb(): Promise<string>;
21
+ expire(key: string, seconds: number): Promise<number>;
21
22
  }
@@ -58,5 +58,8 @@ class RedisClient {
58
58
  async flushDb() {
59
59
  return this.client.flushdb();
60
60
  }
61
+ async expire(key, seconds) {
62
+ return this.client.expire(key, seconds);
63
+ }
61
64
  }
62
65
  exports.RedisClient = RedisClient;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "1.2.10",
3
+ "version": "1.3.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {