Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add redis ExistsMany method #3769

Merged
merged 1 commit into from
Dec 16, 2023
Merged
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
27 changes: 27 additions & 0 deletions core/stores/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,33 @@
return
}

// ExistsMany is the implementation of redis exists command.
// checks the existence of multiple keys in Redis using the EXISTS command.
func (s *Redis) ExistsMany(keys ...string) (int64, error) {
return s.ExistsManyCtx(context.Background(), keys...)
}

// ExistsManyCtx is the implementation of redis exists command.
// checks the existence of multiple keys in Redis using the EXISTS command.
func (s *Redis) ExistsManyCtx(ctx context.Context, keys ...string) (val int64, err error) {
err = s.brk.DoWithAcceptable(func() error {
conn, err := getRedis(s)
if err != nil {
return err
}

v, err := conn.Exists(ctx, keys...).Result()
if err != nil {
return err
}

Check warning on line 488 in core/stores/redis/redis.go

View check run for this annotation

Codecov / codecov/patch

core/stores/redis/redis.go#L487-L488

Added lines #L487 - L488 were not covered by tests

val = v
return nil
}, acceptable)

return
}

// Expire is the implementation of redis expire command.
func (s *Redis) Expire(key string, seconds int) error {
return s.ExpireCtx(context.Background(), key, seconds)
Expand Down
30 changes: 30 additions & 0 deletions core/stores/redis/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,36 @@ func TestRedisTLS_Exists(t *testing.T) {
})
}

func TestRedis_ExistsMany(t *testing.T) {
runOnRedis(t, func(client *Redis) {
// Attempt to create a new Redis instance with an incorrect type and call ExistsMany
_, err := New(client.Addr, badType()).ExistsMany("key1", "key2")
assert.NotNil(t, err)

// Check if key1 and key2 exist, expecting that they do not
val, err := client.ExistsMany("key1", "key2")
assert.Nil(t, err)
assert.Equal(t, int64(0), val)

// Set the value for key1 and check if key1 exists
assert.Nil(t, client.Set("key1", "value1"))
val, err = client.ExistsMany("key1")
assert.Nil(t, err)
assert.Equal(t, int64(1), val)

// Set the value for key2 and check if key1 and key2 exist
assert.Nil(t, client.Set("key2", "value2"))
val, err = client.ExistsMany("key1", "key2")
assert.Nil(t, err)
assert.Equal(t, int64(2), val)

// Check if key1, key2, and a non-existent key3 exist, expecting that key1 and key2 do
val, err = client.ExistsMany("key1", "key2", "key3")
assert.Nil(t, err)
assert.Equal(t, int64(2), val)
})
}

func TestRedis_Eval(t *testing.T) {
runOnRedis(t, func(client *Redis) {
_, err := New(client.Addr, badType()).Eval(`redis.call("EXISTS", KEYS[1])`, []string{"notexist"})
Expand Down