forked from CNSRE/ABTestingGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache.lua
More file actions
126 lines (99 loc) · 3.77 KB
/
cache.lua
File metadata and controls
126 lines (99 loc) · 3.77 KB
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
117
118
119
120
121
122
123
124
125
126
local modulename = "abtestingCache"
local _M = {}
_M._VERSION = '0.0.1'
local ERRORINFO = require('abtesting.error.errcode').info
local systemConf = require('abtesting.utils.init')
local prefixConf = systemConf.prefixConf
local runtimeLib = prefixConf.runtimeInfoPrefix
local indices = systemConf.indices
local fields = systemConf.fields
local divConf = systemConf.divConf
local shdict_expire = divConf.shdict_expire or 60
_M.new = function(self, sharedDict)
if not sharedDict then
error{ERRORINFO.ARG_BLANK_ERROR, 'cache name valid from nginx.conf'}
end
self.cache = ngx.shared[sharedDict]
if not self.cache then
error{ERRORINFO.PARAMETER_ERROR, 'cache name [' .. sharedDict .. '] valid from nginx.conf'}
end
return setmetatable(self, { __index = _M } )
end
local isNULL = function(v)
return not v or v == ngx.null
end
local areNULL = function(v1, v2, v3)
if isNULL(v1) or isNULL(v2) or isNULL(v3) then
return true
end
return false
end
_M.getSteps = function(self, hostname)
local cache = self.cache
local k_divsteps = runtimeLib..':'..hostname..':'..fields.divsteps
local divsteps = cache:get(k_divsteps)
return tonumber(divsteps)
end
_M.getRuntime = function(self, hostname, divsteps)
local cache = self.cache
local runtimegroup = {}
local prefix = runtimeLib .. ':' .. hostname
for i = 1, divsteps do
local idx = indices[i]
local k_divModname = prefix .. ':'..idx..':'..fields.divModulename
local k_divDataKey = prefix .. ':'..idx..':'..fields.divDataKey
local k_userInfoModname = prefix .. ':'..idx..':'..fields.userInfoModulename
local divMod, err1 = cache:get(k_divModname)
local divPolicy, err2 = cache:get(k_divDataKey)
local userInfoMod, err3 = cache:get(k_userInfoModname)
if areNULL(divMod, divPolicy, userInfoMod) then
return false
end
local runtime = {}
runtime[fields.divModulename ] = divMod
runtime[fields.divDataKey ] = divPolicy
runtime[fields.userInfoModulename] = userInfoMod
runtimegroup[idx] = runtime
end
return true, runtimegroup
end
_M.setRuntime = function(self, hostname, divsteps, runtimegroup)
local cache = self.cache
local prefix = runtimeLib .. ':' .. hostname
local expire = shdict_expire
for i = 1, divsteps do
local idx = indices[i]
local k_divModname = prefix .. ':'..idx..':'..fields.divModulename
local k_divDataKey = prefix .. ':'..idx..':'..fields.divDataKey
local k_userInfoModname = prefix .. ':'..idx..':'..fields.userInfoModulename
local runtime = runtimegroup[idx]
local ok1, err = cache:set(k_divModname, runtime[fields.divModulename], expire)
local ok2, err = cache:set(k_divDataKey, runtime[fields.divDataKey], expire)
local ok3, err = cache:set(k_userInfoModname, runtime[fields.userInfoModulename], expire)
if areNULL(ok1, ok2, ok3) then return false end
end
local k_divsteps = prefix ..':'..fields.divsteps
local ok, err = cache:set(k_divsteps, divsteps, shdict_expire)
if not ok then return false end
return true
end
_M.getUpstream = function(self, divsteps, usertable)
local upstable = {}
local cache = self.cache
for i = 1, divsteps do
local idx = indices[i]
local info = usertable[idx]
-- ups will be an actually value or nil
if info then
local ups = cache:get(info)
upstable[idx] = ups
end
end
return upstable
end
_M.setUpstream = function(self, info, upstream)
local cache = self.cache
local expire = shdict_expire
cache:set(info, upstream, expire)
end
return _M