zoukankan      html  css  js  c++  java
  • 连接redis

    1.安装一个redis server 

    提供一个下载地址:https://github.com/MicrosoftArchive/redis/releases

    2.找到安装redis的路径,执行如下命令启动server

    redis-server.exe redis.windows.conf

    3.启动一个客户端

    redis-cli.exe

    go 连接redis需要使用第三方库redigo

    1)安装redigo

    go get github.com/garyburd/redigo/redis

    连接读写:set

    package main
    
    import(
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    func main() {
    	c ,err:= redis.Dial("tcp","127.0.0.1:6379") //连接
    	if err !=nil {
    		fmt.Println("failed to connection redis server",err)
    		return
    	}
    	defer c.Close()
    	_,err =c.Do("set","surname","wang")  //写入数据
    	if err != nil {
    		fmt.Println("write failed:",err)
    		return
    	}
    	r,err :=redis.String( c.Do("get","surname"))   //读取数据
    	if err != nil {
    		fmt.Println("read failed:",err)
    		return
    	}
    	fmt.Printf(r)
    
    }
    

      存储hash表:

    package main
    
    import(
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    func main() {
    	c ,err:= redis.Dial("tcp","127.0.0.1:6379") //连接
    	if err !=nil {
    		fmt.Println("failed to connection redis server",err)
    		return
    	}
    	defer c.Close()
    	_,err =c.Do("HSet","student","name","wang")  //写入数据
    	if err != nil {
    		fmt.Println("write failed:",err)
    		return
    	}
    	r,err :=redis.String( c.Do("HGet","student","name"))   //读取数据
    	if err != nil {
    		fmt.Println("read failed:",err)
    		return
    	}
    	fmt.Printf(r)
    
    }
    

      队列操作:

    package main
    
    import(
    	"fmt"
    	"github.com/garyburd/redigo/redis"
    )
    func main() {
    	c ,err:= redis.Dial("tcp","127.0.0.1:6379") //连接
    	if err !=nil {
    		fmt.Println("failed to connection redis server",err)
    		return
    	}
    	defer c.Close()
    	_,err =c.Do("lpush","child","wangsan","lisi","zhaowu")  //写入数据到队列
    	if err != nil {
    		fmt.Println("write failed:",err)
    		return
    	}
    	r,err :=redis.String( c.Do("lpop","child"))   //读取数据
    	if err != nil {
    		fmt.Println("read failed:",err)
    		return
    	}
    	fmt.Printf(r)
    
    }
    

     一些批量操作:

    批量写入读取

    MGET key [key …] 
    MSET key value [key value …]

    批量写入读取对象(Hashtable) 
    HMSET key field value [field value …] 
    HMGET key field [field …]

  • 相关阅读:
    一致性hash算法
    运算符的重载
    HTTP协议详解
    SOA 新业务语言 新系统架构——什么是SOA
    js中几种实用的跨域方法原理详解
    Linq基于两个属性的分组
    BackBone结合ASP.NET MVC实现页面路由操作
    CSS学习总结
    单页应用 WebApp SPA 骨架 框架 路由 页面切换 转场
    2016年最值得学习的五大开源项目
  • 原文地址:https://www.cnblogs.com/wanghaijun999/p/8408589.html
Copyright © 2011-2022 走看看