redis.go 926 Bytes
package zredis

import (
	"github.com/gomodule/redigo/redis"
	"time"
)

var (
	_addr string
	_pass string
	_db   int
)
var pool *redis.Pool

func Init(addr, pass string, db int) {

	_addr, _pass, _db = addr, pass, db

	pool = &redis.Pool{
		MaxActive:   10240,
		MaxIdle:     10240,             // Maximum number of idle connections in the pool.
		IdleTimeout: 240 * time.Second, // Close connections after remaining idle for this duration
		Dial:        dial,
		Wait:        true,
	}
}

func dial() (redis.Conn, error) {
	conn, err := redis.Dial("tcp", _addr,
		redis.DialPassword(_pass),
		redis.DialDatabase(_db),
		redis.DialConnectTimeout(5*time.Second),
		redis.DialReadTimeout(5*time.Second),
		redis.DialWriteTimeout(5*time.Second),
	)
	return conn, err
}

func GetConn() redis.Conn {
	if pool == nil {
		return nil
	}
	return pool.Get()
}

func AutoClose(conn redis.Conn) {
	err := conn.Close()
	if err != nil {
	}
}