Redis in Docker

2020-12-09  本文已影响0人  星塵子
  1. 查询

    docker search redis
    
  2. 拉取镜像

    docker pull redis:latest
    
  3. 查看

    docker images
    
  4. 运行实例

    docker run --name redis -p 6379:6379 -v D:\db\redis:/data  -d redis redis-server --appendonly yes --requirepass "password" 
    
  5. 联机测试

    • cli 测试
    docker exec -it redis /bin/bash
    rediscli -h 127.0.0.1 -p 6379
    auth password
    set test 1
    get test 1
    
# 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)
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)
    }
}
上一篇下一篇

猜你喜欢

热点阅读