perf(redis): use the same client in all xp goroutines
This commit is contained in:
parent
dcc260c4b0
commit
48ca185105
3 changed files with 38 additions and 14 deletions
23
xp/member.go
23
xp/member.go
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue