242 lines
5.3 KiB
Go
242 lines
5.3 KiB
Go
package db
|
|
|
|
/*
|
|
Redis Lua 脚本备份
|
|
*/
|
|
|
|
//Redis 自定义脚本 批量读取列表数据
|
|
var LuaScriptgetList = `
|
|
local key = tostring(KEYS[1])
|
|
local keys = redis.call("HGETALL", key)
|
|
local data = {}
|
|
local n = 1
|
|
for i, v in ipairs(keys) do
|
|
if i%2 == 0 then
|
|
data[n] = redis.call("HGETALL", v)
|
|
n = n+1
|
|
end
|
|
end
|
|
return data
|
|
`
|
|
|
|
//Redis 自定义脚本 批量写入列表数据
|
|
var LuaScriptsetList = `
|
|
local n = 1
|
|
for i, v in ipairs(KEYS) do
|
|
local key = v
|
|
local argv = {}
|
|
for i=n,#ARGV,1 do
|
|
n = n+1
|
|
if ARGV[i] == "#end" then
|
|
redis.call("HMSet", key,unpack(argv))
|
|
break
|
|
else
|
|
table.insert(argv, ARGV[i])
|
|
end
|
|
end
|
|
end
|
|
return "OK"
|
|
`
|
|
|
|
//Redis 自定义脚本 批量卸载列表数据
|
|
var LuaScriptdelList = `
|
|
local key = tostring(KEYS[1])
|
|
local keys = redis.call("HGETALL", key)
|
|
for i, v in ipairs(keys) do
|
|
if i%2 == 0 then
|
|
redis.call("DEL", v)
|
|
end
|
|
end
|
|
redis.call("DEL", key)
|
|
return "OK"
|
|
`
|
|
|
|
//Redis 自定义脚本 批量读取队列数据
|
|
var LuaScriptgetQueue = `
|
|
local key = tostring(KEYS[1])
|
|
local count = tonumber(ARGV[1]) * -1
|
|
local keys = redis.call("LRANGE", key,count,-1)
|
|
local data = {}
|
|
for i, v in ipairs(keys) do
|
|
data[i] = redis.call("HGETALL", v)
|
|
end
|
|
return data
|
|
`
|
|
|
|
//Redis 自定义脚本 批量写入队列数据
|
|
var LuaScriptsetQueue = `
|
|
local count = tonumber(ARGV[1])
|
|
local k = tostring(ARGV[3])
|
|
local keys = {}
|
|
local out = {}
|
|
local n = 1
|
|
for i, v in ipairs(KEYS) do
|
|
if (i == 1) then
|
|
for i=n,#ARGV,1 do
|
|
n = n+1
|
|
if ARGV[i] == "#end" then
|
|
break
|
|
end
|
|
end
|
|
elseif (i == 2) then
|
|
for i=n,#ARGV,1 do
|
|
n = n+1
|
|
if ARGV[i] == "#end" then
|
|
break
|
|
end
|
|
end
|
|
else
|
|
local key = v
|
|
local argv = {}
|
|
table.insert(keys, key)
|
|
for i=n,#ARGV,1 do
|
|
n = n+1
|
|
if ARGV[i] == "#end" then
|
|
redis.call("HMSet", key,unpack(argv))
|
|
break
|
|
else
|
|
table.insert(argv, ARGV[i])
|
|
end
|
|
end
|
|
end
|
|
end
|
|
redis.call("RPush", k,unpack(keys))
|
|
local c = tonumber(redis.call("LLEN", k))
|
|
count = count * 3
|
|
if (c > count) then
|
|
local off = c-count
|
|
out = redis.call("LRANGE", k,0,off-1)
|
|
redis.call("LTRIM", k,off,-1)
|
|
for i, v in ipairs(out) do
|
|
redis.call("DEL", v)
|
|
end
|
|
end
|
|
return out
|
|
`
|
|
|
|
/*
|
|
|
|
|
|
|
|
//批量读取列表数据
|
|
func (this *MCompModel) Batchgetlists(key string) (result []map[string]string, err error) {
|
|
var data interface{}
|
|
ret := this.Redis.EvalSha(this.Redis.Context(), this.getListSha1, []string{key})
|
|
if data, err = ret.Result(); err != nil {
|
|
fmt.Printf("Execute batchgetlists err: %v", err.Error())
|
|
} else {
|
|
temp1 := data.([]interface{})
|
|
result = make([]map[string]string, len(temp1))
|
|
for i, v := range temp1 {
|
|
temp2 := v.([]interface{})
|
|
result[i] = make(map[string]string)
|
|
for n := 0; n < len(temp2); n += 2 {
|
|
result[i][temp2[n].(string)] = temp2[n+1].(string)
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
err = redis.RedisNil
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//批量写入数据
|
|
func (this *MCompModel) Batchsetlists(data map[string]map[string]string) (err error) {
|
|
var (
|
|
n int
|
|
keys []string
|
|
values []interface{}
|
|
)
|
|
keys = make([]string, len(data))
|
|
values = make([]interface{}, 0)
|
|
|
|
for k, v := range data {
|
|
keys[n] = k
|
|
for k1, v1 := range v {
|
|
values = append(values, k1, v1)
|
|
}
|
|
values = append(values, "#end")
|
|
n++
|
|
}
|
|
ret := this.Redis.EvalSha(this.Redis.Context(), this.setListSha1, keys, values...)
|
|
if _, err := ret.Result(); err != nil {
|
|
fmt.Printf("Execute batchsetlists err: %v", err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
//批量读取队列数据
|
|
func (this *MCompModel) Batchgetqueues(key string, count int32) (result []map[string]string, err error) {
|
|
var data interface{}
|
|
ret := this.Redis.EvalSha(this.Redis.Context(), this.getQueueSha1, []string{key}, count)
|
|
if data, err = ret.Result(); err != nil {
|
|
fmt.Printf("Execute batchgetqueues err: %v", err.Error())
|
|
} else {
|
|
temp1 := data.([]interface{})
|
|
result = make([]map[string]string, len(temp1))
|
|
for i, v := range temp1 {
|
|
temp2 := v.([]interface{})
|
|
result[i] = make(map[string]string)
|
|
for n := 0; n < len(temp2); n += 2 {
|
|
result[i][temp2[n].(string)] = temp2[n+1].(string)
|
|
}
|
|
}
|
|
if len(result) == 0 {
|
|
err = redis.RedisNil
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//批量写入队列 并返回移除队列
|
|
func (this *MCompModel) Batchsetqueues(key string, count int32, ks []string, vs []map[string]string) (outkey []string, err error) {
|
|
var (
|
|
n int
|
|
keys []string
|
|
values []interface{}
|
|
result interface{}
|
|
)
|
|
keys = make([]string, len(ks)+2)
|
|
values = make([]interface{}, 0)
|
|
keys[0] = "count"
|
|
values = append(values, count)
|
|
values = append(values, "#end")
|
|
keys[1] = "key"
|
|
values = append(values, key)
|
|
values = append(values, "#end")
|
|
n = 2
|
|
for i, v := range ks {
|
|
keys[n] = v
|
|
for k1, v1 := range vs[i] {
|
|
values = append(values, k1, v1)
|
|
}
|
|
values = append(values, "#end")
|
|
n++
|
|
}
|
|
ret := this.Redis.EvalSha(this.Redis.Context(), this.setQueueSha1, keys, values...)
|
|
if result, err = ret.Result(); err != nil {
|
|
fmt.Printf("Execute batchsetqueues err: %v", err.Error())
|
|
} else {
|
|
outkey = make([]string, len(result.([]interface{})))
|
|
for i, v := range result.([]interface{}) {
|
|
outkey[i] = v.(string)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
//批量删除数据
|
|
func (this *MCompModel) BatchDelLists(uid string) (err error) {
|
|
ret := this.Redis.EvalSha(this.Redis.Context(), this.dellListSha1, []string{this.ukey(uid)})
|
|
if _, err := ret.Result(); err != nil {
|
|
fmt.Printf("Execute batchsetlists err: %v", err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
|
|
*/
|