mirror of
https://github.com/hanzokv/lock.git
synced 2026-10-30 15:07:01 +00:00
github.com/hanzokv/go/v9 v9.22.0 renamed its Go package from "redis" to "kv"; the import path is unchanged. A lock library is typed against the client it wraps -- the adapter's NewPool takes the client's UniversalClient and compares against the client's Nil sentinel -- so the qualifier change is load bearing, not cosmetic. Client-driven: - go.mod: github.com/hanzokv/go/v9 v9.21.1 -> v9.22.0 - the adapter for our client speaks kv.UniversalClient / kv.Nil, imported unaliased as in hanzokv/go's own sources Our own vocabulary follows: - redis/ -> kv/, package redis -> package kv. This is redsync's own Pool/Conn/Script abstraction over a KV backend, not anyone's product. - redis/goredis/v9 -> kv/kvgo (package kvgo). That adapter wraps hanzokv/go, not go-redis; the old name said the wrong thing. Its example moves with it: examples/goredis/v9 -> examples/kvgo. - import alias redsyncredis -> redsynckv - RedisError -> KVError (and its tests) - comments and README speak kv Deliberately NOT renamed, because they name foreign things: - the redigo, rueidis, valkeygo and go-redis v6/v7/v8 adapters, which target genuinely different clients, keep their names and their own "redis." qualifiers (redigo's redis.Conn, go-redis's redis.Nil) - the server-side Lua in scripts.go: redis.call is the store's scripting API. Rewriting it would change the script source and therefore the SHA1 hashes pinned in scripts.go, which TestScripts_Hash verifies against a live server. - upstream links (redis.io distlock post, redigo attribution) Import paths move from .../v4/redis/... to .../v4/kv/...; the module major stays v4 by policy. Verified with a local redis-server: go build ./..., go vet ./..., go test -count=1 ./... and go test -race -count=1 ./... all pass, TestScripts_Hash included. Co-authored-by: Hanzo Dev <[email protected]>
88 lines
1.9 KiB
Go
88 lines
1.9 KiB
Go
package kvgo
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/hanzokv/go/v9"
|
|
redsynckv "github.com/hanzokv/lock/v4/kv"
|
|
)
|
|
|
|
type pool struct {
|
|
delegate kv.UniversalClient
|
|
}
|
|
|
|
func (p *pool) Get(ctx context.Context) (redsynckv.Conn, error) {
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
return &conn{p.delegate, ctx}, nil
|
|
}
|
|
|
|
// NewPool returns a hanzokv/go based pool implementation.
|
|
func NewPool(delegate kv.UniversalClient) redsynckv.Pool {
|
|
return &pool{delegate}
|
|
}
|
|
|
|
type conn struct {
|
|
delegate kv.UniversalClient
|
|
ctx context.Context
|
|
}
|
|
|
|
func (c *conn) Get(name string) (string, error) {
|
|
value, err := c.delegate.Get(c.ctx, name).Result()
|
|
return value, noErrNil(err)
|
|
}
|
|
|
|
func (c *conn) Set(name string, value string) (bool, error) {
|
|
reply, err := c.delegate.Set(c.ctx, name, value, 0).Result()
|
|
return reply == "OK", err
|
|
}
|
|
|
|
func (c *conn) SetNX(name string, value string, expiry time.Duration) (bool, error) {
|
|
return c.delegate.SetNX(c.ctx, name, value, expiry).Result()
|
|
}
|
|
|
|
func (c *conn) PTTL(name string) (time.Duration, error) {
|
|
return c.delegate.PTTL(c.ctx, name).Result()
|
|
}
|
|
|
|
func (c *conn) Eval(script *redsynckv.Script, keysAndArgs ...any) (any, error) {
|
|
keys := make([]string, script.KeyCount)
|
|
args := keysAndArgs
|
|
|
|
if script.KeyCount > 0 {
|
|
for i := range script.KeyCount {
|
|
keys[i] = keysAndArgs[i].(string)
|
|
}
|
|
args = keysAndArgs[script.KeyCount:]
|
|
}
|
|
|
|
v, err := c.delegate.EvalSha(c.ctx, script.Hash, keys, args...).Result()
|
|
if err != nil && strings.Contains(err.Error(), "NOSCRIPT ") {
|
|
v, err = c.delegate.Eval(c.ctx, script.Src, keys, args...).Result()
|
|
}
|
|
return v, noErrNil(err)
|
|
}
|
|
|
|
func (c *conn) ScriptLoad(script *redsynckv.Script) error {
|
|
v, err := c.delegate.ScriptLoad(c.ctx, script.Src).Result()
|
|
if err == nil {
|
|
script.Hash = v
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (c *conn) Close() error {
|
|
// Not needed for this library
|
|
return nil
|
|
}
|
|
|
|
func noErrNil(err error) error {
|
|
if err == kv.Nil {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|