Skip to content

Commit

Permalink
Go: Implement Persist Command (#2829)
Browse files Browse the repository at this point in the history
Implement Persist Command

Signed-off-by: EdricCua <[email protected]>
  • Loading branch information
EdricCua authored Jan 8, 2025
1 parent de9526d commit 4341d66
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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()
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())
})
}

0 comments on commit 4341d66

Please sign in to comment.