Redis-List-3

2020-04-10  本文已影响0人  NotFoundW

此篇专攻RPOPLPUSH命令

1. 源列表存在且长度大于0,且目标列表存在。

Shell

$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet1 hornet2
(integer) 2
127.0.0.1:6379> rpush hornet1 1 2
(integer) 2
127.0.0.1:6379> rpush hornet2 2
(integer) 1
127.0.0.1:6379> rpoplpush hornet1 hornet2
"2"
127.0.0.1:6379> lrange hornet1 0 -1
1) "1"
127.0.0.1:6379> lrange hornet2 0 -1
1) "2"
2) "2"
127.0.0.1:6379> del hornet1 hornet2
(integer) 2
Code
func RpopLpush1(c redis.Conn) {
    defer c.Do("DEL", "hornet1", "hornet2")
    c.Do("RPUSH", "hornet1", 1, 2)
    c.Do("RPUSH", "hornet2", 1)
    popElement, err := redis.Int(c.Do("RPOPLPUSH", "hornet1", "hornet2"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("Source list is existing and length > 0 and Destination list is existing. "+
        "Popped element should be:", popElement)
}
Output
$ go run main.go 
Source list is existing and length > 0 and Destination list is existing. Popped element should be: 2

2. 源列表存在且长度大于0,且目标列表不存在,命令将会使用目标列表的名字创建一个新的列表。

Shell
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet1 hornet2
(integer) 0
127.0.0.1:6379> exists hornet2
(integer) 0
127.0.0.1:6379> rpush hornet1 1 2
(integer) 2
127.0.0.1:6379> rpoplpush hornet1 hornet2
"2"
127.0.0.1:6379> lrange hornet1 0 -1
1) "1"
127.0.0.1:6379> lrange hornet2 0 -1
1) "2"
127.0.0.1:6379> del hornet1 hornet2
(integer) 2
Code
func RpopLpush2(c redis.Conn) {
    defer c.Do("DEL", "hornet1", "hornet2")
    c.Do("RPUSH", "hornet1", 1, 2)
    // Make sure destination key doesn't exist.
    isExist, _ := c.Do("EXISTS", "hornet2")
    if isExist == 1 {
        fmt.Println("Destination exists already")
        return
    }
    popElement, err := redis.Int(c.Do("RPOPLPUSH", "hornet1", "hornet2"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    fmt.Println("Source list is existing and length > 0 and Destination list doesn't existing, ")
    fmt.Println("\twill create a new list by destination name. Popped element should be:", popElement)
    // Now we can confirm that list "hornet2" has been created automatically.
    values, _ := redis.Ints(c.Do("LRANGE", "hornet2", 0, -1))
    fmt.Println("After RPOPLPUSH, new list hornet2 is:")
    for _, v := range values {
        fmt.Println(v)
    }
}
Output
$ go run main.go
Source list is existing and length > 0 and Destination list doesn't existing, 
        will create a new list by destination name. Popped element should be: 2
After RPOPLPUSH, new list hornet2 is:
2

3. 源列表存在且长度大于0,且目标不是一个列表,将会返回一个错误。

Shell
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet1 Fox
(integer) 0
127.0.0.1:6379> rpush hornet1 1
(integer) 1
127.0.0.1:6379> set Fox Wen
OK
127.0.0.1:6379> rpoplpush hornet1 Fox
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> del hornet1 Fox
(integer) 2
Code
func RpopLpush3(c redis.Conn) {
    defer c.Do("DEL", "hornet1", "Fox")
    c.Do("RPUSH", "hornet1", 1, 2)
    c.Do("SET", "Fox", "Wen")
    fmt.Println("Source list is existing and length > 0 and Destination is not a list, will get an error.")
    _, err := redis.Int(c.Do("RPOPLPUSH", "hornet1", "Fox"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
}
Output
$ go run main.go 
Source list is existing and length > 0 and Destination is not a list, will get an error.
[ERR]2020/04/10 22:25:13 WRONGTYPE Operation against a key holding the wrong kind of value

4. 源列表存在且长度等于0,将会返回nil。

Shell
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet1 hornet2
(integer) 0
127.0.0.1:6379> rpush hornet1 1
(integer) 1
127.0.0.1:6379> rpush hornet2 2
(integer) 1
127.0.0.1:6379> rpop hornet1
"1"
127.0.0.1:6379> rpoplpush hornet1 hornet2
(nil)
127.0.0.1:6379> del hornet1 hornet2
(integer) 1
Code
func RpopLpush4(c redis.Conn) {
    defer c.Do("DEL", "hornet1", "hornet2")
    c.Do("RPUSH", "hornet1", 1)
    c.Do("RPUSH", "hornet2", 2)
    //  Make hornet1 list null
    c.Do("RPOP", "hornet1")
    popElement, err := c.Do("RPOPLPUSH", "hornet1", "hornet2")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    if popElement == nil {
        fmt.Println("Source list is existing and length == 0, will return nil.")
    }
}
Output
$ go run main.go 
Source list is existing and length == 0, will return nil.

5. 源列表不存在,将会返回nil。

Shell
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet1 hornet2
(integer) 0
127.0.0.1:6379> rpush hornet2 2
(integer) 1
127.0.0.1:6379> rpoplpush hornet1 hornet2
(nil)
127.0.0.1:6379> del hornet1 hornet2
(integer) 1
Code
func RpopLpush5(c redis.Conn) {
    defer c.Do("DEL", "hornet2")
    c.Do("RPUSH", "hornet2", 2)
    popElement, err := c.Do("RPOPLPUSH", "hornet1", "hornet2")
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
    if popElement == nil {
        fmt.Println("Source list doesn't exist, will return nil.")
    }
}
Output
$ go run main.go 
Source list doesn't exist, will return nil.

6. 源不是一个列表,将会返回一个错误。

Shell
$ redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> del hornet2 Fox
(integer) 0
127.0.0.1:6379> set Fox Wen
OK
127.0.0.1:6379> rpush hornet2 2
(integer) 1
127.0.0.1:6379> rpoplpush Fox hornet2
(error) WRONGTYPE Operation against a key holding the wrong kind of value
127.0.0.1:6379> del hornet2 Fox
(integer) 2
Code
func RpopLpush6(c redis.Conn) {
    defer c.Do("DEL", "hornet2", "Fox")
    c.Do("SET", "Fox", "Wen")
    c.Do("RPUSH", "hornet2", 2)
    fmt.Println("Source is not a list, will get an error.")
    _, err := redis.Int(c.Do("RPOPLPUSH", "Fox", "hornet2"))
    if err != nil {
        colorlog.Error(err.Error())
        return
    }
}
Output
$ go run main.go 
Source is not a list, will get an error.
[ERR]2020/04/10 22:29:14 WRONGTYPE Operation against a key holding the wrong kind of value
上一篇下一篇

猜你喜欢

热点阅读