@mulingai-npm/redis 3.10.0 → 3.11.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.
@@ -62,13 +62,24 @@ class MulingstreamSpeakerManager {
62
62
  speakerId,
63
63
  timestamp: new Date(Date.now()).toISOString()
64
64
  };
65
- await this.redisClient.hset(key, this.serialize(speakerData));
66
- await this.redisClient.expire(key, EXPIRATION);
67
- await this.redisClient.sadd(`room:${payload.roomId}:speakers`, speakerId);
68
- await this.redisClient.sadd(`user:${payload.userId}:speakers`, speakerId);
69
- await this.redisClient.set(`socket:${payload.socketId}:speaker`, speakerId);
70
- await this.redisClient.expire(`socket:${payload.socketId}:speaker`, EXPIRATION);
71
- return speakerId;
65
+ try {
66
+ // Store main speaker hash
67
+ const serializedData = this.serialize(speakerData);
68
+ await this.redisClient.hset(key, serializedData);
69
+ await this.redisClient.expire(key, EXPIRATION);
70
+ // Add to room index
71
+ await this.redisClient.sadd(`room:${payload.roomId}:speakers`, speakerId);
72
+ // Add to user index
73
+ await this.redisClient.sadd(`user:${payload.userId}:speakers`, speakerId);
74
+ // Create socket mapping
75
+ await this.redisClient.set(`socket:${payload.socketId}:speaker`, speakerId);
76
+ await this.redisClient.expire(`socket:${payload.socketId}:speaker`, EXPIRATION);
77
+ return speakerId;
78
+ }
79
+ catch (error) {
80
+ console.error('❌ [addSpeaker] Error during speaker registration:', error);
81
+ throw error;
82
+ }
72
83
  }
