-
Notifications
You must be signed in to change notification settings - Fork 1
/
updateListTemplet.go
66 lines (59 loc) · 1.93 KB
/
updateListTemplet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package src
import (
goredis "github.com/adimax2953/go-redis"
logtool "github.com/adimax2953/log-tool"
)
// UpdateList function - keys, args[] string - return string , error
func (s *MyScriptor) UpdateList(keys, args []string) (string, error) {
res, err := s.Scriptor.ExecSha(UpdateListID, keys, args)
if err != nil {
logtool.LogError("UpdateList ExecSha Error", err)
return "", err
}
result := &RedisResult{}
reader := goredis.NewRedisArrayReplyReader(res.([]interface{}))
result.Value = reader.ReadString()
if err != nil {
logtool.LogError("UpdateList Value Error", err)
return "", err
}
return result.Value, nil
}
// UpdateList - 減少數值
const (
UpdateListID = "UpdateList"
UpdateListTemplate = `
--[[
Author : Adimax.Tsai
Description : UpdateList
EVALSHA <script_sha1> 0 {DBKey} {ProjectKey} {TagKey} {k1} {v1} {v2}
--]]
local DBKey = tonumber(KEYS[1])
local ProjectKey = KEYS[2]
local TagKey = KEYS[3]
local k1 = ARGV[1]
local v1 = tonumber(ARGV[2])
local v2 = ARGV[3]
local sender = "UpdateList.lua"
if DBKey and ProjectKey and TagKey and k1 and v1 and v2 then
local MAIN_KEY = ProjectKey..":"..TagKey..":"..k1
redis.call("select",DBKey)
local chkexists = 0
local chkllen = 0
local r1 = ""
chkexists = redis.call('exists',MAIN_KEY)
chkllen = redis.call('LLEN',MAIN_KEY)
if chkexists == 1 then
if tonumber(v1) < chkllen then
local tmp = redis.call('lset',MAIN_KEY,v1,v2)
if tmp~=nil and tmp~=false and tmp~="" then
r1 = v2
return { r1 }
end
elseif tonumber(v1) >= chkllen then
return { "err , index out of range" }
end
end
end
`
)