Player cache and project structure refactor.
This commit is contained in:
69
internal/cache/cache.go
vendored
69
internal/cache/cache.go
vendored
@@ -1,18 +1,40 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"InfantrySkillCalculator/models"
|
||||
"InfantrySkillCalculator/utils"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func GetValue(key string) (string, error) {
|
||||
val, err := models.Cache.Get(ctx, key).Result()
|
||||
type PlayerCache struct {
|
||||
cache *redis.Client
|
||||
lifetime time.Duration
|
||||
}
|
||||
|
||||
func NewPlayerCache(address string, lifetime time.Duration) *PlayerCache {
|
||||
return &PlayerCache{
|
||||
redis.NewClient(
|
||||
&redis.Options{
|
||||
Addr: address,
|
||||
Password: "",
|
||||
DB: 0,
|
||||
}),
|
||||
lifetime,
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PlayerCache) Connect() error {
|
||||
_, err := pc.cache.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
func (pc *PlayerCache) GetValue(key string) (string, error) {
|
||||
val, err := pc.cache.Get(ctx, key).Result()
|
||||
if err != nil {
|
||||
return "", err // cache miss or error
|
||||
} else {
|
||||
@@ -20,31 +42,32 @@ func GetValue(key string) (string, error) {
|
||||
}
|
||||
}
|
||||
|
||||
func SetValue(key string, value interface{}) error {
|
||||
return models.Cache.Set(ctx, key, value, utils.PlayerCacheLifetime).Err()
|
||||
func (pc *PlayerCache) SetValue(key string, value interface{}) error {
|
||||
return pc.cache.Set(ctx, key, value, pc.lifetime).Err()
|
||||
}
|
||||
|
||||
func SetScore(playerId uint, gameTag string, score float32) error {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
return SetValue(key, score)
|
||||
func (pc *PlayerCache) SetScore(playerId uint, gameTag string, score float32) error {
|
||||
key := getPlayerCacheKey(playerId, gameTag)
|
||||
return pc.SetValue(key, score)
|
||||
}
|
||||
|
||||
func GetScore(playerId uint, gameTag string) (float32, error) {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
val, err := GetValue(key)
|
||||
func (pc *PlayerCache) GetScore(playerId uint, gameTag string) (float32, error) {
|
||||
key := getPlayerCacheKey(playerId, gameTag)
|
||||
val, err := pc.GetValue(key)
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return -1.0, nil // cache miss
|
||||
} else if err != nil {
|
||||
return -1.0, err // cache error
|
||||
} else {
|
||||
return utils.StringToFloat(val), nil // cache hit
|
||||
valFloat, _ := strconv.ParseFloat(val, 32)
|
||||
return float32(valFloat), nil // cache hit
|
||||
}
|
||||
}
|
||||
|
||||
func GetScores(players []models.Player, gameTag string) ([]float32, error) {
|
||||
vals, err := models.Cache.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
for _, p := range players {
|
||||
key := GetPlayerCacheKey(p.ID, gameTag)
|
||||
func (pc *PlayerCache) GetScores(playerIds []uint, gameTag string) ([]float32, error) {
|
||||
vals, err := pc.cache.Pipelined(ctx, func(pipe redis.Pipeliner) error {
|
||||
for _, id := range playerIds {
|
||||
key := getPlayerCacheKey(id, gameTag)
|
||||
pipe.Get(ctx, key)
|
||||
}
|
||||
return nil
|
||||
@@ -69,15 +92,15 @@ func GetScores(players []models.Player, gameTag string) ([]float32, error) {
|
||||
return scores, nil
|
||||
}
|
||||
|
||||
func DeleteScore(playerId uint, gameTag string) error {
|
||||
key := GetPlayerCacheKey(playerId, gameTag)
|
||||
return models.Cache.Del(ctx, key).Err()
|
||||
func (pc *PlayerCache) DeleteScore(playerId uint, gameTag string) error {
|
||||
key := getPlayerCacheKey(playerId, gameTag)
|
||||
return pc.cache.Del(ctx, key).Err()
|
||||
}
|
||||
|
||||
func PurgeCache() error {
|
||||
return models.Cache.FlushAll(ctx).Err()
|
||||
func (pc *PlayerCache) PurgeCache() error {
|
||||
return pc.cache.FlushAll(ctx).Err()
|
||||
}
|
||||
|
||||
func GetPlayerCacheKey(playerId uint, gameTag string) string {
|
||||
func getPlayerCacheKey(playerId uint, gameTag string) string {
|
||||
return fmt.Sprintf("player:%d:game:%s", playerId, gameTag)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user