redis操作
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
1.5 KiB

package redis
import (
redisdb "github.com/gomodule/redigo/redis"
)
//list 尾部增加值
func Rpush(key string, field interface{}) (int64, error) {
c := GetConn()
reply, err := c.Do("RPUSH", key, field)
CloseConn(c)
if err != nil {
return 0, err
}
return redisdb.Int64(reply, nil)
}
//移除列表的最后一个元素,返回值为移除的元素。
func Rpop(key string) ([]byte, error) {
c := GetConn()
ret, err := c.Do("RPOP", key)
CloseConn(c)
if err != nil {
return nil, err
}
reply, err := redisdb.Bytes(ret, err)
return reply, err
}
//list 头部增加值
func Lpush(key string, field interface{}) (int64, error) {
c := GetConn()
reply, err := c.Do("LPUSH", key, field)
CloseConn(c)
if err != nil {
return 0, err
}
return redisdb.Int64(reply, nil)
}
//list 长度
func Llen(key string) (int64, error) {
c := GetConn()
reply, err := c.Do("LLEN", key)
CloseConn(c)
if err != nil {
return 0, err
}
return redisdb.Int64(reply, nil)
}
//list 通过索引设置列表元素的值 LSET key index value
func Lset(key string, index int, value interface{}) (interface{}, error) {
c := GetConn()
reply, err := c.Do("LSET", key, index, value)
CloseConn(c)
return reply, err
}
/*
LRANGE key start stop
获取列表指定范围内的元素
*/
func Lrange(key string, start, stop int64) ([][]byte, error) {
c := GetConn()
ret, err := c.Do("LRANGE", key, start, stop)
reply := make([][]byte, 0)
if err == nil {
reply, err = redisdb.ByteSlices(ret, err)
}
CloseConn(c)
return reply, err
}