73
84
  async removeSpeakerBySocketId(socketId) {
74
85
  const speakerId = await this.redisClient.get(`socket:${socketId}:speaker`);
@@ -123,17 +134,27 @@ class MulingstreamSpeakerManager {
123
134
  return null;
124
135
  }
125
136
  async getSpeakersByRoomId(roomId) {
137
+ console.log(`🔍 [getSpeakersByRoomId] Looking for speakers in room: ${roomId}`);
126
138
  const ids = await this.redisClient.smembers(`room:${roomId}:speakers`);
139
+ console.log(`🔍 [getSpeakersByRoomId] Found speaker IDs in room index:`, ids);
127
140
  const result = [];
128
141
  for (const id of ids) {
129
- const hash = await this.redisClient.hgetall(this.buildKey(id));
142
+ console.log(`🔍 [getSpeakersByRoomId] Processing speaker ID: ${id}`);
143
+ const key = this.buildKey(id);
144
+ console.log(`🔍 [getSpeakersByRoomId] Generated key: ${key}`);
145
+ const hash = await this.redisClient.hgetall(key);
146
+ console.log(`🔍 [getSpeakersByRoomId] Retrieved hash for ${id}:`, hash);
130
147
  if (hash !== null && Object.keys(hash).length > 0) {
131
- result.push(this.parseHash(hash));
148
+ const parsedSpeaker = this.parseHash(hash);
149
+ console.log(`✅ [getSpeakersByRoomId] Successfully parsed speaker:`, parsedSpeaker);
150
+ result.push(parsedSpeaker);
132
151
  }
133
152
  else {
153
+ console.log(`⚠️ [getSpeakersByRoomId] Speaker ${id} not found or empty, cleaning indexes`);
134
154
  await this.cleanIndexes(id);
135
155
  }
136
156
  }
157
+ console.log(`🔍 [getSpeakersByRoomId] Final result for room ${roomId}:`, result);
137
158
  return result;
138
159
  }
139
160
  async getSpeakersByUserId(userId) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mulingai-npm/redis",
3
- "version": "3.10.0",
3
+ "version": "3.11.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "repository": {
@@ -1,27 +0,0 @@
1
- import { RedisClient } from '../redis-client';
2
- export interface CachedPageContent {
3
- id: string;
4
- path: string;
5
- locale: string;
6
- route: string;
7
- payload: string;
8
- createdAt: number;
9
- lastUsedAt: number | null;
10
- totalUsage: number;
11
- }
12
- export declare class PageContentManager {
13
- private redis;
14
- private static DEFAULT_TTL;
15
- constructor(redis: RedisClient);
16
- private key;
17
- private build;
18
- private scan;
19
- addPageContent(locale: string, route: string, payload: unknown, ttl?: number): Promise<void>;
20
- getPageContent(locale: string, route: string): Promise<CachedPageContent | null>;
21
- getAllPageContents(): Promise<CachedPageContent[]>;
22
- getAllPageContentsByLocale(locale: string): Promise<CachedPageContent[]>;
23
- clearPageContent(locale: string, route: string): Promise<void>;
24
- clearAllPageContents(): Promise<void>;
25
- clearAllPageContentsByLocale(locale: string): Promise<void>;
26
- incrementUsage(locale: string, route: string): Promise<void>;
27
- }
@@ -1,95 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.PageContentManager = void 0;
4
- class PageContentManager {
5
- constructor(redis) {
6
- this.redis = redis;
7
- }
8
- key(locale, route) {
9
- return `page-content--[${locale.toLowerCase()}]-[${route.toLowerCase()}]`;
10
- }
11
- build(locale, route, payload) {
12
- const id = this.key(locale, route);
13
- const now = Date.now();
14
- return {
15
- id,
16
- path: `/${locale}/${route}`,
17
- locale,
18
- route,
19
- payload: JSON.stringify(payload),
20
- createdAt: now,
21
- lastUsedAt: null,
22
- totalUsage: 0
23
- };
24
- }
25
- async scan(pattern) {
26
- let cursor = 0;
27
- const keys = [];
28
- do {
29
- const [next, batch] = await this.redis.scan(cursor, pattern);
30
- cursor = Number(next);
31
- keys.push(...batch);
32
- } while (cursor !== 0);
33
- return keys;
34
- }
35
- async addPageContent(locale, route, payload, ttl = PageContentManager.DEFAULT_TTL) {
36
- const obj = this.build(locale, route, payload);
37
- await this.redis.set(obj.id, JSON.stringify(obj));
38
- await this.redis.expire(obj.id, ttl);
39
- }
40
- async getPageContent(locale, route) {
41
- const raw = await this.redis.get(this.key(locale, route));
42
- return raw ? JSON.parse(raw) : null;
43
- }
44
- async getAllPageContents() {
45
- const keys = await this.scan('page-content--[*');
46
- if (!keys.length)
47
- return [];
48
- const p = this.redis.pipeline();
49
- keys.forEach((k) => p.get(k));
50
- const res = await p.exec();
51
- return res.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
52
- }
53
- async getAllPageContentsByLocale(locale) {
54
- const keys = await this.scan(`page-content--[${locale.toLowerCase()}]-[*`);
55
- if (!keys.length)
56
- return [];
57
- const p = this.redis.pipeline();
58
- keys.forEach((k) => p.get(k));
59
- const res = await p.exec();
60
- return res.flatMap(([_, v]) => (v ? [JSON.parse(v)] : []));
61
- }
62
- async clearPageContent(locale, route) {
63
- await this.redis.unlink(this.key(locale, route));
64
- }
65
- async clearAllPageContents() {
66
- const keys = await this.scan('page-content--[*');
67
- if (keys.length)
68
- await this.redis.unlink(...keys);
69
- }
70
- async clearAllPageContentsByLocale(locale) {
71
- const keys = await this.scan(`page-content--[${locale.toLowerCase()}]-[*`);
72
- if (keys.length)
73
- await this.redis.unlink(...keys);
74
- }
75
- async incrementUsage(locale, route) {
76
- var _a;
77
- const k = this.key(locale, route);
78
- const pipe = this.redis.pipeline();
79
- pipe.get(k);
80
- pipe.ttl(k);
81
- const [getRes, ttlRes] = (await pipe.exec());
82
- const raw = getRes === null || getRes === void 0 ? void 0 : getRes[1];
83
- if (!raw)
84
- return;
85
- const ttl = Number((_a = ttlRes === null || ttlRes === void 0 ? void 0 : ttlRes[1]) !== null && _a !== void 0 ? _a : -1);
86
- const obj = JSON.parse(raw);
87
- obj.totalUsage += 1;
88
- obj.lastUsedAt = Date.now();
89
- await this.redis.set(k, JSON.stringify(obj));
90
- if (ttl > 0)
91
- await this.redis.expire(k, ttl);
92
- }
93
- }
94
- exports.PageContentManager = PageContentManager;
95
- PageContentManager.DEFAULT_TTL = 24 * 60 * 60;