• go redis


    go redis

    1、安装导入

    使用第三方开源的redis库: github.com/garyburd/redigo/redis

    go get github.com/garyburd/redigo/redis
    import "github.com/garyburd/redigo/redis"

    github:https://github.com/antirez/redis

    Doc:http://godoc.org/github.com/garyburd/redigo/redis

    使用

    1、连接

    package main
    
    import (
    	"fmt"
    
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	c, err := redis.Dial("tcp", "localhost:6379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    }
    

    2、set&get

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	var p *int
    	var a int
    	p = &a
    	*p = 0
    
    	c, err := redis.Dial("tcp", "10.10.83.162:16379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    	_, err = c.Do("Set", "abc", 100)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	r, err := redis.Int(c.Do("Get", "abc"))
    	if err != nil {
    		fmt.Println("get abc failed,", err)
    		return
    	}
    	
    	fmt.Println(r)
    }
    

    3、mset&mget

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	c, err := redis.Dial("tcp", "localhost:6379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    	_, err = c.Do("MSet", "abc", 100, "efg", 300)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	r, err := redis.Ints(c.Do("MGet", "abc", "efg"))
    	if err != nil {
    		fmt.Println("get abc failed,", err)
    		return
    	}
    
    	for _, v := range r {
    		fmt.Println(v)
    	}
    }

    4、hset&hget

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	c, err := redis.Dial("tcp", "10.10.83.162:16379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    	_, err = c.Do("HSet", "books", "abc", 100)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	r, err := redis.Int(c.Do("HGet", "books", "abc"))
    	if err != nil {
    		fmt.Println("get abc failed,", err)
    		return
    	}
    
    	fmt.Println(r)
    }
    

     5、expire

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	c, err := redis.Dial("tcp", "localhost:6379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    	_, err = c.Do("expire", "abc", 10)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    }
    

    6、lpush & lpop & llen

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    func main() {
    	c, err := redis.Dial("tcp", "10.10.83.162:16379")
    	if err != nil {
    		fmt.Println("conn redis failed,", err)
    		return
    	}
    
    	defer c.Close()
    	_, err = c.Do("lpush", "book_list", "abc", "ceg", 300)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	r, err := redis.String(c.Do("lpop", "book_list"))
    	if err != nil {
    		fmt.Println("get abc failed,", err)
    		return
    	}
    
    	fmt.Println(r)
    
    	rlen, err := redis.Int(c.Do("llen", "book_list"))
    	if err != nil {
    		fmt.Println("llen error: ", err)
    		return
    	}
    
    	fmt.Println(rlen)
    }
    

    连接池

    各参数的解释如下:

    MaxIdle:最大的空闲连接数,表示即使没有redis连接时依然可以保持N个空闲的连接,而不被清除,随时处于待命状态。

    MaxActive:最大的激活连接数,表示同时最多有N个连接

    IdleTimeout:最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭

    package main
    
    import (
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    
    var pool *redis.Pool
    
    func init() {
    	pool = &redis.Pool{
    		MaxIdle:     16,
    		MaxActive:   0,
    		IdleTimeout: 300,
    		Dial: func() (redis.Conn, error) {
    			return redis.Dial("tcp", "localhost:6379")
    		},
    	}
    }
    
    func main() {
    
    	c := pool.Get()
    	defer c.Close()
    
    	_, err := c.Do("Set", "abc", 100)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	r, err := redis.Int(c.Do("Get", "abc"))
    	if err != nil {
    		fmt.Println("get abc failed,", err)
    		return
    	}
    
    	fmt.Println(r)
    	pool.Close()
    }
    

    管道操作

    请求/响应服务可以实现持续处理新请求,客户端可以发送多个命令到服务器而无需等待响应,最后在一次读取多个响应。

    使用Send(),Flush(),Receive()方法支持管道化操作

    Send向连接的输出缓冲中写入命令。

    Flush将连接的输出缓冲清空并写入服务器端。

    Recevie按照FIFO顺序依次读取服务器的响应。

    func main() {
        c, err := redis.Dial("tcp", "localhost:6379")
        if err != nil {
            fmt.Println("conn redis failed, err:", err)
            return
        }
        defer c.Close()
    
        c.Send("SET", "name1", "sss1")
        c.Send("SET", "name2", "sss2")
    
        c.Flush()
    
        v, err := c.Receive()
        fmt.Printf("v:%v,err:%v
    ", v, err)
        v, err = c.Receive()
        fmt.Printf("v:%v,err:%v
    ", v, err)
    
        v, err = c.Receive()    // 夯住,一直等待
        fmt.Printf("v:%v,err:%v
    ", v, err)
    }
    
  • 相关阅读:
    安全测试基础知识
    Python思维导图(一)—— 基础
    Java 高级点的东西
    安全测试通用用例
    测试基础篇(一)—— 面试终极常见
    测试基础篇(二)—— 面试挺常见
    差分约束基本讲解
    0x42 数据结构进阶树状数组
    Codeforce Problem 711A Bus to Udayland (正则表达式)
    POJ 1456 Supermarket【贪心 + 并查集】
  • 原文地址:https://www.cnblogs.com/shhnwangjian/p/7504832.html
Copyright © 2020-2023  润新知