| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | /* * @description: * @Author: CP * @Date: 2021-01-27 11:00:27 * @FilePath: \construction_management\datasource\rdshelper.go */package datasourceimport (	"fmt"	"log"	"sync"	"time"	"github.com/gomodule/redigo/redis"	"go.mod/conf")var rdsLock sync.Mutexvar cacheInstance *RedisConn// 封装成一个redis资源池type RedisConn struct {	pool      *redis.Pool	showDebug bool}// 对外只有一个命令,封装了一个redis的命令func (rds *RedisConn) Do(commandName string, args ...interface{}) (reply interface{}, err error) {	conn := rds.pool.Get()	defer conn.Close()	t1 := time.Now().UnixNano()	reply, err = conn.Do(commandName, args...)	if err != nil {		e := conn.Err()		if e != nil {			log.Println("rdshelper Do", err, e)		}	}	t2 := time.Now().UnixNano()	if rds.showDebug {		fmt.Printf("[redis] [info] [%dus]cmd=%s, err=%s, args=%v, reply=%s\n", (t2-t1)/1000, commandName, err, args, reply)	}	return reply, err}// 设置是否打印操作日志func (rds *RedisConn) ShowDebug(b bool) {	rds.showDebug = b}// 得到唯一的redis缓存实例func InstanceCache() *RedisConn {	if cacheInstance != nil {		return cacheInstance	}	rdsLock.Lock()	defer rdsLock.Unlock()	if cacheInstance != nil {		return cacheInstance	}	return NewCache()}// 重新实例化func NewCache() *RedisConn {	pool := redis.Pool{		Dial: func() (redis.Conn, error) {			// redis.DialDatabase(5)			c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", conf.RdsCache.Host, conf.RdsCache.Port), redis.DialDatabase(5))			if err != nil {				log.Fatal("rdshelper.NewCache Dial error ", err)				return nil, err			}			return c, nil		},		TestOnBorrow: func(c redis.Conn, t time.Time) error {			if time.Since(t) < time.Minute {				return nil			}			_, err := c.Do("PING")			return err		},		MaxIdle:         10000,		MaxActive:       10000,		IdleTimeout:     0,		Wait:            false,		MaxConnLifetime: 0,	}	instance := &RedisConn{		pool: &pool,	}	cacheInstance = instance	cacheInstance.ShowDebug(true)	//cacheInstance.ShowDebug(false)	return cacheInstance}
 |