diff options
| author | William Hergès <william@herges.fr> | 2026-01-17 16:31:25 +0100 |
|---|---|---|
| committer | William Hergès <william@herges.fr> | 2026-01-17 16:31:25 +0100 |
| commit | febb77607e81fbb182dd456733ea5adafda44ed4 (patch) | |
| tree | 3ff850a34d716df8315d3b9839768e6a4ff60a4c /user/state.go | |
| parent | 55befa3a53ab56bac31026b1b6099b2d31fd6d91 (diff) | |
perf(member): use stat for load
Diffstat (limited to 'user/state.go')
| -rw-r--r-- | user/state.go | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/user/state.go b/user/state.go index bef2f53..07096db 100644 --- a/user/state.go +++ b/user/state.go @@ -1,6 +1,7 @@ package user import ( + "context" "sync" "github.com/nyttikord/gokord/state" @@ -14,6 +15,35 @@ type CopaingCached struct { XPToAdd uint } +// Copaing turns a CopaingCached into a Copaing. +// This operation is heavy. +func (cc *CopaingCached) Copaing(ctx context.Context) *Copaing { + c := Copaing{DiscordID: cc.DiscordID, GuildID: cc.GuildID} + if err := c.Load(ctx); err != nil { + panic(err) + } + return &c +} + +func (cc *CopaingCached) Save(ctx context.Context) error { + state := GetState(ctx) + + state.mu.Lock() + defer state.mu.Unlock() + + return state.storage.Write(KeyCopaingCachedRaw(cc.GuildID, cc.DiscordID), *cc) +} + +func FromCopaing(c *Copaing) *CopaingCached { + return &CopaingCached{ + ID: c.ID, + DiscordID: c.DiscordID, + GuildID: c.GuildID, + XPs: calcXP(c), + XPToAdd: 0, + } +} + const KeyCopaingCachedPrefix = "cc:" func KeyCopaingCached(c *Copaing) state.Key { @@ -29,6 +59,22 @@ type State struct { storage state.MapStorage[CopaingCached] } +func NewState() *State { + return &State{ + storage: state.MapStorage[CopaingCached]{}, + } +} + +const ContextKeyState = "state" + +func GetState(ctx context.Context) *State { + return ctx.Value(ContextKeyState).(*State) +} + +func SetState(ctx context.Context, state *State) context.Context { + return context.WithValue(ctx, ContextKeyState, state) +} + func (s *State) Copaing(guildID, copaingID string) (*CopaingCached, error) { s.mu.RLock() defer s.mu.RUnlock() @@ -42,26 +88,19 @@ func (s *State) Copaing(guildID, copaingID string) (*CopaingCached, error) { } // CopaingAdd does not call Copaing.Load! -func (s *State) CopaingAdd(c *Copaing, xpToAdd uint) error { - s.mu.Lock() - defer s.mu.Unlock() - - sum := calcXP(c) +func (s *State) CopaingAdd(c *Copaing, xpToAdd uint) (*CopaingCached, error) { var err error var cc *CopaingCached - if cc, err = s.Copaing(c.GuildID, c.DiscordID); err != nil { - cc.XPs = sum + if cc, err = s.Copaing(c.GuildID, c.DiscordID); err == nil { + cc.XPs = calcXP(c) cc.XPToAdd = xpToAdd } else { - cc = &CopaingCached{ - ID: c.ID, - DiscordID: c.DiscordID, - GuildID: c.GuildID, - XPs: sum, - XPToAdd: xpToAdd, - } + cc = FromCopaing(c) } - return s.storage.Write(KeyCopaingCached(c), *cc) + s.mu.Lock() + defer s.mu.Unlock() + + return cc, s.storage.Write(KeyCopaingCached(c), *cc) } func (s *State) CopaingRemove(c *Copaing) error { |
