Tao Zhang 5 роки тому
батько
коміт
e02c433c19
1 змінених файлів з 36 додано та 74 видалено
  1. 36 74
      redisutil.go

+ 36 - 74
redisutil.go

@@ -19,13 +19,13 @@ import (
 // RedisPool redis 多服务端连接池,1个应用同时连接多个redis服务
 var RedisPool map[string]*redigo.Pool
 
-// InitRedis 初始化redis 多端连接池 <br/>
+// 初始化redis 多端连接池 <br/>
 // @param addrs enterprise=192.168.3.14:1379,service=192.168.3.14:2379,other=192.168.3.14:3379
 func InitRedis(addrs string) {
 	InitRedisBySize(addrs, 300, 30, 240)
 }
 
-//初始化redis连接池,支持多个redis库
+// 初始化redis连接池,支持多个redis库
 func InitRedisBySize(addrs string, maxSize, maxIdle, timeout int) {
 	RedisPool = map[string]*redigo.Pool{}
 	addr := strings.Split(addrs, ",")
@@ -38,14 +38,17 @@ func InitRedisBySize(addrs string, maxSize, maxIdle, timeout int) {
 	}
 }
 
-// PutKV  分流redis ,并存入字符串缓存
-// Output: bool 是否存储成功
+// 分流redis ,并存入字符串缓存
 func PutKV(key string, obj interface{}) bool {
 	return Put("other", key, obj, -1)
 }
+
+// 存储KV对,并设置永不超时
 func PutCKV(code, key string, obj interface{}) bool {
 	return Put(code, key, obj, -1)
 }
+
+// 存储KV对,可设置超时时间,单位秒
 func Put(code, key string, obj interface{}, timeout int) bool {
 	b := false
 	defer catch()
@@ -70,6 +73,7 @@ func Put(code, key string, obj interface{}, timeout int) bool {
 	return b
 }
 
+// 批量存储KV对,可指定超时时间,obj格式=[[key,value],,,]
 func BulkPut(code string, timeout int, obj ...interface{}) bool {
 	b := false
 	defer catch()
@@ -103,7 +107,7 @@ func BulkPut(code string, timeout int, obj ...interface{}) bool {
 	return b
 }
 
-//直接存字节流
+// 存储KV对,Value是字节数组,可指定超时时间
 func PutBytes(code, key string, data *[]byte, timeout int) (err error) {
 	defer catch()
 
@@ -121,7 +125,7 @@ func PutBytes(code, key string, data *[]byte, timeout int) (err error) {
 	return
 }
 
-//设置超时时间,单位秒
+// 设置超时时间,单位秒
 func SetExpire(code, key string, expire int) error {
 	defer catch()
 
@@ -131,7 +135,7 @@ func SetExpire(code, key string, expire int) error {
 	return err
 }
 
-//判断一个key是否存在
+// 判断一个key是否存在
 func Exists(code, key string) (bool, error) {
 	defer catch()
 
@@ -142,19 +146,20 @@ func Exists(code, key string) (bool, error) {
 	return ret == 1, err
 }
 
-//获取string
+// 依据Key,获取Value 自动转化为字符串格式
 func GetStr(code, key string) string {
 	res := Get(code, key)
 	str, _ := res.(string)
 	return str
 }
 
-//获取int
+// 依据Key,获取Value 自动转化为int格式
 func GetInt(code, key string) int {
 	result, _ := GetNewInt(code, key)
 	return result
 }
 
+// 依据Key,获取Value 兼容int类型数据将自动转换
 func GetNewInt(code, key string) (int, error) {
 	var res interface{}
 	err := GetNewInterface(code, key, &res)
@@ -165,16 +170,20 @@ func GetNewInt(code, key string) (int, error) {
 	return result, err
 }
 
-//取得字符串,支持变参,2个 (key,code),返回后自己断言
+// 依据Key,获取字符串,返回interface{},需要自己断言
 func Get(code, key string) (result interface{}) {
 	GetInterface(code, key, &result)
 	return
 }
 
+// NOTE: 建议使用GetNewInterface
+// 依据Key,获取字符串,返回interface{} 为兼容老代码所写
 func GetInterface(code, key string, result interface{}) {
 	GetNewInterface(code, key, result)
 }
 
+// NOTE:注意result必须为指针对象
+// 依据Key,获取字符串,返回interface{} ,基础方法,部分外部接口依赖此方法
 func GetNewInterface(code, key string, result interface{}) error {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -195,7 +204,7 @@ func GetNewInterface(code, key string, result interface{}) error {
 	return err
 }
 
-//直接返回字节流
+// 依据Key,获取字节数组
 func GetBytes(code, key string) (ret *[]byte, err error) {
 	defer catch()
 
@@ -214,6 +223,9 @@ func GetBytes(code, key string) (ret *[]byte, err error) {
 	}
 	return
 }
+
+// FIXME: 写注释,表示很忧伤,GetNewBytes/GetBytes的实现,没看出来差异啊
+// 依据Key,获取字节数组
 func GetNewBytes(code, key string) (ret *[]byte, err error) {
 	defer catch()
 	redisPool := RedisPool[code]
@@ -236,7 +248,7 @@ func GetNewBytes(code, key string) (ret *[]byte, err error) {
 	return
 }
 
-//删所有key
+// 删所有key,清理Redis库
 func FlushDB(code string) bool {
 	b := false
 	defer catch()
@@ -254,7 +266,8 @@ func FlushDB(code string) bool {
 	return b
 }
 
-//支持删除多个key
+// 批量删除多个key
+// NOTE: key 为变参
 func Del(code string, key ...interface{}) bool {
 	defer catch()
 	b := false
@@ -271,28 +284,8 @@ func Del(code string, key ...interface{}) bool {
 	return b
 }
 
-//
-//func GetCacheByMgo(key, coll, sql, show string, ftype, timeout int) map[string]interface{} {
-//	obj := Get("", key)
-//	if nil == obj {
-//		switch ftype {
-//		case 1:
-//			m := mongodb.FindById(coll, sql, show)
-//			if m != nil && len(*m) > 0 {
-//				Put("", key, *m, timeout)
-//			}
-//			return *m
-//			break
-//		}
-//		return nil
-//	} else {
-//		res, _ := obj.(map[string]interface{})
-//		return res
-//	}
-//
-//}
-
-//根据代码和前辍key删除多个
+// 根据key前辍,批量删除
+// NOTE: key前缀 如:key*
 func DelByCodePattern(code, key string) {
 	defer catch()
 
@@ -310,40 +303,7 @@ func DelByCodePattern(code, key string) {
 	}
 }
 
-/**
-func DelByPattern(key string) {
-	defer func() {
-		if r := recover(); r != nil {
-			log.Println("[E]", r)
-			for skip := 1; ; skip++ {
-				_, file, line, ok := runtime.Caller(skip)
-				if !ok {
-					break
-				}
-				go log.Printf("%v,%v\n", file, line)
-			}
-		}
-	}()
-	i := 0
-	for _, v := range RedisPool {
-		conn := v.Get()
-		defer conn.Close()
-		ret, err := conn.Do("KEYS", key)
-		var result []interface{}
-		if nil != err {
-			log.Println("redisutil-GetError", err)
-		} else {
-			result = ret.([]interface{})
-			for k := 0; k < len(result); k++ {
-				delByNum(i, string(result[k].([]uint8)))
-			}
-		}
-		i++
-	}
-
-}
-**/
-//自增计数器
+// 指定key,自增计数,并返回增加后的值
 func Incr(code, key string) int64 {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -361,7 +321,7 @@ func Incr(code, key string) int64 {
 	return 0
 }
 
-//自减
+// 指定key,自减计数,并返回增加后的值
 func Decrby(code, key string, val int) int64 {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -379,7 +339,7 @@ func Decrby(code, key string, val int) int64 {
 	return 0
 }
 
-//根据正则去取
+// 根据正则key,获取结果
 func GetKeysByPattern(code, key string) []interface{} {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -394,7 +354,7 @@ func GetKeysByPattern(code, key string) []interface{} {
 	}
 }
 
-//批量取多个key
+// 批量取多个key的值
 func Mget(code string, key []string) []interface{} {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -413,7 +373,7 @@ func Mget(code string, key []string) []interface{} {
 	}
 }
 
-//取出并删除Key
+// 常规KV,出栈操作,查询指定Key,返回Value后,删除此KV对
 func Pop(code string, key string) (result interface{}) {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -435,7 +395,7 @@ func Pop(code string, key string) (result interface{}) {
 	return
 }
 
-//list操作
+// List数据 KV,队首出栈操作,对应RPOP队尾出栈
 func LPOP(code, list string) (result interface{}) {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -452,6 +412,7 @@ func LPOP(code, list string) (result interface{}) {
 	return
 }
 
+// List数据 后端入栈,在List末尾追加数据 对应LPUSH在队首插入数据
 func RPUSH(code, list string, val interface{}) bool {
 	defer catch()
 	conn := RedisPool[code].Get()
@@ -465,6 +426,7 @@ func RPUSH(code, list string, val interface{}) bool {
 	return true
 }
 
+// List数据长度
 func LLEN(code, list string) int64 {
 	defer catch()
 	conn := RedisPool[code].Get()