From f29a5e909041bbdf79b9c4a2964054bc0d273bbf Mon Sep 17 00:00:00 2001 From: EdricCua Date: Tue, 17 Dec 2024 19:48:45 +0000 Subject: [PATCH] Implement Persist Command Signed-off-by: EdricCua --- go/api/base_client.go | 8 ++++++++ go/api/generic_commands.go | 19 +++++++++++++++++++ go/integTest/shared_commands_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/go/api/base_client.go b/go/api/base_client.go index ba83dedd95..fc89fd90dd 100644 --- a/go/api/base_client.go +++ b/go/api/base_client.go @@ -1155,3 +1155,11 @@ func (client *baseClient) Touch(keys []string) (Result[int64], error) { return handleLongResponse(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 5978ce0c25..dfad7be75f 100644 --- a/go/api/generic_commands.go +++ b/go/api/generic_commands.go @@ -385,4 +385,23 @@ type GenericBaseCommands interface { // // [valkey.io]: Https://valkey.io/commands/type/ Type(key string) (Result[string], error) + + // Removes the existing timeout on key, turning the key from volatile + // (a key with an expire set) to persis tent (a key that will never expire as no timeout is associated). + // + // Parameters: + // 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 46e4c86133..d621c3b024 100644 --- a/go/integTest/shared_commands_test.go +++ b/go/integTest/shared_commands_test.go @@ -3504,3 +3504,30 @@ func (suite *GlideTestSuite) TestUnlink() { assert.Equal(suite.T(), int64(0), resultInvalidKey.Value(), "The unlink should be 0") }) } + +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() + suite.verifyOK(client.Set(keyName, initialValue)) + resultExpire, err := client.Expire(keyName, 300) + assert.Nil(suite.T(), err) + assert.True(suite.T(), resultExpire.Value()) + resultPersist, err := client.Persist(keyName) + assert.Nil(suite.T(), err) + assert.True(suite.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(suite.T(), err) + assert.False(suite.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(suite.T(), err) + assert.False(suite.T(), resultInvalidKey.Value()) + }) +}