redisCommon.go 6.28 KB
package sdkredis

import (
	"common"
	"fmt"
	"time"

	"github.com/garyburd/redigo/redis"
)

//正式环境
/*var REDIS_HOST string = "r-2ze79cf083318224.redis.rds.aliyuncs.com:6379"
var REDIS_PWD string = "Icesimba858680668866Redis"
var REDIS_DB int = 2
*/

type Redis struct {
	Config    *common.Config // 配置文件
	RedisPool *redis.Pool    // 连接池
}

var RedisClient *Redis

func RedisInit(config *common.Config) {
	RedisClient = NewRedis(config)
}

func NewRedis(config *common.Config) (r *Redis) {
	r = new(Redis)
	r.Config = config
	fmt.Printf("redis host[%v]port[%v]passwd[%v]dbindex[%d]\n", r.Config.SDKRedis.Address, r.Config.SDKRedis.Port, r.Config.SDKRedis.Password, r.Config.SDKRedis.DBIndex)

	/*
		var REDIS_HOST string = "r-2ze1633d3a4dc424.redis.rds.aliyuncs.com:6379"
		var REDIS_PWD string = "Icesimba8866"
		var REDIS_DB int = 2
	*/
	var REDIS_HOST string = r.Config.SDKRedis.Address + ":" + r.Config.SDKRedis.Port
	var REDIS_PWD string = r.Config.SDKRedis.Password
	var REDIS_DB int = r.Config.SDKRedis.DBIndex

	r.RedisPool = &redis.Pool{
		MaxIdle:     50,                // 最大的空闲连接数
		MaxActive:   100,               // 最大的激活连接数
		IdleTimeout: 180 * time.Second, // 最大的空闲连接等待时间,超过此时间后,空闲连接将被关闭
		//Wait:        true,              //是否在超过最大连接数的时候等待
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial("tcp", REDIS_HOST)
			if err != nil {
				return nil, err
			}

			// 验证密码
			if _, err := conn.Do("AUTH", REDIS_PWD); err != nil {
				conn.Close()
				return nil, err
			}

			// 选择db
			if _, err := conn.Do("SELECT", REDIS_DB); err != nil {
				return nil, err
			}

			return conn, nil
		},
		TestOnBorrow: func(conn redis.Conn, t time.Time) error {
			_, err := conn.Do("PING")
			return err
		},
	}

	return
}

/**
 *  从连接池获得连接
 */
func (self *Redis) GetConn() redis.Conn {
	return self.RedisPool.Get()
}

/**
 *  释放redis连接
 */
func RedisClose(conn redis.Conn) {
	conn.Close()
}

/**
 *  执行redis命令
 */
func RedisCommand(conn redis.Conn, commandName string, args ...interface{}) (reply interface{}, err error) {
	reply, err = conn.Do(commandName, args...)

	return
}

/**
*  选择数据库
 */
func RedisDBSelect(conn redis.Conn, db int) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "select", db)

	return
}

/**
 *  set key value
 */
func RedisSetKV(conn redis.Conn, key string, value string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "set", key, value)

	return
}

/**
 *  del key
 */
func RedisDelKey(conn redis.Conn, key string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "del", key)

	return
}

/**
 *  keys命令
 */
func RedisGetKeys(conn redis.Conn, pattern string) (strlist []string, err error) {
	reply, err := RedisCommand(conn, "keys", pattern)
	strlist, _ = redis.Strings(reply, nil)

	return
}

/**
 *  get key value
 */
func RedisGetKV(conn redis.Conn, key string) (value string, err error) {
	reply, err := RedisCommand(conn, "get", key)
	value, _ = redis.String(reply, nil)

	return
}

/**
 *  add sortset
 */
func RedisZAddSortSet(conn redis.Conn, sortsetName string, score float64, key string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "zadd", sortsetName, score, key)

	return
}

/**
 *  sortset rem
 */
func RedisZSortSetRem(conn redis.Conn, sortsetName string, key string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "zrem", sortsetName, key)

	return
}

/**
 *  sort正序
 */
func RedisSort(conn redis.Conn, sortsetName string, start int, stop int) (strlist []string, err error) {
	reply, err := RedisCommand(conn, "zrange", sortsetName, start, stop, "withscores")
	strlist, _ = redis.Strings(reply, nil)

	return
}

/**
 *  sort倒序
 */
func RedisReverseSort(conn redis.Conn, sortsetName string, start int, stop int) (strlist []string, err error) {
	reply, err := RedisCommand(conn, "zrevrange", sortsetName, start, stop, "withscores")
	strlist, _ = redis.Strings(reply, nil)

	return
}

/**
 *  rpush
 */
func RedisRPush(conn redis.Conn, queueName string, msg string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "rpush", queueName, msg)

	return
}

/**
 *  lpop
 */
func RedisLPop(conn redis.Conn, queueName string) (msg string, err error) {
	reply, err := RedisCommand(conn, "lpop", queueName)
	msg, _ = redis.String(reply, nil)

	return
}

/**
 *  incr
 */
func RedisIncr(conn redis.Conn, key string) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "incr", key)

	return
}

/**
 *  incrby
 */
func RedisIncrBy(conn redis.Conn, key string, num int64) (reply interface{}, err error) {
	reply, err = RedisCommand(conn, "incrby", key, num)

	return
}

/*
 * zrank
 */
func RedisZRank(conn redis.Conn, key string, member string) (reply interface{}, err error) {
	return RedisCommand(conn, "zrank", key, member)
}

/*
 * zrangebyscore
 */
func RedisZRangeByScore(conn redis.Conn, key string, startScore string, endScore string, limit string, limitStart int, limitEnd int) (reply []string, err error) {
	if limit == "true" {
		res, err := RedisCommand(conn, "zrangebyscore", key, startScore, endScore, "withscores", "limit", limitStart, limitEnd)
		reply, _ = redis.Strings(res, nil)
		return reply, err
	}
	res, err := RedisCommand(conn, "zrangebyscore", key, startScore, endScore, "withscores")
	reply, _ = redis.Strings(res, nil)
	return reply, err
}

/*
 * zrevrangebyscore
 */
func RedisZRevRangeByScore(conn redis.Conn, key string, startScore string, endScore string, limit string, limitStart int, limitEnd int) (reply []string, err error) {
	if limit == "true" {
		res, err := RedisCommand(conn, "zrevrangebyscore", key, startScore, endScore, "withscores", "limit", limitStart, limitEnd)
		reply, _ = redis.Strings(res, nil)
		return reply, err
	}
	res, err := RedisCommand(conn, "zrevrangebyscore", key, startScore, endScore, "withscores")
	reply, _ = redis.Strings(res, nil)
	return reply, err
}

/*
 * zscore
 */
func RedisZScore(conn redis.Conn, key string, member string) (reply interface{}, err error) {
	return RedisCommand(conn, "zscore", key, member)
}

/*
 * zcount
 */
func RedisZCount(conn redis.Conn, key string, min string, max string) (reply interface{}, err error) {
	return RedisCommand(conn, "zcount", key, min, max)
}