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

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

  • 相关阅读:
    WPF入门教程系列六——布局介绍与Canvas(一)
    WPF入门教程系列五——Window 介绍
    WPF入门教程系列四——Dispatcher介绍
    WPF入门教程系列三——Application介绍(续)
    html5 标签
    html5
    sublime汉化教程
    html5 文本格式化
    主键和索引的区别
    响应式布局的开发基础知识
  • 原文地址:https://www.cnblogs.com/newmiracle/p/12982453.html
Copyright © 2011-2022 走看看