forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/redis/go-redis/v9/osscluster_commands.go

192 lines
2.4 KiB
Go
Raw Normal View History

2024-02-18 10:42:21 +00:00
package redis
import (
"context"
"sync"
"sync/atomic"
)
func (c *ClusterClient) DBSize(ctx context.Context) *IntCmd {
2024-02-18 10:42:21 +00:00
cmd := NewIntCmd(ctx, "dbsize")
2024-02-18 10:42:21 +00:00
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
2024-02-18 10:42:21 +00:00
var size int64
2024-02-18 10:42:21 +00:00
err := c.ForEachMaster(ctx, func(ctx context.Context, master *Client) error {
2024-02-18 10:42:21 +00:00
n, err := master.DBSize(ctx).Result()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
atomic.AddInt64(&size, n)
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
cmd.SetErr(err)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cmd.val = size
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c *ClusterClient) ScriptLoad(ctx context.Context, script string) *StringCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStringCmd(ctx, "script", "load", script)
2024-02-18 10:42:21 +00:00
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
2024-02-18 10:42:21 +00:00
var mu sync.Mutex
2024-02-18 10:42:21 +00:00
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
2024-02-18 10:42:21 +00:00
val, err := shard.ScriptLoad(ctx, script).Result()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mu.Lock()
2024-02-18 10:42:21 +00:00
if cmd.Val() == "" {
2024-02-18 10:42:21 +00:00
cmd.val = val
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mu.Unlock()
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
cmd.SetErr(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c *ClusterClient) ScriptFlush(ctx context.Context) *StatusCmd {
2024-02-18 10:42:21 +00:00
cmd := NewStatusCmd(ctx, "script", "flush")
2024-02-18 10:42:21 +00:00
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
2024-02-18 10:42:21 +00:00
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
2024-02-18 10:42:21 +00:00
return shard.ScriptFlush(ctx).Err()
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
cmd.SetErr(err)
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}
func (c *ClusterClient) ScriptExists(ctx context.Context, hashes ...string) *BoolSliceCmd {
2024-02-18 10:42:21 +00:00
args := make([]interface{}, 2+len(hashes))
2024-02-18 10:42:21 +00:00
args[0] = "script"
2024-02-18 10:42:21 +00:00
args[1] = "exists"
2024-02-18 10:42:21 +00:00
for i, hash := range hashes {
2024-02-18 10:42:21 +00:00
args[2+i] = hash
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
cmd := NewBoolSliceCmd(ctx, args...)
result := make([]bool, len(hashes))
2024-02-18 10:42:21 +00:00
for i := range result {
2024-02-18 10:42:21 +00:00
result[i] = true
2024-02-18 10:42:21 +00:00
}
_ = c.withProcessHook(ctx, cmd, func(ctx context.Context, _ Cmder) error {
2024-02-18 10:42:21 +00:00
var mu sync.Mutex
2024-02-18 10:42:21 +00:00
err := c.ForEachShard(ctx, func(ctx context.Context, shard *Client) error {
2024-02-18 10:42:21 +00:00
val, err := shard.ScriptExists(ctx, hashes...).Result()
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
return err
2024-02-18 10:42:21 +00:00
}
mu.Lock()
2024-02-18 10:42:21 +00:00
for i, v := range val {
2024-02-18 10:42:21 +00:00
result[i] = result[i] && v
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
mu.Unlock()
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
if err != nil {
2024-02-18 10:42:21 +00:00
cmd.SetErr(err)
2024-02-18 10:42:21 +00:00
} else {
2024-02-18 10:42:21 +00:00
cmd.val = result
2024-02-18 10:42:21 +00:00
}
2024-02-18 10:42:21 +00:00
return nil
2024-02-18 10:42:21 +00:00
})
2024-02-18 10:42:21 +00:00
return cmd
2024-02-18 10:42:21 +00:00
}