diff options
| author | Anhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me> | 2024-04-15 11:52:21 +0200 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <anhgelus.morhtuuzh@proton.me> | 2024-04-15 11:52:21 +0200 |
| commit | 48ca185105988aba0626850bf133ba364edd835e (patch) | |
| tree | e9fbb1b327212845d4689fd650366aeecbc35cd5 /xp/member.go | |
| parent | dcc260c4b0c8733fcc63322bcdd74f4540fb5a95 (diff) | |
perf(redis): use the same client in all xp goroutines
Diffstat (limited to 'xp/member.go')
| -rw-r--r-- | xp/member.go | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/xp/member.go b/xp/member.go index e3714fa..578e533 100644 --- a/xp/member.go +++ b/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()) + } +} |
