zoukankan      html  css  js  c++  java
  • golang redis消息队列

    package main
    
    import (
        "fmt"
        "time"
    
        "github.com/garyburd/redigo/redis"
    )
    
    const (
        RedisURL            = "redis://127.0.0.1:6379"
        redisMaxIdle        = 3   //最大空闲连接数
        redisIdleTimeoutSec = 240 //最大空闲连接时间
        RedisPassword       = "123456"
    )
    
    // NewRedisPool 返回redis连接池
    func NewRedisPool(redisURL string) *redis.Pool {
        return &redis.Pool{
            MaxIdle:     redisMaxIdle,
            IdleTimeout: redisIdleTimeoutSec * time.Second,
            Dial: func() (redis.Conn, error) {
                c, err := redis.DialURL(redisURL)
                if err != nil {
                    return nil, fmt.Errorf("redis connection error: %s", err)
                }
                //验证redis密码
                if _, authErr := c.Do("AUTH", RedisPassword); authErr != nil {
                    return nil, fmt.Errorf("redis auth password error: %s", authErr)
                }
                return c, err
            },
            TestOnBorrow: func(c redis.Conn, t time.Time) error {
                _, err := c.Do("PING")
                if err != nil {
                    return fmt.Errorf("ping redis error: %s", err)
                }
                return nil
            },
        }
    }
    
    func set(k, v string) {
        c := NewRedisPool(RedisURL).Get()
        defer c.Close()
        _, err := c.Do("SET", k, v)
        if err != nil {
            fmt.Println("set error", err.Error())
        }
    }
    
    func getStringValue(k string) string {
        c := NewRedisPool(RedisURL).Get()
        defer c.Close()
        username, err := redis.String(c.Do("GET", k))
        if err != nil {
            fmt.Println("Get Error: ", err.Error())
            return ""
        }
        return username
    }
    func main() {
        set("ds","cd")
        fmt.Println(getStringValue("ds"))
    }

    golang redis消息队列

    要设置下密码

    127.0.0.1:6379> auth 123456
    ERR Client sent AUTH, but no password is set

    设置其密码

    redis 127.0.0.1:6379> CONFIG SET requirepass "123456"
    OK
    redis 127.0.0.1:6379> AUTH 123456
    Ok

    设置下这个配置密码就好了

  • 相关阅读:
    Java的Regex --正则表达式
    Java的包装类
    类的始祖Object
    abstract和interface关键字介绍
    内部类
    Accumulation Degree [换根dp,二次扫描]
    牛客练习赛61 [口胡]
    CF1334G Substring Search [bitset,乱搞]
    CF1175F The Number of Subpermutations [哈希,乱搞]
    CF793G Oleg and chess [线段树优化建边,扫描线,最大流]
  • 原文地址:https://www.cnblogs.com/newmiracle/p/12982453.html
Copyright © 2011-2022 走看看