| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 | package redisimport (	"fmt"	"github.com/go-redsync/redsync"	"github.com/gomodule/redigo/redis"	"time")var pool *redis.Poolvar redisLock *redsync.Redsyncfunc InitRedis(host, port, password string) error  {	pool = &redis.Pool{		MaxIdle:     20,		IdleTimeout: 240 * time.Second,		MaxActive:   50,		Dial: func() (redis.Conn, error) {			c, err := redis.Dial("tcp", fmt.Sprintf("%s:%s", host, port))			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 redis.Conn, t time.Time) error {			_, err := c.Do("PING")			return err		},	}	redisLock = redsync.New([]redsync.Pool{pool})	return nil}func GetRedisConn() (redis.Conn, error)  {	conn := pool.Get()	return conn, conn.Err()}func GetRedisLock(key string, expireTime time.Duration) *redsync.Mutex  {	return redisLock.NewMutex(key, redsync.SetExpiry(expireTime))}
 |