feat: added DECRBY command

This commit is contained in:
Sahil
2024-06-25 02:11:30 +05:30
parent 13d86dfe66
commit 876ac0b4ba
5 changed files with 282 additions and 7 deletions

View File

@@ -461,3 +461,28 @@ func (server *EchoVault) Decr(key string) (int, error) {
// Parse the integer response
return internal.ParseIntegerResponse(b)
}
// DecrBy decrements the integer value of the specified key by the given increment.
// If the key does not exist, it is created with an initial value of 0 before decrementing.
// If the value stored at the key is not an integer, an error is returned.
//
// Parameters:
// - `key` (string): The key whose value is to be decremented.
// - `increment` (int): The amount by which to decrement the key's value. This can be a positive or negative integer.
//
// Returns:
// - (int): The new value of the key after the decrement operation.
func (server *EchoVault) DecrBy(key string, value string) (int, error) {
// Construct the command
cmd := []string{"DECRBY", key, value}
// Execute the command
b, err := server.handleCommand(server.context, internal.EncodeCommand(cmd), nil, false, true)
if err != nil {
return 0, err
}
// Parse the integer response
return internal.ParseIntegerResponse(b)
}