Skip to content

Commit

Permalink
Go: Implement Type, Touch and Unlink commands (valkey-io#2756)
Browse files Browse the repository at this point in the history
* Go: Implement Type, Touch and Unlink command
Signed-off-by: EdricCua <[email protected]>
  • Loading branch information
EdricCua authored Dec 17, 2024
1 parent daa5af2 commit 2882c81
Show file tree
Hide file tree
Showing 3 changed files with 285 additions and 131 deletions.
26 changes: 26 additions & 0 deletions go/api/base_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,29 @@ func (client *baseClient) PTTL(key string) (Result[int64], error) {

return handleLongResponse(result)
}

func (client *baseClient) Unlink(keys []string) (Result[int64], error) {
result, err := client.executeCommand(C.Unlink, keys)
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}

func (client *baseClient) Type(key string) (Result[string], error) {
result, err := client.executeCommand(C.Type, []string{key})
if err != nil {
return CreateNilStringResult(), err
}
return handleStringOrNullResponse(result)
}

func (client *baseClient) Touch(keys []string) (Result[int64], error) {
result, err := client.executeCommand(C.Touch, keys)
if err != nil {
return CreateNilInt64Result(), err
}

return handleLongResponse(result)
}
72 changes: 72 additions & 0 deletions go/api/generic_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,4 +313,76 @@ type GenericBaseCommands interface {
//
// [valkey.io]: https://valkey.io/commands/pttl/
PTTL(key string) (Result[int64], error)

// Unlink (delete) multiple keys from the database. A key is ignored if it does not exist.
// This command, similar to Del However, this command does not block the server
//
// Note:
// In cluster mode, if keys in keys map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
// while others did not. If this behavior impacts your application logic, consider splitting
// the request into sub-requests per slot to ensure atomicity.
//
// Parameters:
// keys - One or more keys to unlink.
//
// Return value:
// Return the number of keys that were unlinked.
//
// Example:
// result, err := client.Unlink([]string{"key1", "key2", "key3"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: 3
//
// [valkey.io]: Https://valkey.io/commands/unlink/
Unlink(keys []string) (Result[int64], error)

// Alters the last access time of a key(s). A key is ignored if it does not exist.
//
// Note:
// In cluster mode, if keys in keys map to different hash slots, the command
// will be split across these slots and executed separately for each. This means the command
// is atomic only at the slot level. If one or more slot-specific requests fail, the entire
// call will return the first encountered error, even though some requests may have succeeded
// while others did not. If this behavior impacts your application logic, consider splitting
// the request into sub-requests per slot to ensure atomicity.
//
// Parameters:
// The keys to update last access time.
//
// Return value:
// The number of keys that were updated.
//
// Example:
// result, err := client.Touch([]string{"key1", "key2", "key3"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: 3
//
// [valkey.io]: Https://valkey.io/commands/touch/
Touch(keys []string) (Result[int64], error)

// Type returns the string representation of the type of the value stored at key.
// The different types that can be returned are: string, list, set, zset, hash and stream.
//
// Parameters:
// key - string
//
// Return value:
// If the key exists, the type of the stored value is returned. Otherwise, a none" string is returned.
//
// Example:
// result, err := client.Type([]string{"key"})
// if err != nil {
// // handle error
// }
// fmt.Println(result.Value()) // Output: string
//
// [valkey.io]: Https://valkey.io/commands/type/
Type(key string) (Result[string], error)
}
Loading

0 comments on commit 2882c81

Please sign in to comment.