上传redis lua支持

This commit is contained in:
liwei1dao 2022-07-14 16:06:12 +08:00
parent 0a37601f1a
commit 9092120d56
5 changed files with 118 additions and 21 deletions

View File

@ -89,3 +89,21 @@ func (this *Redis) UnLock(key string) (err error) {
err = this.Delete(key) err = this.Delete(key)
return return
} }
//lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src)
return script.Load(this.getContext(), this.client)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
}

View File

@ -16,6 +16,7 @@ type (
Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) Pipeline(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error)
TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error) TxPipelined(ctx context.Context, fn func(pipe redis.Pipeliner) error) (err error)
Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error) Watch(ctx context.Context, fn func(*redis.Tx) error, keys ...string) (err error)
/*Key*/ /*Key*/
Delete(key string) (err error) Delete(key string) (err error)
ExistsKey(key string) (iskeep bool, err error) ExistsKey(key string) (iskeep bool, err error)
@ -112,6 +113,12 @@ type (
ZScore(key string, member string) (result float64, err error) ZScore(key string, member string) (result float64, err error)
ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) ZUnionStore(dest string, store *redis.ZStore) (result int64, err error)
ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error)
/*Lua Script*/
NewScript(src string) *redis.StringCmd
Eval(script string, keys []string, args ...interface{}) *redis.Cmd
EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd
ScriptExists(hashes ...string) *redis.BoolSliceCmd
} }
ISys interface { ISys interface {
@ -445,3 +452,17 @@ func ZUnionStore(dest string, store *redis.ZStore) (result int64, err error) {
func ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) { func ZScan(key string, _cursor uint64, match string, count int64) (keys []string, cursor uint64, err error) {
return defsys.ZScan(key, _cursor, match, count) return defsys.ZScan(key, _cursor, match, count)
} }
/*Lua Script*/
func NewScript(src string) *redis.StringCmd {
return defsys.NewScript(src)
}
func Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(script, keys, args...)
}
func EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return defsys.Eval(sha1, keys, args...)
}
func ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return defsys.ScriptExists(hashes...)
}

View File

