redis/rueidis — v1.0.79 snapshot¶
Pinned: v1.0.79 Source: tagged source
Client construction¶
import "github.com/redis/rueidis"
client, err := rueidis.NewClient(rueidis.ClientOption{
InitAddress: []string{"localhost:6379"},
Password: "",
SelectDB: 0,
})
defer client.Close()
Commands¶
// SET / GET
err := client.Do(ctx,
client.B().Set().Key("k").Value("v").Ex(60*time.Second).Build(),
).Error()
val, err := client.Do(ctx, client.B().Get().Key("k").Build()).ToString()
// Pipeline
cmds := []rueidis.Completed{
client.B().Set().Key("a").Value("1").Build(),
client.B().Set().Key("b").Value("2").Build(),
}
results := client.DoMulti(ctx, cmds...)
// Pub/Sub
err = client.Receive(
ctx,
client.B().Subscribe().Channel("chan").Build(),
func(msg rueidis.PubSubMessage) { handleMessage(msg.Message) },
)
Distributed lock (rueidislock)¶
import "github.com/redis/rueidis/rueidislock"
locker, err := rueidislock.NewLocker(rueidislock.LockerOption{
ClientOption: rueidis.ClientOption{InitAddress: []string{":6379"}},
})
ctx, cancel, err := locker.TryWithContext(ctx, "resource-name")
if err != nil {
return fmt.Errorf("acquire Redis lock: %w", err)
}
defer cancel()
All Redis calls use the caller's bounded ctx; do not replace it with
context.Background().
Changes since v1.0.78¶
NewClientno longer returns a non-nilClientwrapping a nil pointer when construction fails; checkerrbefore using the client.- Blocking commands built with
.UnblockOnCancel()sendCLIENT UNBLOCKon context cancellation and keep the connection. - New helpers:
MUnlink(slot-grouped unlink) andClusterScanner. rueidislockis unchanged;rueidislimitergained an opt-in GCRA algorithm.
golusoris usage¶
cache/redis/—rueidis.Client+rueidislock.Lockerprovided via fx.