gobox中redis操作

作者: ligang1109 | 来源:发表于2018-07-29 14:40 被阅读20次

今天来说下使用gobox中redis操作相关

说明

本包的driver部分使用了redigo:https://github.com/garyburd/redigo

用法示例

package main

import (
    "github.com/goinbox/redis"

    "time"
    "fmt"
)

func main() {
    client := redis.NewClient(redis.NewConfig("127.0.0.1", "6379", "123"), nil)

    fmt.Println("====== testGeneral ======")
    testGeneral(client)

    fmt.Println("====== testAutoReconnect ======")
    testAutoReconnect(client)

    client.Free()

}

func testGeneral(client *redis.Client) {
    reply := client.Do("set", "c", "1")
    fmt.Println(reply.String())
    reply = client.Do("get", "c")
    fmt.Println(reply.Int())

    reply = client.DoWithoutLog("set", "d", "1")
    fmt.Println(reply.String())
    reply = client.DoWithoutLog("get", "d")
    fmt.Println(reply.Int())

    client.Send("set", "a", "a")
    client.Send("set", "b", "b")
    client.Send("get", "a")
    client.Send("get", "b")
    replies, errIndexes := client.ExecPipelining()
    fmt.Println(errIndexes)
    for _, reply := range replies {
        fmt.Println(reply.String())
        fmt.Println(reply.Err)
    }

    client.BeginTrans()
    client.Send("set", "a", "1")
    client.Send("set", "b", "2")
    client.Send("get", "a")
    client.Send("get", "b")
    replies, _ = client.ExecTrans()
    for _, reply := range replies {
        fmt.Println(reply.String())
        fmt.Println(reply.Err)
    }

}

func testAutoReconnect(client *redis.Client) {
    reply := client.Do("set", "a", "1")
    fmt.Println(reply.String())
    time.Sleep(time.Second * 4) //set redis-server timeout = 3
    reply = client.Do("get", "a")
    fmt.Println(reply.Err)
    fmt.Println(reply.Int())

    time.Sleep(time.Second * 4)

    client.Send("set", "a", "a")
    client.Send("set", "b", "b")
    client.Send("get", "a")
    client.Send("get", "b")
    replies, errIndexes := client.ExecPipelining()
    fmt.Println(errIndexes)
    for _, reply := range replies {
        fmt.Println(reply.String())
        fmt.Println(reply.Err)
    }

    time.Sleep(time.Second * 4)

    client.BeginTrans()
    client.Send("set", "a", "1")
    client.Send("set", "b", "2")
    client.Send("get", "a")
    client.Send("get", "b")
    replies, _ = client.ExecTrans()
    for _, reply := range replies {
        fmt.Println(reply.String())
        fmt.Println(reply.Err)
    }

    client.Free()
}

程序输出:

====== testGeneral ======
OK <nil>
1 <nil>
OK <nil>
1 <nil>
[]
OK <nil>
<nil>
OK <nil>
<nil>
a <nil>
<nil>
b <nil>
<nil>
OK <nil>
<nil>
OK <nil>
<nil>
1 <nil>
<nil>
2 <nil>
<nil>
====== testAutoReconnect ======
OK <nil>
<nil>
1 <nil>
[]
OK <nil>
<nil>
OK <nil>
<nil>
a <nil>
<nil>
b <nil>
<nil>
OK <nil>
<nil>
OK <nil>
<nil>
1 <nil>
<nil>
2 <nil>
<nil>

欢迎大家使用,使用中有遇到问题随时反馈,我们会尽快响应,谢谢!

相关文章

  • gobox中redis操作

    今天来说下使用gobox中redis操作相关 说明 本包的driver部分使用了redigo:https://gi...

  • gobox中mysql操作

    今天来说下使用gobox中mysql操作相关 说明 本包的driver部分使用了go-sql-driver:htt...

  • gobox中的分页操作

    今天来说下使用gobox中的分页操作 说明 分页也是我们开发时的一个常见需求,gobox中提供了page包做这个事...

  • gobox中的log操作

    今天来说下使用gobox中的log操作 log级别定义 重要的interface IWriter 定义消息写入到哪...

  • gobox中的连接池pool

    今天来说下gobox中的连接池底层实现pool 为什么需要连接池 我们的系统在访问外部资源(redis、mysql...

  • 03-Redis 操作之Hash操作

    Redis 操作之Hash操作 Hash操作,redis中Hash在内存中的存储格式如下: hset(name, ...

  • 02-Redis 操作之String操作

    Redis 操作之String操作 String操作,redis中的String在在内存中按照一个name对应一个...

  • Redis学习之路(二):Redis数据结构简介

    Redis提供的5种结构 Redis中的字符串 基本命令 操作练习: Redis中的列表 基本命令 基本操作: R...

  • 04-Redis 操作之List操作

    Redis 操作之List操作 List操作,redis中的List在在内存中按照一个name对应一个List来存...

  • redis pipeline简介

    java中redis的pipeline pipeline方式执行redis操作: doInRedis方法中实现需要...

网友评论

    本文标题:gobox中redis操作

    本文链接:https://www.haomeiwen.com/subject/fnwzmftx.html