mirror of
https://github.com/pagefaultgames/rogueserver.git
synced 2025-04-02 02:57:15 +08:00
feat: Update cache implementation to use Redis for improved performance
This commit is contained in:
parent
a36f13b817
commit
ce54f57f53
90
cache/account.go
vendored
90
cache/account.go
vendored
@ -18,18 +18,20 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func AddAccountSession(uuid []byte, token []byte) bool {
|
func AddAccountSession(uuid []byte, token []byte) bool {
|
||||||
rdb.Do("SELECT", sessionDB)
|
key := fmt.Sprintf("session:%s", token)
|
||||||
err := rdb.Set(string(token), string(uuid), 7*24*time.Hour).Err()
|
err := rdb.Set(key, string(uuid), 24*time.Hour).Err()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchUsernameBySessionToken(token []byte) (string, bool) {
|
func FetchUsernameBySessionToken(token []byte) (string, bool) {
|
||||||
rdb.Do("SELECT", sessionDB)
|
key := fmt.Sprintf("session:%s", token)
|
||||||
username, err := rdb.Get(string(token)).Result()
|
username, err := rdb.Get(key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
@ -37,40 +39,31 @@ func FetchUsernameBySessionToken(token []byte) (string, bool) {
|
|||||||
return username, true
|
return username, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateActivePlayers(uuid []byte) bool {
|
|
||||||
rdb.Do("SELECT", activePlayersDB)
|
|
||||||
err := rdb.Set(string(uuid), 1, 0).Err()
|
|
||||||
if err != nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
err = rdb.Expire(string(uuid), 5*time.Minute).Err()
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateAccountLastActivity(uuid []byte) bool {
|
func UpdateAccountLastActivity(uuid []byte) bool {
|
||||||
rdb.Do("SELECT", accountsDB)
|
key := fmt.Sprintf("account:%s", uuid)
|
||||||
err := rdb.HSet(string(uuid), "lastActivity", time.Now().Format("2006-01-02 15:04:05")).Err()
|
err := rdb.HSet(key, "lastActivity", time.Now().Format("2006-01-02 15:04:05")).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
updateActivePlayers(uuid)
|
err = rdb.Expire(key, 5*time.Minute).Err()
|
||||||
|
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME
|
||||||
func UpdateAccountStats(uuid []byte, battles, classicSessionsPlayed int) bool {
|
func UpdateAccountStats(uuid []byte, battles, classicSessionsPlayed int) bool {
|
||||||
rdb.Do("SELECT", accountsDB)
|
key := fmt.Sprintf("account:%s", uuid)
|
||||||
err := rdb.HIncrBy(string(uuid), "battles", int64(battles)).Err()
|
err := rdb.HIncrBy(key, "battles", int64(battles)).Err()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
err = rdb.HIncrBy(string(uuid), "classicSessionsPlayed", int64(classicSessionsPlayed)).Err()
|
err = rdb.HIncrBy(key, "classicSessionsPlayed", int64(classicSessionsPlayed)).Err()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchTrainerIds(uuid []byte) (int, int, bool) {
|
func FetchTrainerIds(uuid []byte) (int, int, bool) {
|
||||||
rdb.Do("SELECT", accountsDB)
|
log.Println("FetchTrainerIds", uuid)
|
||||||
vals, err := rdb.HMGet(string(uuid), "trainerId", "secretId").Result()
|
key := fmt.Sprintf("account:%s", uuid)
|
||||||
|
vals, err := rdb.HMGet(key, "trainerId", "secretId").Result()
|
||||||
if err == nil && len(vals) == 2 && vals[0] != nil && vals[1] != nil {
|
if err == nil && len(vals) == 2 && vals[0] != nil && vals[1] != nil {
|
||||||
trainerId, ok1 := vals[0].(int)
|
trainerId, ok1 := vals[0].(int)
|
||||||
secretId, ok2 := vals[1].(int)
|
secretId, ok2 := vals[1].(int)
|
||||||
@ -83,42 +76,53 @@ func FetchTrainerIds(uuid []byte) (int, int, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTrainerIds(trainerId, secretId int, uuid []byte) bool {
|
func UpdateTrainerIds(trainerId, secretId int, uuid []byte) bool {
|
||||||
rdb.Do("SELECT", accountsDB)
|
key := fmt.Sprintf("account:%s", uuid)
|
||||||
err := rdb.HMSet(string(uuid), map[string]interface{}{
|
err := rdb.HMSet(key, map[string]interface{}{
|
||||||
"trainerId": trainerId,
|
"trainerId": trainerId,
|
||||||
"secretId": secretId,
|
"secretId": secretId,
|
||||||
}).Err()
|
}).Err()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rdb.Expire(key, 5*time.Minute).Err()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsActiveSession(uuid []byte, sessionId string) (bool, bool) {
|
func IsActiveSession(uuid []byte, sessionId string) (bool, bool) {
|
||||||
rdb.Do("SELECT", activeClientSessionsDB)
|
key := fmt.Sprintf("active_sessions:%s", uuid)
|
||||||
id, err := rdb.Get(string(uuid)).Result()
|
id, err := rdb.Get(key).Result()
|
||||||
if err != nil {
|
return id == sessionId, err == nil
|
||||||
return false, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return id == sessionId, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateActiveSession(uuid []byte, sessionId string) bool {
|
func UpdateActiveSession(uuid []byte, sessionId string) bool {
|
||||||
rdb.Do("SELECT", activeClientSessionsDB)
|
key := fmt.Sprintf("active_sessions:%s", uuid)
|
||||||
err := rdb.Set(string(uuid), sessionId, 0).Err()
|
err := rdb.Set(key, sessionId, 0).Err()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
err = rdb.Expire(key, 5*time.Minute).Err()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
err = rdb.SAdd("active_players", uuid).Err()
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
err = rdb.Expire("active_players", 5*time.Minute).Err()
|
||||||
|
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchUUIDFromToken(token []byte) ([]byte, bool) {
|
func FetchUUIDFromToken(token []byte) ([]byte, bool) {
|
||||||
rdb.Do("SELECT", sessionDB)
|
key := fmt.Sprintf("session:%s", token)
|
||||||
uuid, err := rdb.Get(string(token)).Bytes()
|
uuid, err := rdb.Get(key).Bytes()
|
||||||
if err != nil {
|
return uuid, err == nil
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return uuid, true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func RemoveSessionFromToken(token []byte) bool {
|
func RemoveSessionFromToken(token []byte) bool {
|
||||||
rdb.Do("SELECT", sessionDB)
|
key := fmt.Sprintf("session:%s", token)
|
||||||
err := rdb.Del(string(token)).Err()
|
err := rdb.Del(key).Err()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
10
cache/cache.go
vendored
10
cache/cache.go
vendored
@ -24,16 +24,6 @@ import (
|
|||||||
"github.com/go-redis/redis"
|
"github.com/go-redis/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
dailyRunCompletionsDB = 1
|
|
||||||
dailyRunsDB = 2
|
|
||||||
accountDailyRunsDB = 3
|
|
||||||
accountsDB = 4
|
|
||||||
sessionDB = 5
|
|
||||||
activeClientSessionsDB = 6
|
|
||||||
activePlayersDB = 7
|
|
||||||
)
|
|
||||||
|
|
||||||
var rdb *redis.Client
|
var rdb *redis.Client
|
||||||
|
|
||||||
func InitRedis(address, password, database string) error {
|
func InitRedis(address, password, database string) error {
|
||||||
|
12
cache/daily.go
vendored
12
cache/daily.go
vendored
@ -18,18 +18,22 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TryAddDailyRun(seed string) bool {
|
func TryAddDailyRun(seed string) bool {
|
||||||
rdb.Do("SELECT", dailyRunsDB)
|
key := fmt.Sprintf("daily:%s", time.Now().Format("2006-01-02"))
|
||||||
err := rdb.Set(time.Now().Format("2006-01-02"), seed, 24*time.Hour).Err()
|
now := time.Now()
|
||||||
|
midnight := time.Date(now.Year(), now.Month(), now.Day()+1, 0, 1, 0, 0, now.Location())
|
||||||
|
duration := time.Until(midnight)
|
||||||
|
err := rdb.Set(key, seed, duration).Err()
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetDailyRunSeed() (string, bool) {
|
func GetDailyRunSeed() (string, bool) {
|
||||||
rdb.Do("SELECT", dailyRunsDB)
|
key := fmt.Sprintf("daily:%s", time.Now().Format("2006-01-02"))
|
||||||
cachedSeed, err := rdb.Get(time.Now().Format("2006-01-02")).Result()
|
cachedSeed, err := rdb.Get(key).Result()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
|
35
cache/game.go
vendored
35
cache/game.go
vendored
@ -18,8 +18,7 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
func FetchPlayerCount() (int, bool) {
|
func FetchPlayerCount() (int, bool) {
|
||||||
rdb.Do("SELECT", activePlayersDB)
|
cachedPlayerCount, err := rdb.SCard("active_players").Result()
|
||||||
cachedPlayerCount, err := rdb.DBSize().Result()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
@ -27,34 +26,6 @@ func FetchPlayerCount() (int, bool) {
|
|||||||
return int(cachedPlayerCount), true
|
return int(cachedPlayerCount), true
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchBattleCount() (int, bool) {
|
// TODO Cache battle count
|
||||||
rdb.Do("SELECT", accountsDB)
|
|
||||||
cachedBattleCount, err := rdb.Get("battleCount").Int()
|
|
||||||
if err != nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return cachedBattleCount, true
|
// TODO Cache classic session count
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateBattleCount(battleCount int) bool {
|
|
||||||
rdb.Do("SELECT", accountsDB)
|
|
||||||
err := rdb.Set("battleCount", battleCount, 0).Err()
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func FetchClassicSessionCount() (int, bool) {
|
|
||||||
rdb.Do("SELECT", accountsDB)
|
|
||||||
cachedClassicSessionCount, err := rdb.Get("classicSessionCount").Int()
|
|
||||||
if err != nil {
|
|
||||||
return 0, false
|
|
||||||
}
|
|
||||||
|
|
||||||
return cachedClassicSessionCount, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func UpdateClassicSessionCount(classicSessionCount int) bool {
|
|
||||||
rdb.Do("SELECT", accountsDB)
|
|
||||||
err := rdb.Set("classicSessionCount", classicSessionCount, 0).Err()
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
9
cache/savedata.go
vendored
9
cache/savedata.go
vendored
@ -18,12 +18,13 @@
|
|||||||
package cache
|
package cache
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TryAddSeedCompletion(uuid []byte, seed string, mode int) bool {
|
func TryAddSeedCompletion(uuid []byte, seed string, mode int) bool {
|
||||||
rdb.Do("SELECT", dailyRunCompletionsDB)
|
key := fmt.Sprintf("savedata:%s", uuid)
|
||||||
err := rdb.HMSet(string(uuid), map[string]interface{}{
|
err := rdb.HMSet(key, map[string]interface{}{
|
||||||
"mode": mode,
|
"mode": mode,
|
||||||
"seed": seed,
|
"seed": seed,
|
||||||
"timestamp": time.Now().Unix(),
|
"timestamp": time.Now().Unix(),
|
||||||
@ -32,7 +33,7 @@ func TryAddSeedCompletion(uuid []byte, seed string, mode int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ReadSeedCompletion(uuid []byte, seed string) (bool, bool) {
|
func ReadSeedCompletion(uuid []byte, seed string) (bool, bool) {
|
||||||
rdb.Do("SELECT", dailyRunCompletionsDB)
|
key := fmt.Sprintf("savedata:%s", uuid)
|
||||||
completed, err := rdb.HExists(string(uuid), seed).Result()
|
completed, err := rdb.HExists(key, seed).Result()
|
||||||
return completed, err == nil
|
return completed, err == nil
|
||||||
}
|
}
|
||||||
|
14
db/game.go
14
db/game.go
@ -36,9 +36,9 @@ func FetchPlayerCount() (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func FetchBattleCount() (int, error) {
|
func FetchBattleCount() (int, error) {
|
||||||
if cachedBattleCount, ok := cache.FetchBattleCount(); ok {
|
// if cachedBattleCount, ok := cache.FetchBattleCount(); ok {
|
||||||
return cachedBattleCount, nil
|
// return cachedBattleCount, nil
|
||||||
}
|
// }
|
||||||
|
|
||||||
var battleCount int
|
var battleCount int
|
||||||
err := handle.QueryRow("SELECT COALESCE(SUM(s.battles), 0) FROM accountStats s JOIN accounts a ON a.uuid = s.uuid WHERE a.banned = 0").Scan(&battleCount)
|
err := handle.QueryRow("SELECT COALESCE(SUM(s.battles), 0) FROM accountStats s JOIN accounts a ON a.uuid = s.uuid WHERE a.banned = 0").Scan(&battleCount)
|
||||||
@ -46,23 +46,15 @@ func FetchBattleCount() (int, error) {
|
|||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.UpdateBattleCount(battleCount)
|
|
||||||
|
|
||||||
return battleCount, nil
|
return battleCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func FetchClassicSessionCount() (int, error) {
|
func FetchClassicSessionCount() (int, error) {
|
||||||
if cachedClassicSessionCount, ok := cache.FetchClassicSessionCount(); ok {
|
|
||||||
return cachedClassicSessionCount, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var classicSessionCount int
|
var classicSessionCount int
|
||||||
err := handle.QueryRow("SELECT COALESCE(SUM(s.classicSessionsPlayed), 0) FROM accountStats s JOIN accounts a ON a.uuid = s.uuid WHERE a.banned = 0").Scan(&classicSessionCount)
|
err := handle.QueryRow("SELECT COALESCE(SUM(s.classicSessionsPlayed), 0) FROM accountStats s JOIN accounts a ON a.uuid = s.uuid WHERE a.banned = 0").Scan(&classicSessionCount)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
cache.UpdateClassicSessionCount(classicSessionCount)
|
|
||||||
|
|
||||||
return classicSessionCount, nil
|
return classicSessionCount, nil
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user