-
Notifications
You must be signed in to change notification settings - Fork 1
/
takeValueTemplet.go
65 lines (55 loc) · 1.84 KB
/
takeValueTemplet.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
package src
import (
goredis "github.com/adimax2953/go-redis"
logtool "github.com/adimax2953/log-tool"
)
// TakeValue function - keys, args[] string - return int64 , error
func (s *MyScriptor) TakeValue(keys, args []string) (int64, error) {
res, err := s.Scriptor.ExecSha(TakeValueID, keys, args)
if err != nil {
logtool.LogError("TakeValue ExecSha Error", err)
return 0, err
}
result := &RedisResult{}
reader := goredis.NewRedisArrayReplyReader(res.([]interface{}))
result.ValueInt64, err = reader.ReadInt64(0)
if err != nil {
logtool.LogError("TakeValue Value Error", err)
return 0, err
}
return result.ValueInt64, nil
}
// TakeValue - 取得數字
const (
TakeValueID = "TakeValue"
TakeValueTemplate = `
--[[
Author : Adimax.Tsai
Description : TakeValue
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 = tonumber(ARGV[3])
local sender = "TakeValue.lua"
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 POOL = redis.call('hget',MAIN_KEY,k2)
local tmp_money = 0
if POOL~=nil and POOL~="" and POOL~=false then
tmp_money = POOL * v1 /100
else
POOL = 0
redis.call('hset',MAIN_KEY, k2, 0 )
end
redis.call('hset',MAIN_KEY, k2, POOL - tmp_money )
result = tostring(tmp_money)
return {result}
end
`
)