我把redis装在docker里并且做了端口映射,运行过程中发现值始终没办法设置进redis中,自己写了一个demo测试了一下,发现有这个报错
// redis连接测试代码
func testRedis() bool {
// e.g. REDIS_PORT = tcp://172.17.0.89:6379
conn, err := redis.DialTimeout("tcp", "127.0.0.1:6379", 0, 1*time.Second, 1*time.Second)
if err != nil {
fmt.Println(err)
return false
}
defer conn.Close()
size, err := conn.Do("DBSIZE")
if err != nil {
fmt.Println(err)
return false
}
fmt.Printf("DB size is %d \n", size)
_, err = conn.Do("SET", "user:user0", 123)
_, err = conn.Do("SET", "user:user1", 456)
_, err = conn.Do("APPEND", "user:user0", 87)
user0, err := redis.Int(conn.Do("GET", "user:user0"))
user1, err := redis.Int(conn.Do("GET", "user:user1"))
fmt.Printf("user0 is %d , user1 is %d \n", user0, user1)
return true
}
func main() {
fmt.Println(time.Now(), "[Redis test start]")
if testRedis() {
fmt.Println("[Redis test ok]")
} else {
fmt.Println("[Redis test failed]")
}
}