Skip to content

Commit

Permalink
feat: rename to valkey-go
Browse files Browse the repository at this point in the history
  • Loading branch information
rueian committed Apr 19, 2024
1 parent cac87e5 commit 4e1b801
Show file tree
Hide file tree
Showing 115 changed files with 3,963 additions and 3,958 deletions.
2 changes: 1 addition & 1 deletion NOTICE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
rueidis
valkey-go
Copyright 2024 Rueian (https://github.com/rueian)
174 changes: 87 additions & 87 deletions README.md

Large diffs are not rendered by default.

20 changes: 10 additions & 10 deletions binary.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rueidis
package valkey

import (
"encoding/binary"
Expand All @@ -8,20 +8,20 @@ import (
)

// BinaryString convert the provided []byte into a string without copy. It does what strings.Builder.String() does.
// Redis Strings are binary safe, this means that it is safe to store any []byte into Redis directly.
// Users can use this BinaryString helper to insert a []byte as the part of redis command. For example:
// Valkey Strings are binary safe, this means that it is safe to store any []byte into Valkey directly.
// Users can use this BinaryString helper to insert a []byte as the part of valkey command. For example:
//
// client.B().Set().Key(rueidis.BinaryString([]byte{0})).Value(rueidis.BinaryString([]byte{0})).Build()
// client.B().Set().Key(valkey.BinaryString([]byte{0})).Value(valkey.BinaryString([]byte{0})).Build()
//
// To read back the []byte of the string returned from the Redis, it is recommended to use the RedisMessage.AsReader.
// To read back the []byte of the string returned from the Valkey, it is recommended to use the ValkeyMessage.AsReader.
func BinaryString(bs []byte) string {
return unsafe.String(unsafe.SliceData(bs), len(bs))
}

// VectorString32 convert the provided []float32 into a string. Users can use this to build vector search queries:
//
// client.B().FtSearch().Index("idx").Query("*=>[KNN 5 @vec $V]").
// Params().Nargs(2).NameValue().NameValue("V", rueidis.VectorString32([]float32{1})).
// Params().Nargs(2).NameValue().NameValue("V", valkey.VectorString32([]float32{1})).
// Dialect(2).Build()
func VectorString32(v []float32) string {
b := make([]byte, len(v)*4)
Expand All @@ -32,7 +32,7 @@ func VectorString32(v []float32) string {
return BinaryString(b)
}

// ToVector32 reverts VectorString32. User can use this to convert redis response back to []float32.
// ToVector32 reverts VectorString32. User can use this to convert valkey response back to []float32.
func ToVector32(s string) []float32 {
bs := unsafe.Slice(unsafe.StringData(s), len(s))
vs := make([]float32, 0, len(bs)/4)
Expand All @@ -45,7 +45,7 @@ func ToVector32(s string) []float32 {
// VectorString64 convert the provided []float64 into a string. Users can use this to build vector search queries:
//
// client.B().FtSearch().Index("idx").Query("*=>[KNN 5 @vec $V]").
// Params().Nargs(2).NameValue().NameValue("V", rueidis.VectorString64([]float64{1})).
// Params().Nargs(2).NameValue().NameValue("V", valkey.VectorString64([]float64{1})).
// Dialect(2).Build()
func VectorString64(v []float64) string {
b := make([]byte, len(v)*8)
Expand All @@ -56,7 +56,7 @@ func VectorString64(v []float64) string {
return BinaryString(b)
}

// ToVector64 reverts VectorString64. User can use this to convert redis response back to []float64.
// ToVector64 reverts VectorString64. User can use this to convert valkey response back to []float64.
func ToVector64(s string) []float64 {
bs := unsafe.Slice(unsafe.StringData(s), len(s))
vs := make([]float64, 0, len(bs)/8)
Expand All @@ -69,7 +69,7 @@ func ToVector64(s string) []float64 {
// JSON convert the provided parameter into a JSON string. Users can use this JSON helper to work with RedisJSON commands.
// For example:
//
// client.B().JsonSet().Key("a").Path("$.myField").Value(rueidis.JSON("str")).Build()
// client.B().JsonSet().Key("a").Path("$.myField").Value(valkey.JSON("str")).Build()
func JSON(in any) string {
bs, err := json.Marshal(in)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions binary_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rueidis
package valkey

import (
"encoding/json"
Expand Down Expand Up @@ -30,7 +30,6 @@ func TestJSONPanic(t *testing.T) {
JSON(a)
}


func TestVectorString32(t *testing.T) {
for _, test := range [][]float32{
{},
Expand Down
52 changes: 26 additions & 26 deletions cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package rueidis
package valkey

import (
"context"
Expand All @@ -11,7 +11,7 @@ type NewCacheStoreFn func(CacheStoreOption) CacheStore

// CacheStoreOption will be passed to NewCacheStoreFn
type CacheStoreOption struct {
// CacheSizeEachConn is redis client side cache size that bind to each TCP connection to a single redis instance.
// CacheSizeEachConn is valkey client side cache size that bind to each TCP connection to a single valkey instance.
// The default is DefaultCacheBytes.
CacheSizeEachConn int
}
Expand All @@ -21,35 +21,35 @@ type CacheStoreOption struct {
type CacheStore interface {
// Flight is called when DoCache and DoMultiCache, with the requested client side ttl and the current time.
// It should look up the store in single-flight manner and return one of the following three combinations:
// Case 1: (empty RedisMessage, nil CacheEntry) <- when cache missed, and rueidis will send the request to redis.
// Case 2: (empty RedisMessage, non-nil CacheEntry) <- when cache missed, and rueidis will use CacheEntry.Wait to wait for response.
// Case 3: (non-empty RedisMessage, nil CacheEntry) <- when cache hit
Flight(key, cmd string, ttl time.Duration, now time.Time) (v RedisMessage, e CacheEntry)
// Update is called when receiving the response of the request sent by the above Flight Case 1 from redis.
// Case 1: (empty ValkeyMessage, nil CacheEntry) <- when cache missed, and valkey will send the request to valkey.
// Case 2: (empty ValkeyMessage, non-nil CacheEntry) <- when cache missed, and valkey will use CacheEntry.Wait to wait for response.
// Case 3: (non-empty ValkeyMessage, nil CacheEntry) <- when cache hit
Flight(key, cmd string, ttl time.Duration, now time.Time) (v ValkeyMessage, e CacheEntry)
// Update is called when receiving the response of the request sent by the above Flight Case 1 from valkey.
// It should not only update the store but also deliver the response to all CacheEntry.Wait and return a desired client side PXAT of the response.
// Note that the server side expire time can be retrieved from RedisMessage.CachePXAT.
Update(key, cmd string, val RedisMessage) (pxat int64)
// Note that the server side expire time can be retrieved from ValkeyMessage.CachePXAT.
Update(key, cmd string, val ValkeyMessage) (pxat int64)
// Cancel is called when the request sent by the above Flight Case 1 failed.
// It should not only deliver the error to all CacheEntry.Wait but also remove the CacheEntry from the store.
Cancel(key, cmd string, err error)
// Delete is called when receiving invalidation notifications from redis.
// Delete is called when receiving invalidation notifications from valkey.
// If the keys is nil then it should delete all non-pending cached entries under all keys.
// If the keys is not nil then it should delete all non-pending cached entries under those keys.
Delete(keys []RedisMessage)
// Close is called when connection between redis is broken.
Delete(keys []ValkeyMessage)
// Close is called when connection between valkey is broken.
// It should flush all cached entries and deliver the error to all pending CacheEntry.Wait.
Close(err error)
}

// CacheEntry should be used to wait for single-flight response when cache missed.
type CacheEntry interface {
Wait(ctx context.Context) (RedisMessage, error)
Wait(ctx context.Context) (ValkeyMessage, error)
}

// SimpleCache is an alternative interface should be paired with NewSimpleCacheAdapter to construct a CacheStore
type SimpleCache interface {
Get(key string) RedisMessage
Set(key string, val RedisMessage)
Get(key string) ValkeyMessage
Set(key string, val ValkeyMessage)
Del(key string)
Flush()
}
Expand All @@ -65,7 +65,7 @@ type adapter struct {
mu sync.RWMutex
}

func (a *adapter) Flight(key, cmd string, ttl time.Duration, now time.Time) (RedisMessage, CacheEntry) {
func (a *adapter) Flight(key, cmd string, ttl time.Duration, now time.Time) (ValkeyMessage, CacheEntry) {
a.mu.RLock()
if v := a.store.Get(key + cmd); v.typ != 0 && v.relativePTTL(now) > 0 {
a.mu.RUnlock()
Expand All @@ -74,7 +74,7 @@ func (a *adapter) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
flight := a.flights[key][cmd]
a.mu.RUnlock()
if flight != nil {
return RedisMessage{}, flight
return ValkeyMessage{}, flight
}
a.mu.Lock()
entries := a.flights[key]
Expand All @@ -86,10 +86,10 @@ func (a *adapter) Flight(key, cmd string, ttl time.Duration, now time.Time) (Red
entries[cmd] = &adapterEntry{ch: make(chan struct{}), xat: now.Add(ttl).UnixMilli()}
}
a.mu.Unlock()
return RedisMessage{}, flight
return ValkeyMessage{}, flight
}

func (a *adapter) Update(key, cmd string, val RedisMessage) (sxat int64) {
func (a *adapter) Update(key, cmd string, val ValkeyMessage) (sxat int64) {
a.mu.Lock()
entries := a.flights[key]
if flight, ok := entries[cmd].(*adapterEntry); ok {
Expand All @@ -110,7 +110,7 @@ func (a *adapter) Cancel(key, cmd string, err error) {
a.mu.Lock()
entries := a.flights[key]
if flight, ok := entries[cmd].(*adapterEntry); ok {
flight.set(RedisMessage{}, err)
flight.set(ValkeyMessage{}, err)
entries[cmd] = nil
}
a.mu.Unlock()
Expand All @@ -129,7 +129,7 @@ func (a *adapter) del(key string) {
}
}

func (a *adapter) Delete(keys []RedisMessage) {
func (a *adapter) Delete(keys []ValkeyMessage) {
a.mu.Lock()
if keys == nil {
for key := range a.flights {
Expand All @@ -152,7 +152,7 @@ func (a *adapter) Close(err error) {
for _, entries := range flights {
for _, e := range entries {
if e != nil {
e.(*adapterEntry).set(RedisMessage{}, err)
e.(*adapterEntry).set(ValkeyMessage{}, err)
}
}
}
Expand All @@ -161,19 +161,19 @@ func (a *adapter) Close(err error) {
type adapterEntry struct {
err error
ch chan struct{}
val RedisMessage
val ValkeyMessage
xat int64
}

func (a *adapterEntry) set(val RedisMessage, err error) {
func (a *adapterEntry) set(val ValkeyMessage, err error) {
a.err, a.val = err, val
close(a.ch)
}

func (a *adapterEntry) Wait(ctx context.Context) (RedisMessage, error) {
func (a *adapterEntry) Wait(ctx context.Context) (ValkeyMessage, error) {
select {
case <-ctx.Done():
return RedisMessage{}, ctx.Err()
return ValkeyMessage{}, ctx.Err()
case <-a.ch:
return a.val, a.err
}
Expand Down
Loading

0 comments on commit 4e1b801

Please sign in to comment.