redis中文版 http://www.redis.cn/
windows下载地址: https://github.com/MicrosoftArchive/redis/releases,
备用地址:https://github.com/ServiceStack/redis-windows,这里下载的是redis-64.3.0.503版本
下载完成后启动redis服务器 redis-server.exe redis.windows.conf
安装redigo
go get github.com/garyburd/redigo/redis
redis基本操作
package main
import (
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
func checkErr(errMasg error) {
if errMasg != nil {
panic(errMasg)
}
}
func main() {
//建立连接
c, err := redis.Dial("tcp", "127.0.0.1:6379")
checkErr(err)
defer c.Close()
//查看redis已有数据量
size, err := c.Do("DBSIZE")
fmt.Printf("size is %d \n", size)
//执行set命令,写入数据
_, err = c.Do("set", "name", "yuanye")
checkErr(err)
//取数据
name, err := redis.String(c.Do("get", "name"))
if err != nil {
checkErr(err)
} else {
fmt.Println(name)
}
//删除数据
_, err = c.Do("del", "name")
checkErr(err)
//检查name是否存在
has, err := redis.Bool(c.Do("exists", "name"))
if err != nil {
fmt.Println("name is", err)
} else {
fmt.Println(has)
}
//设置redis过期时间3s
_, err = c.Do("set", "myName", "hehe", "ex", 3)
checkErr(err)
myName, err := redis.String(c.Do("get", "myName"))
fmt.Println("myName : ", myName)
//5s后取数据
time.Sleep(time.Second * 5)
myName, err = redis.String(c.Do("get", "myName"))
if err != nil {
fmt.Println("After 5s ", err)
} else {
fmt.Println("After 5s myName : ", myName)
}
}
打印结果:
size is 2
yuanye
false
myName : hehe
After 5s redigo: nil returned
对于redis传回来的普通对象,redis做了类型转换
func Bool(reply interface{}, err error) (bool, error) func Bytes(reply interface{}, err error) ([]byte, error) func Float64(reply interface{}, err error) (float64, error) func Int(reply interface{}, err error) (int, error) func Int64(reply interface{}, err error) (int64, error) func String(reply interface{}, err error) (string, error) func Strings(reply interface{}, err error) ([]string, error) func Uint64(reply interface{}, err error) (uint64, error)
redis相关命令可以查看redis中文网http://www.redis.cn/commands.html