shivam413 commited on
Commit
fa45681
·
verified ·
1 Parent(s): c4a0bc9

Update lib/cache.ts

Browse files
Files changed (1) hide show
  1. lib/cache.ts +30 -50
lib/cache.ts CHANGED
@@ -1,71 +1,51 @@
1
- import { createClient } from "redis"
2
- import { getRedisURL } from "./env"
3
  import { RoomState } from "./types"
4
 
5
- const client = createClient({
6
- url: getRedisURL(),
7
- })
8
 
9
- ;(async () => {
10
- await client.connect()
11
- })()
12
-
13
- client.on("reconnecting", () => {
14
- console.log("Trying to reconnect to redis server ...")
15
- })
16
- client.on("error", (error) => {
17
- console.error("Failed to contact redis server due to:", error)
18
- })
19
-
20
- export const getRoom = async (roomId: string) => {
21
- const data = await client.get("room:" + roomId)
22
- if (data === null) {
23
- return data
24
- }
25
-
26
- return JSON.parse(data) as RoomState
27
  }
28
 
29
- export const roomExists = async (roomId: string) => {
30
- return await client.exists("room:" + roomId)
 
31
  }
32
 
33
- export const setRoom = async (roomId: string, data: RoomState) => {
34
- if (!(await client.sIsMember("rooms", roomId))) {
35
- await client.sAdd("rooms", roomId)
36
- }
37
- return await client.set("room:" + roomId, JSON.stringify(data))
38
  }
39
 
40
- export const deleteRoom = async (roomId: string) => {
41
- await client.sRem("rooms", roomId)
42
- return await client.del("room:" + roomId)
43
  }
44
 
45
- export const listRooms = async () => {
46
- return await client.sMembers("rooms")
47
  }
48
 
49
- export const countRooms = async () => {
50
- return await client.sCard("rooms")
51
  }
52
 
53
- export const countUsers = async () => {
54
- const count = await client.get("userCount")
55
- if (count === null) {
56
- return 0
57
- }
58
- return parseInt(count)
59
  }
60
 
61
- export const incUsers = async () => {
62
- return await client.incr("userCount")
 
63
  }
64
 
65
- export const decUsers = async () => {
66
- return await client.decr("userCount")
 
67
  }
68
 
69
- export const wipeCache = async () => {
70
- return await client.flushAll()
71
- }
 
 
 
 
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  }
10
 
11
+ export const roomExists = async (roomId: string): Promise<number> => {
12
+ // Keep Redis-like return type (0/1) for compatibility
13
+ return rooms.has(roomId) ? 1 : 0
14
  }
15
 
16
+ export const setRoom = async (roomId: string, data: RoomState): Promise<"OK"> => {
17
+ rooms.set(roomId, data)
18
+ return "OK"
 
 
19
  }
20
 
21
+ export const deleteRoom = async (roomId: string): Promise<number> => {
22
+ return rooms.delete(roomId) ? 1 : 0
 
23
  }
24
 
25
+ export const listRooms = async (): Promise<string[]> => {
26
+ return Array.from(rooms.keys())
27
  }
28
 
29
+ export const countRooms = async (): Promise<number> => {
30
+ return rooms.size
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
+ }