Skip to content

Commit

Permalink
move session and user management to redis
Browse files Browse the repository at this point in the history
  • Loading branch information
t0lia committed May 25, 2024
1 parent ad9ad4b commit 3532288
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
3 changes: 2 additions & 1 deletion backend/src/main/kotlin/com/retypeme/project/auth/User.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.retypeme.project.auth

import com.moonstoneid.siwe.util.Utils.generateNonce
import java.io.Serializable

data class User(
val address: String,
var nonce: String = generateNonce()
) {
) : Serializable {
fun changeNonce() {
nonce = generateNonce()
}
Expand Down
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) }
}
}
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
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")
}

}

0 comments on commit 3532288

Please sign in to comment.