refactor(cache): remove exists lookup

This commit is contained in:
ayn2op 2026-07-02 21:37:15 -04:00
parent 825745e9e2
commit bb3ee02edc
2 changed files with 9 additions and 11 deletions

View file

@ -17,14 +17,12 @@ func (c *Cache) Create(query string, value uint) {
c.items.Store(query, value)
}
func (c *Cache) Exists(query string) (ok bool) {
_, ok = c.items.Load(query)
return
}
func (c *Cache) Get(query string) uint {
i, _ := c.items.Load(query)
return i.(uint)
func (c *Cache) Get(query string) (uint, bool) {
i, ok := c.items.Load(query)
if !ok {
return 0, false
}
return i.(uint), true
}
// Invalidate is only needed when a member leaves and the search query reaches

View file

@ -620,15 +620,15 @@ func channelHasUser(state *ningen.State, channelID discord.ChannelID, userID dis
// It emits a follow-up suggestion message once results are loaded.
func (c *composer) searchMember(gID discord.GuildID, name string) tview.Cmd {
key := gID.String() + " " + name
if c.cache.Exists(key) {
if _, ok := c.cache.Get(key); ok {
return nil
}
// If searching for "ab" returns less than SearchLimit,
// then "abc" would not return anything new because we already searched
// everything starting with "ab". This will still be true even if a new
// member joins because arikawa loads new members into the state.
if k := key[:len(key)-1]; c.cache.Exists(k) {
if count := c.cache.Get(k); count < c.chat.state.MemberState.SearchLimit {
if count, ok := c.cache.Get(key[:len(key)-1]); ok {
if count < c.chat.state.MemberState.SearchLimit {
c.cache.Create(key, count)
return nil
}