From 71d204406ca1f0530aba107193ce3b6074975f49 Mon Sep 17 00:00:00 2001 From: guzeng Date: Fri, 5 Feb 2021 16:29:59 +0800 Subject: [PATCH] init --- conn.go | 101 +++++++++++++++++++++++++++++++++++++++ conn_test.go | 18 +++++++ hash.go | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ hash_test.go | 40 ++++++++++++++++ string.go | 77 ++++++++++++++++++++++++++++++ string_test.go | 21 ++++++++ 6 files changed, 384 insertions(+) create mode 100644 conn.go create mode 100644 conn_test.go create mode 100644 hash.go create mode 100644 hash_test.go create mode 100644 string.go create mode 100644 string_test.go diff --git a/conn.go b/conn.go new file mode 100644 index 0000000..0bf113f --- /dev/null +++ b/conn.go @@ -0,0 +1,101 @@ +package redis + +import ( + redisdb "github.com/gomodule/redigo/redis" +) + +// 定义redis链接池 +var pool *redisdb.Pool + +// 初始化redis链接池 +func init() { + pool = &redisdb.Pool{ + MaxIdle: 500, /*最大的空闲连接数*/ + MaxActive: 10000, /*最大的激活连接数*/ + Dial: conn, + } +} + +func conn() (redisdb.Conn, error) { + c, err := redisdb.Dial("tcp", "127.0.0.1:6379") + if err != nil { + return nil, err + } + return c, nil +} + +// // 向key的hash中添加元素field的值 +// func HashSet(key, field string, data interface{}) { +// err := client.HSet(key, field, data) +// if err != nil { +// logger.Error("Redis HSet Error:", err) +// } +// } + +// // 批量向key的hash添加对应元素field的值 +// func BatchHashSet(key string, fields map[string]interface{}) string { +// val, err := client.HMSet(key, fields).Result() +// if err != nil { +// logger.Error("Redis HMSet Error:", err) +// } +// return val +// } + +// // 通过key获取hash的元素值 +// func HashGet(key, field string) string { +// result := "" +// val, err := client.HGet(key, field).Result() +// if err == redis.Nil { +// logger.Debug("Key Doesn't Exists:", field) +// return result +// } else if err != nil { +// logger.Error("Redis HGet Error:", err) +// return result +// } +// return val +// } + +// // 批量获取key的hash中对应多元素值 +// func BatchHashGet(key string, fields ...string) map[string]interface{} { +// resMap := make(map[string]interface{}) +// for _, field := range fields { +// var result interface{} +// val, err := client.HGet(key, fmt.Sprintf("%s", field)).Result() +// if err == redis.Nil { +// logger.Debug("Key Doesn't Exists:", field) +// resMap[field] = result +// } else if err != nil { +// logger.Error("Redis HMGet Error:", err) +// resMap[field] = result +// } +// if val != "" { +// resMap[field] = val +// } else { +// resMap[field] = result +// } +// } +// return resMap +// } + +// // 获取自增唯一ID +// func Incr(key string) int { +// val, err := client.Incr(key).Result() +// if err != nil { +// logger.Error("Redis Incr Error:", err) +// } +// return int(val) +// } + +// // 添加集合数据 +// func SetAdd(key, val string) { +// client.SAdd(key, val) +// } + +// // 从集合中获取数据 +// func SetGet(key string) []string { +// val, err := client.SMembers(key).Result() +// if err != nil { +// logger.Error("Redis SMembers Error:", err) +// } +// return val +// } diff --git a/conn_test.go b/conn_test.go new file mode 100644 index 0000000..e906d02 --- /dev/null +++ b/conn_test.go @@ -0,0 +1,18 @@ +package redis + +import ( + "testing" +) + +func Test_SetAdd(t *testing.T) { + + reply, err := Set(123, "不知道433ee4") + + t.Log(reply) + t.Log(err) + + ret, err := Get("123") + + t.Log(ret) + t.Log(err) +} diff --git a/hash.go b/hash.go new file mode 100644 index 0000000..6a53f5e --- /dev/null +++ b/hash.go @@ -0,0 +1,127 @@ +package redis + +import ( + // "errors" + + redisdb "github.com/gomodule/redigo/redis" +) + +//hash取值, 返回interface{} + +func HGet(key string, field interface{}) (interface{}, error) { + c := pool.Get() + defer c.Close() + + return c.Do("HGET", key, field) +} + +//hash取值, 返回字符串 + +func HGetString(key string, field interface{}) (string, error) { + c := pool.Get() + defer c.Close() + + ret, err := c.Do("HGET", key, field) + if err != nil { + return "", err + } + return redisdb.String(ret, err) +} + +//hash取值,返回接口类型 + +func HGetStringMap(key string, field interface{}) (map[string]string, error) { + c := pool.Get() + defer c.Close() + + ret, err := c.Do("HGET", key, field) + if err != nil { + return ret.(map[string]string), err + } + return redisdb.StringMap(ret, err) + +} + +//hash取值,返回[]byte + +func HGetBytes(key string, field interface{}) ([]byte, error) { + c := pool.Get() + defer c.Close() + + ret, err := c.Do("HGET", key, field) + if err != nil { + return nil, err + } + return redisdb.Bytes(ret, err) + +} + +//hash取所有值 + +func HGetAll(key string) ([][]byte, error) { + c := pool.Get() + defer c.Close() + + ret, err := c.Do("HGETAll", key) + if err != nil { + return nil, err + } + // return ret, err + // return redisdb.MultiBulk(ret, err) + return redisdb.ByteSlices(ret, err) + +} + +/* + * hash存值, + * key 域 + * field 名 + * value 值 + * 2020/06/06 + */ +func HSet(key string, field, value interface{}) (interface{}, error) { + c := pool.Get() + defer c.Close() + + reply, err := c.Do("HSET", key, field, value) + + return reply, err +} + +/* + * 删除hash值 + */ +func HDel(key,field string) error{ + c := pool.Get() + defer c.Close() + + _, err := c.Do("HDEL", key, field) + + return err +} + +/* + * hash批量存值, + * args (key,field,value,field,value,field,value...) 域,名,值,名,值,名,值 ... + * 2020/06/06 + */ +func HMSet(args ...interface{}) (interface{}, error) { + c := pool.Get() + defer c.Close() + + reply, err := c.Do("HMSET", args...) + + return reply, err +} + +/* + * hash存值, + * key 域 + * expire 过期时长 + * 2020/06/06 + */ +func HSetExpire(key string, expire int) (interface{}, error) { + c := pool.Get() + defer c.Close() + return c.Do("expire", key, expire) +} diff --git a/hash_test.go b/hash_test.go new file mode 100644 index 0000000..41ad2b4 --- /dev/null +++ b/hash_test.go @@ -0,0 +1,40 @@ +package redis + +import ( + // "strconv" + "testing" + // "tgo/helper" +) + +func Test_HSet(t *testing.T) { + + // val := map[string]interface{}{"id": "123", "name": "这是一个测试", "dis": "xxx"} + + // reply, err := HSet("testing2", "test1", val) + + // t.Log(reply) + // t.Log(err) + + // ret, err := HGetString("testing2", "test1") + + // t.Log(ret) + // t.Log(err) + + // ret2, err := HSetExpire("testing2", 200) + // t.Log(ret2) + // t.Log(err) + + // ret3, err := HMSet("testing2", "test1", "test1", "test2", "test2", "test3", "test3") + // t.Log(ret3) + // t.Log(err) + + ret4, err := HGetAll("1000064_product") + // ret := ret4.([]interface{}) + // t.Log() + for _, val := range ret4 { + // str := strconv.FormatUint(val.([]uint64), 10) + // t.Log(helper.ToStr(val)) + t.Log(string(val)) + } + t.Log(err) +} diff --git a/string.go b/string.go new file mode 100644 index 0000000..cdeb72e --- /dev/null +++ b/string.go @@ -0,0 +1,77 @@ +package redis + +import ( + "errors" + + redisdb "github.com/gomodule/redigo/redis" +) + +//取值 +func Get(key string) (string, error) { + c := pool.Get() + defer c.Close() + + return redisdb.String(c.Do("get", key)) +} + +/* + * 存值, + * args,范围(key, value)或(key, value,expire) + * 2020/06/06 + */ +func Set(args ...interface{}) (interface{}, error) { + c := pool.Get() + defer c.Close() + if len(args) < 2 { + return nil, errors.New("参数错误,至少需两个参数") + } + var err error + var reply interface{} + if len(args) > 2 { + reply, err = c.Do("SET", args[0], args[1], "EX", args[2]) //设置过期时间 + } else { + reply, err = c.Do("SET", args[0], args[1]) + } + return reply, err +} + +/** + * 删除key + */ +func Del(key string) (int,error){ + c := pool.Get() + defer c.Close() + var err error + var reply interface{} + reply, err = c.Do("DEL", key) + return redisdb.Int(reply, err) +} + +/** + * 设锁 + * 2020/01/15 + */ +func SetLock(args ...interface{}) (bool,error){ + c := pool.Get() + defer c.Close() + if len(args) < 2 { + return false, errors.New("参数错误,至少需两个参数") + } + var err error + var result interface{} + if len(args) > 2 { + result, err = c.Do("SETNX", args[0], args[1], "EX", args[2]) //设置过期时间 + } else { + result, err = c.Do("SETNX", args[0], args[1]) + } + + if err != nil{ + return false,err + } + + if result.(int) == 1{ + return true, nil + } else{ + return false, nil + } +} \ No newline at end of file diff --git a/string_test.go b/string_test.go new file mode 100644 index 0000000..877fe81 --- /dev/null +++ b/string_test.go @@ -0,0 +1,21 @@ +package redis + +import ( + "testing" + // "time" +) + +func Test_Get(t *testing.T) { + + reply, err := Get("siteListCache") + + t.Log(reply) + t.Log(err) +} + +func Test_Set(t *testing.T) { + + c, err := Set("test", 1111, 7200) + t.Log(c) + t.Log(err) +}