美文网首页
Redis in Docker

Redis in Docker

作者: 星塵子 | 来源:发表于2020-12-09 09:34 被阅读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
      
    • 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)
        }
    }
    

    相关文章

      网友评论

          本文标题:Redis in Docker

          本文链接:https://www.haomeiwen.com/subject/gkgcgktx.html