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
设置下这个配置密码就好了