mirror of
https://github.com/nalgeon/redka.git
synced 2025-10-16 04:50:40 +08:00
40 lines
825 B
Go
40 lines
825 B
Go
package zset
|
|
|
|
import (
|
|
"github.com/nalgeon/redka/internal/parser"
|
|
"github.com/nalgeon/redka/internal/redis"
|
|
)
|
|
|
|
// Returns the count of members in a sorted set that have scores within a range.
|
|
// ZCOUNT key min max
|
|
// https://redis.io/commands/zcount
|
|
type ZCount struct {
|
|
redis.BaseCmd
|
|
Key string
|
|
Min float64
|
|
Max float64
|
|
}
|
|
|
|
func ParseZCount(b redis.BaseCmd) (*ZCount, error) {
|
|
cmd := &ZCount{BaseCmd: b}
|
|
err := parser.New(
|
|
parser.String(&cmd.Key),
|
|
parser.Float(&cmd.Min),
|
|
parser.Float(&cmd.Max),
|
|
).Required(3).Run(cmd.Args())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return cmd, nil
|
|
}
|
|
|
|
func (cmd *ZCount) Run(w redis.Writer, red redis.Redka) (any, error) {
|
|
n, err := red.ZSet().Count(cmd.Key, cmd.Min, cmd.Max)
|
|
if err != nil {
|
|
w.WriteError(cmd.Error(err))
|
|
return nil, err
|
|
}
|
|
w.WriteInt(n)
|
|
return n, nil
|
|
}
|