redis使用说明
- 启动服务端
安装成功后,通过终端键入如下命令:
redis-server.exe redis.windows.conf
一切没问题会出现下面的提示: [11368] 13 Jul 10:10:31.487 # Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
- 启动客户端
新打开一个终端,启动redis客户端,键入命令:
redis-cli.exe -h 127.0.0.1 -p 6379
- 测试
127.0.0.1:6379> set mykey abc
OK
127.0.0.1:6379> get mykey
"abc"
-
开源库redigo的使用
连接Redis
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("Connect to redis error", err)
return
}
defer c.Close()
}
- 初始化连接池
import (
"github.com/garyburd/redigo/redis"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
"github.com/golang/glog"
_ "github.com/lib/pq"
)
var RedisClient *redis.Pool
func InitRedis(Host string,Port int,Passwd string,Index int,IdleTimeout int,MaxIdleCount int,MaxOpenCount int) {
RedisClient = &redis.Pool{
MaxIdle: MaxIdleCount,
MaxActive: MaxOpenCount,
IdleTimeout: time.Duration(IdleTimeout) * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", Host, Port))
if err != nil {
return nil, err
}
/*fmt.Printf("Host:%s,Port:%s, Passwd:%s,Index:%s\n",Host,Port,Passwd,Index)
if _, err := c.Do("AUTH", Passwd); err != nil {
c.Close()
return nil, err
}*/
if _, err := c.Do("SELECT", Index); err != nil {
c.Close()
return nil, err
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
- 读写
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("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("SET", "mykey", "superWang")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "mykey"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
}
- 设置过期
package main
import (
"fmt"
"time"
"github.com/garyburd/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("SET", "mykey", "superWang", "EX", "5")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "mykey"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
time.Sleep(8 * time.Second)
username, err = redis.String(c.Do("GET", "mykey"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
}
- 检测值是否存在
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("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("SET", "mykey", "superWang")
if err != nil {
fmt.Println("redis set failed:", err)
}
is_key_exit, err := redis.Bool(c.Do("EXISTS", "mykey1"))
if err != nil {
fmt.Println("error:", err)
} else {
fmt.Printf("exists or not: %v \n", is_key_exit)
}
}
- 删除
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("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("SET", "mykey", "superWang")
if err != nil {
fmt.Println("redis set failed:", err)
}
username, err := redis.String(c.Do("GET", "mykey"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
_, err = c.Do("DEL", "mykey")
if err != nil {
fmt.Println("redis delelte failed:", err)
}
username, err = redis.String(c.Do("GET", "mykey"))
if err != nil {
fmt.Println("redis get failed:", err)
} else {
fmt.Printf("Get mykey: %v \n", username)
}
}
- 读写Json到Redis
package main
import (
"encoding/json"
"fmt"
"github.com/garyburd/redigo/redis"
)
func main() {
c, err := redis.Dial("tcp", "127.0.0.1:6379")
if err != nil {
fmt.Println("Connect to redis error", err)
return
}
defer c.Close()
key := "profile"
imap := map[string]string{"username": "666", "phonenumber": "888"}
value, _ := json.Marshal(imap)
n, err := c.Do("SETNX", key, value)
if err != nil {
fmt.Println(err)
}
if n == int64(1) {
fmt.Println("success")
}
var imapGet map[string]string
valueGet, err := redis.Bytes(c.Do("GET", key))
if err != nil {
fmt.Println(err)
}
errShal := json.Unmarshal(valueGet, &imapGet)
if errShal != nil {
fmt.Println(err)
}
fmt.Println(imapGet["username"])
fmt.Println(imapGet["phonenumber"])
}
- 列表操作
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("Connect to redis error", err)
return
}
defer c.Close()
_, err = c.Do("lpush", "runoobkey", "redis")
if err != nil {
fmt.Println("redis set failed:", err)
}
_, err = c.Do("lpush", "runoobkey", "mongodb")
if err != nil {
fmt.Println("redis set failed:", err)
}
_, err = c.Do("lpush", "runoobkey", "mysql")
if err != nil {
fmt.Println("redis set failed:", err)
}
values, _ := redis.Values(c.Do("lrange", "runoobkey", "0", "100"))
for _, v := range values {
fmt.Println(string(v.([]byte)))
}
}
- 管道
c.Send("SET", "foo", "bar")
c.Send("GET", "foo")
c.Flush()
c.Receive() // reply from SET
v, err = c.Receive() // reply from GET
- 应用
package main
import (
"fmt"
"github.com/go-redis/redis"
)
func main() {
client := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "", // no password set
DB: 0, // use default DB
})
pong, err := client.Ping().Result()
fmt.Println(pong, err)
err = client.Set("key", "value", 0).Err()
if err != nil {
panic(err)
}
val, err := client.Get("key").Result()
if err != nil {
panic(err)
}
fmt.Println("key", val)
val2, err := client.Get("key2").Result()
if err == redis.Nil {
fmt.Println("key2 does not exists")
} else if err != nil {
panic(err)
} else {
fmt.Println("key2", val2)
}
}
github地址: https://github.com/go-redis/redis
文档地址: https://godoc.org/github.com/go-redis/redis
获取:
go get -u github.com/go-redis/redis