-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move session and user management to redis
- Loading branch information
Showing
4 changed files
with
29 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 11 additions & 5 deletions
16
backend/src/main/kotlin/com/retypeme/project/auth/UserRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
package com.retypeme.project.auth | ||
|
||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.data.redis.core.HashOperations | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.stereotype.Repository | ||
import java.util.concurrent.ConcurrentHashMap | ||
|
||
|
||
@Repository | ||
class UserRepository { | ||
private val users: MutableMap<String, User> = ConcurrentHashMap() | ||
class UserRepository(@Autowired val redisTemplate: RedisTemplate<String, User>) { | ||
|
||
private val hashOps: HashOperations<String, String, User> by lazy { | ||
redisTemplate.opsForHash() | ||
} | ||
|
||
fun getUser(id: String): User = users.computeIfAbsent(id) { User(it) } | ||
fun getUser(id: String): User { | ||
return hashOps.get("users", id) ?: User(id).also { hashOps.put("users", id, it) } | ||
} | ||
} |
4 changes: 3 additions & 1 deletion
4
backend/src/main/kotlin/com/retypeme/project/control/Session.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package com.retypeme.project.control | ||
|
||
class Session(val id: String, val chain: Int, var players: Int) | ||
import java.io.Serializable | ||
|
||
data class Session(val id: String, val chain: Int, var players: Int) : Serializable |
21 changes: 13 additions & 8 deletions
21
backend/src/main/kotlin/com/retypeme/project/control/SessionRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,35 @@ | ||
package com.retypeme.project.control | ||
|
||
import org.springframework.stereotype.Component | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.data.redis.core.HashOperations | ||
import org.springframework.data.redis.core.RedisTemplate | ||
import org.springframework.stereotype.Repository | ||
import kotlin.random.Random | ||
|
||
@Component | ||
class SessionRepository { | ||
@Repository | ||
class SessionRepository(@Autowired val redisTemplate: RedisTemplate<String, Session>) { | ||
|
||
private val openSessions: MutableMap<String, Session> = mutableMapOf() | ||
private val hashOps: HashOperations<String, String, Session> by lazy { | ||
redisTemplate.opsForHash() | ||
} | ||
|
||
fun createSession(players: Int, chain: Int): Session { | ||
var sessionId: String | ||
do { | ||
sessionId = Random.nextInt(11111, 99999).toString() | ||
} while (openSessions.containsKey(sessionId)) | ||
} while (hashOps.hasKey("sessions", sessionId)) | ||
|
||
val session = Session(sessionId, chain, players) | ||
openSessions[sessionId] = session | ||
hashOps.put("sessions", sessionId, session) | ||
return session | ||
} | ||
|
||
fun getAllSessions(): List<Session> { | ||
return openSessions.values.map { it } | ||
return hashOps.entries("sessions").values.toList() | ||
} | ||
|
||
fun getSessionById(id: String): Session { | ||
return openSessions[id] ?: throw Exception("Session not found") | ||
return hashOps.get("sessions", id) ?: throw Exception("Session not found") | ||
} | ||
|
||
} |