-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcmd_lrange.go
45 lines (40 loc) · 1000 Bytes
/
cmd_lrange.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package redis
import (
"fmt"
"github.com/redis-go/redcon"
"strconv"
)
func LRangeCommand(c *Client, cmd redcon.Command) {
if len(cmd.Args) < 3 {
c.Conn().WriteError(fmt.Sprintf(WrongNumOfArgsErr, "lrange"))
return
}
key := string(cmd.Args[1])
start, err := strconv.Atoi(string(cmd.Args[2]))
if err != nil {
c.Conn().WriteError(fmt.Sprintf("%s: %s", InvalidIntErr, err.Error()))
return
}
end, err := strconv.Atoi(string(cmd.Args[3]))
if err != nil {
c.Conn().WriteError(fmt.Sprintf("%s: %s", InvalidIntErr, err.Error()))
return
}
db := c.Db()
i := db.GetOrExpire(&key, true)
if i == nil {
c.Conn().WriteNull()
return
} else if i.Type() != ListType {
c.Conn().WriteError(fmt.Sprintf("%s: key is a %s not a %s", WrongTypeErr, i.TypeFancy(), ListTypeFancy))
return
}
l := i.(*List)
c.Redis().Mu().RLock()
values := l.LRange(start, end)
c.Redis().Mu().RUnlock()
c.Conn().WriteArray(len(values))
for _, v := range values {
c.Conn().WriteBulkString(v)
}
}