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 Persist Command #2829

Merged
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
8 changes: 8 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1475,3 +1475,11 @@ func (client *baseClient) BZPopMin(keys []string, timeoutSecs float64) (Result[K

return handleKeyWithMemberAndScoreResponse(result)
}

func (client *baseClient) Persist(key string) (Result[bool], error) {
result, err := client.executeCommand(C.Persist, []string{key})
if err != nil {
return CreateNilBoolResult(), err
}
return handleBooleanResponse(result)
}
19 changes: 19 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -428,4 +428,23 @@ type GenericBaseCommands interface {
//
// [valkey.io]: https://valkey.io/commands/renamenx/
Renamenx(key string, newKey string) (Result[bool], error)

// Removes the existing timeout on key, turning the key from volatile
// (a key with an expire set) to persistent (a key that will never expire as no timeout is associated).
//
// Parameters:
// key - The key to remove the existing timeout on.
//
// Return value:
// false if key does not exist or does not have an associated timeout, true if the timeout has been removed.
//
// Example:
// result, err := client.Persist([]string{"key"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: true
//
// [valkey.io]: https://valkey.io/commands/persist/
Persist(key string) (Result[bool], error)
}
28 changes: 28 additions & 0 deletions go/integTest/shared_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4456,3 +4456,31 @@ func (suite *GlideTestSuite) TestZRem() {
assert.IsType(suite.T(), &api.RequestError{}, err)
})
}

func (suite *GlideTestSuite) TestPersist() {
suite.runWithDefaultClients(func(client api.BaseClient) {
// Test 1: Check if persist command removes the expiration time of a key.
keyName := "{keyName}" + uuid.NewString()
EdricCua marked this conversation as resolved.
Show resolved Hide resolved
t := suite.T()
suite.verifyOK(client.Set(keyName, initialValue))
resultExpire, err := client.Expire(keyName, 300)
assert.Nil(t, err)
assert.True(t, resultExpire.Value())
resultPersist, err := client.Persist(keyName)
assert.Nil(t, err)
assert.True(t, resultPersist.Value())

// Test 2: Check if persist command return false if key that doesnt have associated timeout.
keyNoExp := "{keyName}" + uuid.NewString()
suite.verifyOK(client.Set(keyNoExp, initialValue))
resultPersistNoExp, err := client.Persist(keyNoExp)
assert.Nil(t, err)
assert.False(t, resultPersistNoExp.Value())

// Test 3: Check if persist command return false if key not exist.
keyInvalid := "{invalidkey_forPersistTest}" + uuid.NewString()
resultInvalidKey, err := client.Persist(keyInvalid)
assert.Nil(t, err)
assert.False(t, resultInvalidKey.Value())
})
}
Loading