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