diff --git a/go/api/base_client.go b/go/api/base_client.go index 035ff774ba..b954ddabb3 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -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) +} diff --git a/go/api/generic_commands.go b/go/api/generic_commands.go index c583dfe31b..e562c67c4c 100644 --- a/go/api/generic_commands.go +++ b/go/api/generic_commands.go @@ -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) } diff --git a/go/integTest/shared_commands_test.go b/go/integTest/shared_commands_test.go index a43967bf1f..df0568e3f4 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -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()) + }) +}