mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-05 07:56:49 +08:00
39 lines
881 B
Go
39 lines
881 B
Go
package string
|
|
|
|
import (
|
|
"github.com/nalgeon/redka/internal/parser"
|
|
"github.com/nalgeon/redka/internal/redis"
|
|
)
|
|
|
|
// Increment the floating point value of a key by a number.
|
|
// Uses 0 as initial value if the key doesn't exist.
|
|
// INCRBYFLOAT key increment
|
|
// https://redis.io/commands/incrbyfloat
|
|
type IncrByFloat struct {
|
|
redis.BaseCmd
|
|
key string
|
|
delta float64
|
|
}
|
|
|
|
func ParseIncrByFloat(b redis.BaseCmd) (IncrByFloat, error) {
|
|
cmd := IncrByFloat{BaseCmd: b}
|
|
err := parser.New(
|
|
parser.String(&cmd.key),
|
|
parser.Float(&cmd.delta),
|
|
).Required(2).Run(cmd.Args())
|
|
if err != nil {
|
|
return IncrByFloat{}, err
|
|
}
|
|
return cmd, nil
|
|
}
|
|
|
|
func (cmd IncrByFloat) Run(w redis.Writer, red redis.Redka) (any, error) {
|
|
val, err := red.Str().IncrFloat(cmd.key, cmd.delta)
|
|
if err != nil {
|
|
w.WriteError(cmd.Error(err))
|
|
return nil, err
|
|
}
|
|
redis.WriteFloat(w, val)
|
|
return val, nil
|
|
}
|