perf(redis): use the same client in all xp goroutines

This commit is contained in:
Anhgelus Morhtuuzh 2024-04-15 11:52:21 +02:00
parent dcc260c4b0
commit 48ca185105
No known key found for this signature in database
GPG key ID: CF4550297832A29F
3 changed files with 38 additions and 14 deletions

View file

@ -2,7 +2,9 @@ package xp
import (
"github.com/anhgelus/gokord"
"github.com/anhgelus/gokord/utils"
"github.com/bwmarrin/discordgo"
"github.com/redis/go-redis/v9"
"gorm.io/gorm"
)
@ -13,6 +15,8 @@ type Copaing struct {
GuildID string
}
var r *redis.Client
func (c *Copaing) Load() *Copaing {
gokord.DB.Where("discord_id = ? and guild_id = ?", c.DiscordID, c.GuildID).FirstOrCreate(c)
return c
@ -32,3 +36,22 @@ func (c *Copaing) AddXP(s *discordgo.Session, xp uint, fn func(uint, uint)) {
onNewLevel(s, newLevel)
}
}
func getRedisClient() (*redis.Client, error) {
if r == nil {
var err error
r, err = gokord.BaseCfg.Redis.Get()
return r, err
}
return r, nil
}
func CloseRedisClient() {
if r == nil {
return
}
err := r.Close()
if err != nil {
utils.SendAlert("xp/member.go - Closing redis client", err.Error())
}
}