copilot-swe-agent[bot] ArnavSingh76533 commited on
Commit
dc76b3f
·
1 Parent(s): aa9d0a3

Fix cache module isolation issue with global object

Browse files

Co-authored-by: ArnavSingh76533 <160649079+ArnavSingh76533@users.noreply.github.com>

Files changed (1) hide show
  1. lib/cache.ts +35 -10
lib/cache.ts CHANGED
@@ -1,8 +1,28 @@
1
  import { RoomState } from "./types"
2
 
3
- // In-memory stores
4
- const rooms = new Map<string, RoomState>()
5
- let userCount = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  export const getRoom = async (roomId: string): Promise<RoomState | null> => {
8
  return rooms.get(roomId) ?? null
@@ -31,31 +51,36 @@ export const countRooms = async (): Promise<number> => {
31
  }
32
 
33
  export const countUsers = async (): Promise<number> => {
34
- return userCount
35
  }
36
 
37
  export const incUsers = async (): Promise<number> => {
38
- userCount += 1
39
- return userCount
 
40
  }
41
 
42
  export const decUsers = async (): Promise<number> => {
43
- userCount = Math.max(0, userCount - 1)
44
- return userCount
 
45
  }
46
 
47
  export const wipeCache = async (): Promise<"OK"> => {
48
  rooms.clear()
49
- userCount = 0
50
  return "OK"
51
  }
52
 
53
  export const getPublicRooms = async (): Promise<RoomState[]> => {
54
  const publicRooms: RoomState[] = []
55
- for (const [_, room] of rooms) {
 
 
56
  if (room.isPublic) {
57
  publicRooms.push(room)
58
  }
59
  }
 
60
  return publicRooms
61
  }
 
1
  import { RoomState } from "./types"
2
 
3
+ // Use global object to ensure cache is shared across all API routes in development
4
+ // In Next.js dev mode, modules can be loaded multiple times, so we need to use global
5
+ const globalForCache = global as typeof globalThis & {
6
+ roomsCache?: Map<string, RoomState>
7
+ userCount?: number
8
+ }
9
+
10
+ // In-memory stores - use global to persist across hot reloads
11
+ const rooms = globalForCache.roomsCache || new Map<string, RoomState>()
12
+ if (!globalForCache.roomsCache) {
13
+ globalForCache.roomsCache = rooms
14
+ }
15
+
16
+ let userCount = globalForCache.userCount || 0
17
+ if (!globalForCache.userCount) {
18
+ globalForCache.userCount = 0
19
+ }
20
+
21
+ const getUserCount = () => globalForCache.userCount || 0
22
+ const setUserCount = (count: number) => {
23
+ globalForCache.userCount = count
24
+ userCount = count
25
+ }
26
 
27
  export const getRoom = async (roomId: string): Promise<RoomState | null> => {
28
  return rooms.get(roomId) ?? null
 
51
  }
52
 
53
  export const countUsers = async (): Promise<number> => {
54
+ return getUserCount()
55
  }
56
 
57
  export const incUsers = async (): Promise<number> => {
58
+ const newCount = getUserCount() + 1
59
+ setUserCount(newCount)
60
+ return newCount
61
  }
62
 
63
  export const decUsers = async (): Promise<number> => {
64
+ const newCount = Math.max(0, getUserCount() - 1)
65
+ setUserCount(newCount)
66
+ return newCount
67
  }
68
 
69
  export const wipeCache = async (): Promise<"OK"> => {
70
  rooms.clear()
71
+ setUserCount(0)
72
  return "OK"
73
  }
74
 
75
  export const getPublicRooms = async (): Promise<RoomState[]> => {
76
  const publicRooms: RoomState[] = []
77
+ console.log('[cache] getPublicRooms: total rooms =', rooms.size)
78
+ for (const [roomId, room] of rooms) {
79
+ console.log(`[cache] Room ${roomId}: isPublic =`, room.isPublic, 'users =', room.users.length)
80
  if (room.isPublic) {
81
  publicRooms.push(room)
82
  }
83
  }
84
+ console.log('[cache] Returning', publicRooms.length, 'public rooms')
85
  return publicRooms
86
  }