Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package valkey
import (
"context"
"sync"
"sync/atomic"
"time"
)

Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down