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

Go: Implement Random Key #3358

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Go: Add `FLUSHALL` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
* Go: Add `FLUSHDB` ([#3117](https://github.com/valkey-io/valkey-glide/pull/3117))
* Go: Add password update api ([#3346](https://github.com/valkey-io/valkey-glide/pull/3346))
* Go: Add `Random Key` ([#3358](https://github.com/valkey-io/valkey-glide/pull/3358))

#### Breaking Changes

Expand Down
4 changes: 4 additions & 0 deletions go/api/generic_cluster_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ type GenericClusterCommands interface {
cursor options.ClusterScanCursor,
opts options.ClusterScanOptions,
) (options.ClusterScanCursor, []string, error)

RandomKey() (string, error)

RandomKeyWithRoute(opts options.RouteOption) (string, error)
}
30 changes: 30 additions & 0 deletions go/api/generic_cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package api
import (
"fmt"

"github.com/google/uuid"
"github.com/valkey-io/valkey-glide/go/api/config"
"github.com/valkey-io/valkey-glide/go/api/options"
)
Expand Down Expand Up @@ -212,3 +213,32 @@ func ExampleGlideClusterClient_ScanWithOptions_type() {

// Output: [someKey]
}

func ExampleGlideClusterClient_RandomKey() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
key := uuid.New().String()
client.Set(key, "Hello")
result, err := client.RandomKey()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}

fmt.Println(len(result) > 0)

// Output: true
}

func ExampleGlideClusterClient_RandomKeyWithRoute() {
var client *GlideClusterClient = getExampleGlideClusterClient() // example helper function
options := options.RouteOption{Route: nil}
key := uuid.New().String()
client.Set(key, "Hello")
result, err := client.RandomKeyWithRoute(options)
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}

fmt.Println(len(result) > 0)

// Output: true
}
2 changes: 2 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ package api
// [valkey.io]: https://valkey.io/commands/#generic
type GenericCommands interface {
CustomCommand(args []string) (interface{}, error)

RandomKey() (string, error)
}
15 changes: 15 additions & 0 deletions go/api/generic_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package api

import (
"fmt"

"github.com/google/uuid"
)

func ExampleGlideClient_CustomCommand() {
Expand All @@ -16,3 +18,16 @@ func ExampleGlideClient_CustomCommand() {

// Output: PONG
}

func ExampleGlideClient_RandomKey() {
var client *GlideClient = getExampleGlideClient() // example helper function
key := uuid.New().String()
client.Set(key, "Hello")
result, err := client.RandomKey()
if err != nil {
fmt.Println("Glide example failed with an error: ", err)
}
fmt.Println(len(result) > 0)

// Output: true
}
15 changes: 15 additions & 0 deletions go/api/glide_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,18 @@ func (client *GlideClient) FlushDBWithOptions(mode options.FlushMode) (string, e
}
return handleStringResponse(result)
}

// Returns a random existing key name from the currently selected database.
//
// Return value:
//
// A random existing key name from the currently selected database.
//
// [valkey.io]: https://valkey.io/commands/randomkey/
func (client *GlideClient) RandomKey() (string, error) {
result, err := client.executeCommand(C.RandomKey, []string{})
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(result)
}
37 changes: 37 additions & 0 deletions go/api/glide_cluster_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -544,3 +544,40 @@ func (client *GlideClusterClient) ScanWithOptions(
nextCursor, keys, err := handleScanResponse(response)
return *options.NewClusterScanCursorWithId(nextCursor), keys, err
}

// Returns a random key.
//
// Return value:
//
// A random key from the database.
//
// [valkey.io]: https://valkey.io/commands/randomkey/
func (client *GlideClusterClient) RandomKey() (string, error) {
result, err := client.executeCommand(C.RandomKey, []string{})
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(result)
}

// Returns a random key.
//
// Parameters:
//
// opts - specifies the routing configuration for the command.
//
// The client will route the command to the nodes defined by route,
// and will return the first successful result.
//
// Return value:
//
// A random key from the database.
//
// [valkey.io]: https://valkey.io/commands/randomkey/
func (client *GlideClusterClient) RandomKeyWithRoute(opts options.RouteOption) (string, error) {
result, err := client.executeCommandWithRoute(C.RandomKey, []string{}, opts.Route)
if err != nil {
return DefaultStringResponse, err
}
return handleStringResponse(result)
}
11 changes: 11 additions & 0 deletions go/integTest/cluster_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,14 @@ func (suite *GlideTestSuite) TestUpdateConnectionPasswordCluster_ImmediateAuthWr
_, err = adminClient.CustomCommand([]string{"CONFIG", "SET", "requirepass", ""})
assert.NoError(suite.T(), err)
}

func (suite *GlideTestSuite) TestRandomKeyWithRoute() {
client := suite.defaultClusterClient()
// Test 1: Check if Echo command return the message
t := suite.T()
route := config.Route(config.RandomRoute)
options := options.RouteOption{Route: route}
result, err := client.RandomKeyWithRoute(options)
assert.NoError(t, err)
assert.NotNil(t, result)
}
9 changes: 9 additions & 0 deletions go/integTest/standalone_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,3 +789,12 @@ func (suite *GlideTestSuite) TestUpdateConnectionPassword_ImmediateAuthWrongPass
_, err = adminClient.ConfigSet(map[string]string{"requirepass": ""})
assert.NoError(suite.T(), err)
}

func (suite *GlideTestSuite) TestRandomKey() {
client := suite.defaultClient()
// Test 1: Check if Echo command return the message
t := suite.T()
result, err := client.RandomKey()
assert.Nil(t, err)
assert.NotNil(t, result)
}
Loading