-
Notifications
You must be signed in to change notification settings - Fork 1
/
incBase62Templet.go
116 lines (99 loc) · 3.19 KB
/
incBase62Templet.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package src
import (
goredis "github.com/adimax2953/go-redis"
logtool "github.com/adimax2953/log-tool"
)
// IncBase62 function - keys, args[] string - return string , error
func (s *MyScriptor) IncBase62(keys, args []string) (string, error) {
res, err := s.Scriptor.ExecSha(IncBase62ID, keys, args)
if err != nil {
logtool.LogError("IncBase62 ExecSha Error", err)
return "", err
}
result := &RedisResult{}
reader := goredis.NewRedisArrayReplyReader(res.([]interface{}))
result.Value = reader.ReadString()
if err != nil {
logtool.LogError("IncBase62 Value Error", err)
return "", err
}
return result.Value, nil
}
// IncValue - 增加數字
const (
IncBase62ID = "IncBase62"
IncBase62Template = `
--[[
Author : Adimax.Tsai
Description : IncBase62
EVALSHA <script_sha1> 0 {DBKey} {ProjectKey} {TagKey} {k1} {k2} {v1}
--]]
local DBKey = tonumber(KEYS[1])
local ProjectKey = KEYS[2]
local TagKey = KEYS[3]
local k1 = ARGV[1]
local k2 = ARGV[2]
local v1 = ARGV[3]
local sender = "IncBase62.lua"
if not DBKey or DBKey=="" then
return {err="invalid argument 'DBKey'", sender=sender}
end
if not ProjectKey or ProjectKey=="" then
return {err="invalid argument 'ProjectKey'", sender=sender}
end
if not TagKey or TagKey=="" then
return {err="invalid argument 'TagKey'", sender=sender}
end
if not k1 or k1=="" then
return {err="invalid argument 'k1'", sender=sender}
end
if not k2 or k2=="" then
return {err="invalid argument 'k2'", sender=sender}
end
if not v1 or v1=="" then
return {err="invalid argument 'v1'", sender=sender}
end
---@return number
local function string_indexOf(s,pattern,init)
init = init or 0
local index = string.find(s,pattern,init,true)
return index or -1
end
---@return string
local function Base62Inc(s)
local chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
local firstChar = string.sub(s,0,1)
if string_indexOf(chars , firstChar) == 62 then
return Base62Inc("0"..s)
end
local lastChar = string.sub(s, #s, #s)
local fragment = string.sub(s, 1, #s-1)
if string_indexOf(chars,lastChar) < 62 then
lastChar = string.sub(chars , string_indexOf(chars,lastChar) +1 , string_indexOf(chars,lastChar) +1)
return fragment..lastChar
end
return Base62Inc(fragment).."0"
end
if DBKey and ProjectKey and TagKey and k1 and k2 and v1 then
local MAIN_KEY = ProjectKey..":"..TagKey..":"..k1
redis.call("select",DBKey)
local result = ""
local s = ""
local Tmp = redis.call('hget',MAIN_KEY , k2)
if Tmp~=nil and Tmp~="" and Tmp~=false then
local slicedTmp = string.sub(Tmp, 1, 6)
local slicedv1 = string.sub(v1, 1, 6)
if slicedv1==slicedTmp then
s = Tmp
else
s = v1
end
else
s = v1
end
result = Base62Inc(s)
redis.call('hset',MAIN_KEY, k2, result)
return {result}
end
`
)