package cache import ( "3tee_admin/conf" "fmt" "time" "github.com/gomodule/redigo/redis" redigo "github.com/gomodule/redigo/redis" ) var redisPool *redis.Pool func init() { redisPool = PoolInitRedis(fmt.Sprintf("%v:%v", conf.Conf.Redis.Ip, conf.Conf.Redis.Port), conf.Conf.Redis.Password) } func PoolInitRedis(server string, password string) *redigo.Pool { return &redigo.Pool{ MaxIdle: 2, IdleTimeout: 240 * time.Second, MaxActive: 3, Dial: func() (redigo.Conn, error) { c, err := redigo.Dial("tcp", server) if err != nil { return nil, err } if password != "" { if _, err := c.Do("AUTH", password); err != nil { c.Close() return nil, err } } return c, err }, TestOnBorrow: func(c redigo.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } } func Set(key string, value interface{}) error { _, err := redisPool.Get().Do("SET", key, value) if err != nil { return err } _, err = redisPool.Get().Do("EXPIRE", key, conf.Conf.Redis.TokenExpiresTime) return err } func GetInt(key string) (int, error) { id, err := redigo.Int(redisPool.Get().Do("GET", key)) return id, err } func Del(key string) error { _, err := redisPool.Get().Do("DEL", key) return err }