Browse Source

init

master v0.1.0
guzeng 3 years ago
parent
commit
71d204406c
6 changed files with 384 additions and 0 deletions
  1. +101
    -0
      conn.go
  2. +18
    -0
      conn_test.go
  3. +127
    -0
      hash.go
  4. +40
    -0
      hash_test.go
  5. +77
    -0
      string.go
  6. +21
    -0
      string_test.go

+ 101
- 0
conn.go View File

@ -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
// }

+ 18
- 0
conn_test.go View File

@ -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)
}

+ 127
- 0
hash.go View File

@ -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)
}

+ 40
- 0
hash_test.go View File

@ -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)
}

+ 77
- 0
string.go View File

@ -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
}
}

+ 21
- 0
string_test.go View File

@ -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)
}

Loading…
Cancel
Save