@ -1,3 +0,0 @@ | |||
# redis | |||
redis操作 |
@ -1,18 +0,0 @@ | |||
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) | |||
} |
@ -0,0 +1,22 @@ | |||
package redis | |||
import ( | |||
redisdb "github.com/gomodule/redigo/redis" | |||
) | |||
/** | |||
* 设置有效期 | |||
*/ | |||
func SetExpire(key string, expire int64) (int64, error) { | |||
c := GetConn() | |||
var err error | |||
var reply interface{} | |||
reply, err = c.Do("expire", key, expire) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return redisdb.Int64(reply, err) | |||
} |
@ -1,5 +1,9 @@ | |||
module git.tetele.net/tgo/redis | |||
module git.tetele.net/yueheng/redis | |||
go 1.14 | |||
go 1.17 | |||
require github.com/gomodule/redigo v2.0.0+incompatible // indirect | |||
require ( | |||
git.tetele.net/tgo/conf v0.47.0 | |||
git.tetele.net/tgo/helper v0.3.1 | |||
github.com/gomodule/redigo v1.8.8 | |||
) |
@ -1,3 +1,16 @@ | |||
github.com/gomodule/redigo v1.7.0 h1:ZKld1VOtsGhAe37E7wMxEDgAlGM5dvFY+DiOhSkhP9Y= | |||
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0= | |||
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= | |||
git.tetele.net/tgo/conf v0.47.0 h1:2nGvXjyFV6gO7BPliG4lwERP/bgTOeSJpUv/I2ALGPg= | |||
git.tetele.net/tgo/conf v0.47.0/go.mod h1:AWVIBEDE5dtotthUgR0SWaR2Qa6/f+O5WQ3s7Tj8q7A= | |||
git.tetele.net/tgo/helper v0.3.1 h1:5+6xK6nr2BB1A0XVKrrA8R/GW0y3aUuKUIcz3zgAwZo= | |||
git.tetele.net/tgo/helper v0.3.1/go.mod h1:89mQwyfqZ+t8YXiVwzSxA70gLlUNqoZGDEUxvV46jXk= | |||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | |||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | |||
github.com/gomodule/redigo v1.8.8 h1:f6cXq6RRfiyrOJEV7p3JhLDlmawGBVBBP1MggY8Mo4E= | |||
github.com/gomodule/redigo v1.8.8/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE= | |||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | |||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | |||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | |||
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= | |||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | |||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | |||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= | |||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
@ -1,44 +0,0 @@ | |||
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) | |||
// if err == nil { | |||
// t.Log("nil", err) | |||
// } else { | |||
// t.Log("error:", err) | |||
// } | |||
// ret2, err := HSetExpire("testing2", 200) | |||
// t.Log(ret2) | |||
// t.Log(err) | |||
ret3, err := HMSet("testing2", "test1", "test1") | |||
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) | |||
} |
@ -0,0 +1,58 @@ | |||
package redis | |||
import ( | |||
"log" | |||
"time" | |||
"git.tetele.net/tgo/conf" | |||
redisdb "github.com/gomodule/redigo/redis" | |||
) | |||
// 定义redis链接池 | |||
var Pool *redisdb.Pool | |||
// func init() { | |||
// if Pool == nil { | |||
// RedisInit() | |||
// } | |||
// } | |||
// 初始化redis链接池 | |||
func RedisInit(max ...int) { | |||
var MaxActive, MaxIdle int | |||
if len(max) > 0 { | |||
MaxActive = max[0] | |||
} | |||
if len(max) > 1 { | |||
MaxIdle = max[1] | |||
} | |||
Pool = &redisdb.Pool{ | |||
MaxIdle: MaxIdle, /*最大的空闲连接数*/ | |||
MaxActive: MaxActive, /*最大的激活连接数*/ | |||
Dial: redisConn, | |||
} | |||
} | |||
func redisConn() (redisdb.Conn, error) { | |||
var url string = conf.REDIS_SERVER | |||
if url == "" { | |||
url = "127.0.0.1:6379" | |||
} | |||
dbOption := redisdb.DialDatabase(0) | |||
pwOption := redisdb.DialPassword("") | |||
// **重要** 设置读写超时 | |||
readTimeout := redisdb.DialReadTimeout(time.Second * time.Duration(2)) | |||
writeTimeout := redisdb.DialWriteTimeout(time.Second * time.Duration(5)) | |||
conTimeout := redisdb.DialConnectTimeout(time.Second * time.Duration(2)) | |||
c, err := redisdb.Dial("tcp", url, dbOption, pwOption, readTimeout, writeTimeout, conTimeout) | |||
if err != nil { | |||
log.Println("redis server connect failed", err) | |||
return nil, err | |||
} | |||
return c, nil | |||
} |
@ -1,17 +0,0 @@ | |||
package redis | |||
func Incr(key string) (interface{}, error) { | |||
c := pool.Get() | |||
defer c.Close() | |||
return c.Do("INCR", key) | |||
} | |||
func Decr(key string) (interface{}, error) { | |||
c := pool.Get() | |||
defer c.Close() | |||
return c.Do("DECR", key) | |||
} |
@ -1,13 +0,0 @@ | |||
package redis | |||
import ( | |||
"testing" | |||
) | |||
func Test_Incr(t *testing.T) { | |||
reply, err := Incr("xxx") | |||
t.Log(reply, err) | |||
reply, err := Decr("xxx") | |||
t.Log(reply, err) | |||
} |
@ -0,0 +1,80 @@ | |||
package redis | |||
import ( | |||
redisdb "github.com/gomodule/redigo/redis" | |||
) | |||
//list 尾部增加值 | |||
func Rpush(key string, field interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("RPUSH", key, field) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return redisdb.Int64(reply, nil) | |||
} | |||
//list 头部增加值 | |||
func Lpush(key string, field interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("LPUSH", key, field) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return redisdb.Int64(reply, nil) | |||
} | |||
//list 长度 | |||
func Llen(key string) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("LLEN", key) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
return redisdb.Int64(reply, nil) | |||
} | |||
//list 通过索引设置列表元素的值 LSET key index value | |||
func Lset(key string, index int, value interface{}) (interface{}, error) { | |||
c := GetConn() | |||
reply, err := c.Do("LSET", key, index, value) | |||
CloseConn(c) | |||
return reply, err | |||
} | |||
/* | |||
LRANGE key start stop | |||
获取列表指定范围内的元素 | |||
*/ | |||
func Lrange(key string, start, stop int64) ([][]byte, error) { | |||
c := GetConn() | |||
ret, err := c.Do("LRANGE", key, start, stop) | |||
reply := make([][]byte, 0) | |||
if err == nil { | |||
reply, err = redisdb.ByteSlices(ret, err) | |||
} | |||
CloseConn(c) | |||
return reply, err | |||
} |
@ -0,0 +1,41 @@ | |||
package redis | |||
import ( | |||
redisdb "github.com/gomodule/redigo/redis" | |||
) | |||
/** | |||
* key增加 | |||
*/ | |||
func Incrby(key string, value interface{}) (int64, error) { | |||
c := GetConn() | |||
var err error | |||
var reply interface{} | |||
reply, err = c.Do("INCRBY", key, value) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
ret, err := redisdb.Int64(reply, err) | |||
return ret, err | |||
} | |||
/** | |||
* key减 | |||
*/ | |||
func Decrby(key string, value interface{}) (int64, error) { | |||
c := GetConn() | |||
var err error | |||
var reply interface{} | |||
reply, err = c.Do("DECRBY", key, value) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} | |||
ret, err := redisdb.Int64(reply, err) | |||
return ret, err | |||
} |
@ -0,0 +1,175 @@ | |||
package redis | |||
import ( | |||
"errors" | |||
redisdb "github.com/gomodule/redigo/redis" | |||
) | |||
/* | |||
* 存值 | |||
* key 域 | |||
* value 值 | |||
* 2021/08/31 | |||
*/ | |||
func SAdd(key string, value interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("SADD", key, value) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
/* | |||
* 删除hash值 | |||
*/ | |||
func SRem(key, value interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("SREM", key, value) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
/** | |||
* 集合键是否存在 | |||
* key 域 | |||
* field 名 | |||
*/ | |||
func SIsmember(key, value interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("SISMEMBER", key, value) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
/** | |||
* 集合成员数量 | |||
*/ | |||
func SCard(key interface{}) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("SCard", key) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
//hash取值,返回[]byte | |||
func SMembers(key string) ([]byte, error) { | |||
c := GetConn() | |||
ret, err := c.Do("SMEMBERS", key) | |||
reply := make([]byte, 0) | |||
if err == nil { | |||
reply, err = redisdb.Bytes(ret, err) | |||
} | |||
CloseConn(c) | |||
return reply, err | |||
} | |||
func ZAdd(key, score, member string) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("ZADD", key, score, member) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
func ZIncrBy(key, inc, member string) (int64, error) { | |||
c := GetConn() | |||
reply, err := c.Do("ZINCRBY", key, inc, member) | |||
CloseConn(c) | |||
if err != nil { | |||
return 0, err | |||
} else { | |||
return redisdb.Int64(reply, nil) | |||
} | |||
} | |||
func ZRange(key string, start, stop int64) ([]map[string]string, error) { | |||
c := GetConn() | |||
defer CloseConn(c) | |||
values, err := redisdb.Values(c.Do("ZRANGE", key, start, stop, "WITHSCORES")) | |||
if err != nil { | |||
return nil, err | |||
} | |||
if len(values)%2 != 0 { | |||
return nil, errors.New("redigo: ZRange expects even number of values result") | |||
} | |||
var l []map[string]string | |||
for i := 0; i < len(values); i += 2 { | |||
key, okKey := values[i].([]byte) | |||
value, okValue := values[i+1].([]byte) | |||
if !okKey || !okValue { | |||
return nil, errors.New("redigo: ZRange key not a bulk string value") | |||
} | |||
l = append(l, map[string]string{ | |||
"member": string(key), | |||
"score": string(value), | |||
}) | |||
} | |||
return l, nil | |||
} | |||
func ZScan(key, field string, len int64) ([]string, error) { | |||
c := GetConn() | |||
defer CloseConn(c) | |||
zscanResult, err := redisdb.Values(c.Do("ZSCAN", key, "0", "MATCH", field, "COUNT", len)) | |||
if err != nil { | |||
return nil, err | |||
} | |||
var cursor int | |||
var membersAndScores []string | |||
var member []string | |||
redisdb.Scan(zscanResult, &cursor, &membersAndScores) | |||
for i, v := range membersAndScores { | |||
if i%2 == 0 { | |||
member = append(member, v) | |||
} | |||
} | |||
return member, nil | |||
} |
@ -1,23 +0,0 @@ | |||
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) | |||
p,err := pool. | |||
} |
@ -1,71 +0,0 @@ | |||
package redis | |||
import ( | |||
"strconv" | |||
"testing" | |||
// "time" | |||
) | |||
var succ []int | |||
func Test_Watch(t *testing.T) { | |||
var key string = "tttttt" | |||
// var tx *Tx | |||
var count int | |||
for i := 0; i < 100000; i++ { | |||
// t.Log(i) | |||
go func(i int) { | |||
// t.Log(i) | |||
tx, err := NewTx() | |||
// t.Log(tx, err) | |||
if err == nil { | |||
reply, err := tx.Watch(key) | |||
// t.Log("watch:", reply, err) | |||
count_str, err := Get(key) | |||
count, _ = strconv.Atoi(count_str) | |||
// t.Log("count:", count, err) | |||
reply, err = tx.Multi() | |||
// t.Log("Multi:", reply, err) | |||
if count > 0 && err == nil { | |||
// t.Log("start decr,", i) | |||
reply, err = tx.Decr(key) | |||
// t.Log("Decr:", reply, err) | |||
reply, err = tx.Exec() | |||
// t.Log("Exec:", exec_reply, err, i) | |||
if err == nil && reply != nil { | |||
// reply, err = HSet("succ", i, i) | |||
succ = append(succ, i) | |||
t.Log("succ:", i, succ) | |||
} | |||
} | |||
err = tx.Close() | |||
// t.Log("Close:", err) | |||
} | |||
}(i) | |||
} | |||
t.Log(succ) | |||
} |