diff --git a/cache.go b/cache.go index 5de1550..da481b6 100644 --- a/cache.go +++ b/cache.go @@ -3,6 +3,7 @@ package valkey import ( "context" "sync" + "sync/atomic" "time" ) @@ -98,7 +99,11 @@ func (a *adapter) Update(key, cmd string, val ValkeyMessage) (sxat int64) { sxat = flight.xat val.setExpireAt(sxat) } - val.stash = &cacheStash{} + if val.stash == nil { + val.stash = &atomic.Pointer[any]{} + } else { + val.stash.Store(nil) + } a.store.Set(key+cmd, val) flight.set(val, nil) entries[cmd] = nil diff --git a/lru.go b/lru.go index fbc0078..8590ced 100644 --- a/lru.go +++ b/lru.go @@ -115,6 +115,9 @@ func (c *lru) Flight(key, cmd string, ttl time.Duration, now time.Time) (v Valke v = e.val c.list.MoveToBack(ele) ce = e + if v.stash == nil { + v.stash = &atomic.Pointer[any]{} + } goto ret } else { c.list.Remove(ele) @@ -231,7 +234,11 @@ func (c *lru) Update(key, cmd string, value ValkeyMessage) (pxat int64) { pxat = cpttl value.setExpireAt(pxat) } - value.stash = &cacheStash{} + if value.stash == nil { + value.stash = &atomic.Pointer[any]{} + } else { + value.stash.Store(nil) + } e.val = value e.size = entryBaseSize + 2*(len(key)+len(cmd)) + value.approximateSize() c.size += e.size diff --git a/message.go b/message.go index a76f225..6131a09 100644 --- a/message.go +++ b/message.go @@ -588,7 +588,7 @@ func (r *prettyValkeyResult) MarshalJSON() ([]byte, error) { // ValkeyMessage is a valkey response message, it may be a nil response type ValkeyMessage struct { attrs *ValkeyMessage - stash *cacheStash + stash *atomic.Pointer[any] bytes *byte array *ValkeyMessage @@ -1598,17 +1598,17 @@ func (m *ValkeyMessage) CachePXAT() int64 { // DoMultiCache. func (m *ValkeyMessage) CachePut(val any) { if m.stash != nil { - m.stash.val.Store(&val) + m.stash.Store(&val) } } // CacheGet retrieves the user-provided value previously stored with CachePut. // It returns nil if no value has been stored or if the message is not from the // client side cache. -func (m *ValkeyMessage) CacheGet() any { +func (m *ValkeyMessage) CacheGet() *any { if m.stash != nil { - if p := m.stash.val.Load(); p != nil { - return *p + if p := m.stash.Load(); p != nil { + return p } } return nil