@ -45,7 +45,6 @@ func (this *Redis) init() (err error) {
} }
return return
} }
func (this *Redis) Close() (err error) { func (this *Redis) Close() (err error) {
return this.client.Close() return this.client.Close()
} }
@ -67,15 +66,12 @@ func (this *Redis) Lock(key string, outTime int) (result bool, err error) {
func (this *Redis) UnLock(key string) (err error) { func (this *Redis) UnLock(key string) (err error) {
return this.client.UnLock(key) return this.client.UnLock(key)
} }
func (this *Redis) Delete(key string) (err error) { func (this *Redis) Delete(key string) (err error) {
return this.client.Delete(key) return this.client.Delete(key)
} }
func (this *Redis) ExistsKey(key string) (iskeep bool, err error) { func (this *Redis) ExistsKey(key string) (iskeep bool, err error) {
return this.client.ExistsKey(key) return this.client.ExistsKey(key)
} }
func (this *Redis) Expire(key string, expire time.Duration) (err error) { func (this *Redis) Expire(key string, expire time.Duration) (err error) {
return this.client.Expire(key, expire) return this.client.Expire(key, expire)
} }
@ -106,8 +102,6 @@ func (this *Redis) RenameNX(oldkey string, newkey string) (err error) {
func (this *Redis) Keys(pattern string) (keys []string, err error) { func (this *Redis) Keys(pattern string) (keys []string, err error) {
return this.client.Keys(pattern) return this.client.Keys(pattern)
} }
///获取键类型
func (this *Redis) Type(key string) (ty string, err error) { func (this *Redis) Type(key string) (ty string, err error) {
return this.client.Type(key) return this.client.Type(key)
} }
@ -355,6 +349,20 @@ func (this *Redis) ZScan(key string, _cursor uint64, match string, count int64)
return this.client.ZScan(key, _cursor, match, count) return this.client.ZScan(key, _cursor, match, count)
} }
//lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd {
return this.client.NewScript(src)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(hashes...)
}
//Codec--------------------------------------------------------------------------------------------------------------------------------------- //Codec---------------------------------------------------------------------------------------------------------------------------------------
func (this *Redis) Marshal(v interface{}) ([]byte, error) { func (this *Redis) Marshal(v interface{}) ([]byte, error) {
if this.options.Codec != nil { if this.options.Codec != nil {

View File

@ -88,3 +88,21 @@ func (this *Redis) UnLock(key string) (err error) {
err = this.Delete(key) err = this.Delete(key)
return return
} }
//lua Script
func (this *Redis) NewScript(src string) *redis.StringCmd {
script := redis.NewScript(src)
return script.Load(this.getContext(), this.client)
}
func (this *Redis) Eval(script string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.Eval(this.getContext(), script, keys, args...)
}
func (this *Redis) EvalSha(sha1 string, keys []string, args ...interface{}) *redis.Cmd {
return this.client.EvalSha(this.getContext(), sha1, keys, args...)
}
func (this *Redis) ScriptExists(hashes ...string) *redis.BoolSliceCmd {
return this.client.ScriptExists(this.getContext(), hashes...)
}
func (this *Redis) ScriptLoad(script string) *redis.StringCmd {
return this.client.ScriptLoad(this.getContext(), script)
}

View File

@ -3,6 +3,7 @@ package redis_test
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log"
"os" "os"
"sync" "sync"
"testing" "testing"
@ -181,25 +182,56 @@ func Test_Redis_Encoder_int(t *testing.T) {
} }
func Test_Redis_Encoder_Hash(t *testing.T) { func Test_Redis_Encoder_Hash(t *testing.T) {
// err := redis.HMSet("test:1005", &TestData{Name: "liwei1dao", Agr: 12, Sub: &TestAny{SubName: "test", Age: 20}})
// fmt.Printf("err:%v\n", err)
// data := &TestData{}
// err = redis.HGetAll("test:1005", data)
// fmt.Printf("data:%v err:%v\n", data, err)
// redis.HSet("test:1003", "Name", "eeee")
name := "" name := ""
err := redis.HGet("test:103", "name", &name) err := redis.HGet("test:103", "name", &name)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
fmt.Println(name) fmt.Println(name)
}
// data1 := map[string]*TestData{"li_1": {Name: "liwei2dao", Agr: 56}, "li_2": {Name: "liwei3dao", Agr: 78}}
// err := redis.HMSet("test:1004", data1) //测试redis lua 脚本
// fmt.Printf("err:%v\n", err) func Test_Redis_Lua(t *testing.T) {
// data2 := make(map[string]*TestData) script := redis.NewScript(`
// err = redis.HGetAll("test:1004", data2) local goodsSurplus
// fmt.Printf("data2:%v err:%v\n", data2, err) local flag
local existUserIds = tostring(KEYS[1])
local memberUid = tonumber(ARGV[1])
local goodsSurplusKey = tostring(KEYS[2])
local hasBuy = redis.call("sIsMember", existUserIds, memberUid)
if hasBuy ~= 0 then
return 0
end
goodsSurplus = redis.call("GET", goodsSurplusKey)
if goodsSurplus == false then
return 0
end
-- 没有剩余可抢购物品
goodsSurplus = tonumber(goodsSurplus)
if goodsSurplus <= 0 then
return 0
end
flag = redis.call("SADD", existUserIds, memberUid)
flag = redis.call("DECR", goodsSurplusKey)
return 1
`)
sha, err := script.Result()
if err != nil {
log.Fatalln(err)
}
ret := redis.EvalSha(sha, []string{
"hadBuyUids",
"goodsSurplus",
}, "userId")
if result, err := ret.Result(); err != nil {
log.Fatalf("Execute Redis fail: %v", err.Error())
} else {
fmt.Println("")
fmt.Printf("userid: %s, result: %d", "userId", result)
}
} }