-
查询
docker search redis
-
拉取镜像
docker pull redis:latest
-
查看
docker images
-
运行实例
docker run --name redis -p 6379:6379 -v D:\db\redis:/data -d redis redis-server --appendonly yes --requirepass "password"
-
联机测试
- cli 测试
docker exec -it redis /bin/bash rediscli -h 127.0.0.1 -p 6379 auth password set test 1 get test 1
- Python 测试
# pip install redis
import redis
r = redis.StrictRedis(host='localhost',port=6379,db=0,password='password',decode_responses=True)
r.set('hello','Redis in Docker')
print(r.get('hello')) # or print(r['hello'])
StrictRedis
实现了大部分官方命令
decode_responses=True
返回字符串,默认是字节
import redis
#使用连接池
pool = redis.ConnectionPool(host='localhost',port=6379,db=0,password='password',decode_responses=True,max_connections=20)
r = redis.Redis(connection_pool=pool)
- Go 测试
package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
)
func main() {
ctx := context.Background()
client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "password",
DB: 0,
})
//Pong
res, err := client.Ping(ctx).Result()
if err != nil {
panic(err)
}
fmt.Println(res)
res, err = client.Get(ctx, "hello").Result()
if err == redis.Nil {
fmt.Println("key:hello does not exist")
} else if err != nil {
panic(err)
} else {
fmt.Println("key:hello " + res)
}
}
网友评论