1:安装redigo


go get github.com/garyburd/redigo/redis


2:连接redis(通过ip端口)


import (

    "github.com/garyburd/redigo/redis"

)

c, err := redis.Dial("tcp", "192.168.2.225:6379")

if err != nil {

    fmt.Println("connect to redis err", err.Error())

    return

}

defer c.Close()


3:命令执行


n,err := c.Do("lpush","key","value")  //写

result,err := redis.Values(c.Do("lpop","key"))//读


4:list类型常见操作


rpush(key, value):在名称为key的list尾添加一个值为value的元素

lpush(key, value):在名称为key的list头添加一个值为value的 元素

llen(key):返回名称为key的list的长度

lrange(key, start, end):返回名称为key的list中start至end之间的元素

ltrim(key, start, end):截取名称为key的list

lindex(key, index):返回名称为key的list中index位置的元素

lset(key, index, value):给名称为key的list中index位置的元素赋值

lrem(key, count, value):删除count个key的list中值为value的元素

lpop(key):返回并删除名称为key的list中的首元素

rpop(key):返回并删除名称为key的list中的尾元素

blpop(key1, key2,… key N, timeout):lpop命令的block版本。

brpop(key1, key2,… key N, timeout):rpop的block版本。

rpoplpush(srckey, dstkey):返回并删除名称为srckey的list的尾元素,并将该元素添加到名称为dstkey的list的头部


5:示例

5.1:lpush示例


_,err = c.Do("lpush","mylist","ofo","mobike","foo")

if err != nil   {

    fmt.Println("redis lpush failed",err.Error())

}


5.2:rpush示例


_,err = c.Do("rpush","mylist","bluegogo","xiaolan","xiaoming")

if err != nil{

    fmt.Println("redis rpush failed",err.Error())

}


5.3:llen示例


num,err := c.Do("llen","mylist")

if err != nil{

    fmt.Println("mylist get len err",err.Error())

}else{

    fmt.Println("mylist's len is ",num)

}


5.4:lrange示例


values,err := redis.Values(c.Do("lrange","mylist",0,10))

if err != nil{

    fmt.Println("lrange err",err.Error())

}

fmt.Printf("mylist is:")

for _,v := range values{

    fmt.Printf(" %s ",v.([]byte))

}

fmt.Println()


5.5:ltrim示例


_, err = c.Do("ltrim", "mylist", 0, 4)

if err != nil {

    fmt.Println("ltrim error", err.Error())

} else {

    values, err = redis.Values(c.Do("lrange", "mylist", 0, 4))

    if err != nil {

        fmt.Println("ltrim failed:", err.Error())

    }

    fmt.Printf("ltrim mylist is:")

    for _, v := range values {

        fmt.Printf("%s ", v.([]byte))

    }

    fmt.Printf("n")

}


5.6:lindex示例


val, err := c.Do("lindex", "mylist", 2)

if err != nil {

    fmt.Println("lindex error:", err.Error())

} else {

    fmt.Printf("lindex get result :%sn", val)

}


5.7:rpoplpush示例


_, err = c.Do("rpoplpush", "mylist", "mybike")

if err != nil {

    fmt.Println("rpoplpush failed:", err.Error())

} else {

    values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))

    if err != nil {

        fmt.Println("lrange failed:", err.Error())

    }

    for _, v := range values {

        fmt.Printf("rpoplpush %sn", v.([]byte))

    }


    values, err = redis.Values(c.Do("lrange", "mybike", 0, 10))

    if err != nil {

        fmt.Println("lrange failed:", err.Error())

    }

    for _, v := range values {

        //fmt.Println(string(v.([]byte)))

        fmt.Printf("rpoplpush %sn", v.([]byte))

    }

}


5.8:lset示例


_, err = c.Do("lset", "mylist", 2, "mysql")

if err != nil {

    fmt.Println("lset error:", err.Error())

}

val, err = c.Do("lindex", "mylist", 2)

if err != nil {

    fmt.Println("lset error:", err.Error())

} else {

    fmt.Printf("lset get result:%sn", val)

}


5.9:lrem示例


_, err = c.Do("lrem", "mylist", 1, "mysql")

if err != nil {

    fmt.Println("lrem error", err.Error())

} else {

    values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))

    if err != nil {

        fmt.Println("ltrim failed:", err.Error())

    }

    for _, v := range values {

        fmt.Printf("lrem mylist: %s", v.([]byte))

    }

    fmt.Printf("n")

}


5.10:lpop示例


_, err = c.Do("lpop", "mylist")

if err != nil {

    fmt.Println("lpop failed:", err.Error())

} else {

    values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))

    if err != nil {

        fmt.Println("lpop failed:", err.Error())

    }

    fmt.Printf("lpop mylist :")

    for _, v := range values {

        fmt.Printf("lpop mylist %s", v.([]byte))

    }

    fmt.Printf("n")

}


5.11:rpop示例


_, err = c.Do("rpop", "mylist")

if err != nil {

    fmt.Println("rpop failed", err.Error())

} else {

    values, err = redis.Values(c.Do("lrange", "mylist", 0, 10))

    if err != nil {

        fmt.Println("rpop failed:", err.Error())

    }

    fmt.Printf("rpop mylist :")

    for _, v := range values {

        //fmt.Println(string(v.([]byte)))

        fmt.Printf("lpop mylist %s", v.([]byte))

    }

    fmt.Printf("n")

}


5.12:blpop示例


res, err := c.Do("blpop", "mylist", 10)

if err != nil {

    fmt.Println("blpop error")

} else {

    fmt.Printf("blpop from mylist get:%sn", res)

}


res, err = c.Do("blpop", "tmpbike", 10)

if err != nil {

    fmt.Println("blpop time out")

} else {

    fmt.Println("blpop from tmpbike get:", res)

}


5.13:brpop示例


res, err = c.Do("brpop", "tmpbike", 10)

if err != nil {

    fmt.Println("brpop error")

} else {

    fmt.Printf("brpop from tmpbike get :%sn", res)

}


res, err = c.Do("brpop", "mybike", 10)

if err != nil {

    fmt.Println("brpop time out")

} else {

    fmt.Printf("brpop from mybike get:%s ", res)


}


6:结果

这里写图片描述

完整源码:https://github.com/fang2329/learning/blob/master/redis_test/redis_list.go

点赞(1436)

评论列表共有 0 条评论

立即
投稿
返回
顶部