Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 453c69d

Browse files
committedJul 13, 2017
bug fixed: page_cache bug about the gloable Registry
1 parent 94e9edd commit 453c69d

File tree

3 files changed

+68
-66
lines changed

3 files changed

+68
-66
lines changed
 

‎vanilla/base/init.lua

+40-35
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,22 @@
33
-- @author idevz <zhoujing00k@gmail.com>
44
-- version $Id$
55

6-
VANILLA_G = _G
7-
VANILLA_REGISTRY = {}
6+
7+
-- local helpers = require '/media/psf/g/idevz/code/www/vanilla/framework/0_1_0_rc7/vanilla.v.libs.utils'
8+
-- function sprint_r( ... )
9+
-- return helpers.sprint_r(...)
10+
-- end
11+
12+
-- function lprint_r( ... )
13+
-- local rs = sprint_r(...)
14+
-- print(rs)
15+
-- end
16+
17+
-- function print_r( ... )
18+
-- local rs = sprint_r(...)
19+
-- ngx.say(rs)
20+
-- end
21+
822
Registry = require('registry'):new()
923

1024

@@ -92,16 +106,20 @@ end
92106

93107

94108
--+--------------------------------------------------------------------------------+--
95-
local ngx_var = ngx.var
96-
local ngx_req = ngx.req
97109
init_vanilla = function ()
110+
local ngx_var = ngx.var
111+
local ngx_req = ngx.req
98112
Registry.namespace = ngx_var.APP_NAME
99113

100-
Registry['REQ_URI'] = ngx_var.uri
101-
Registry['REQ_ARGS'] = ngx_var.args
102-
Registry['REQ_ARGS_ARR'] = ngx_req.get_uri_args()
103-
Registry['REQ_HEADERS'] = ngx_req.get_headers()
104-
Registry['APP_CACHE_PURGE'] = Registry['REQ_ARGS_ARR']['vapurge']
114+
local REQ_Registry = require('registry'):new()
115+
REQ_Registry.namespace = ngx_var.APP_NAME
116+
117+
REQ_Registry['REQ_URI'] = ngx_var.uri
118+
REQ_Registry['REQ_ARGS'] = ngx_var.args
119+
REQ_Registry['REQ_ARGS_ARR'] = ngx_req.get_uri_args()
120+
REQ_Registry['REQ_HEADERS'] = ngx_req.get_headers()
121+
REQ_Registry['APP_CACHE_PURGE'] = REQ_Registry['REQ_ARGS_ARR']['vapurge']
122+
ngx.ctx.REQ_Registry = REQ_Registry
105123

106124

107125
if Registry['VANILLA_INIT'] then return end
@@ -125,34 +143,19 @@ init_vanilla = function ()
125143
Registry['VANILLA_INIT'] = true
126144
end
127145

128-
-- local helpers = require '/media/psf/g/idevz/code/www/vanilla/framework/0_1_0_rc7/vanilla.v.libs.utils'
129-
-- function sprint_r( ... )
130-
-- return helpers.sprint_r(...)
131-
-- end
132-
133-
-- function lprint_r( ... )
134-
-- local rs = sprint_r(...)
135-
-- print(rs)
136-
-- end
137-
138-
-- function print_r( ... )
139-
-- local rs = sprint_r(...)
140-
-- ngx.say(rs)
141-
-- end
142-
143-
144146
--+--------------------------------------------------------------------------------+--
145147
local ngx_re_find = ngx.re.find
146148
use_page_cache = function ()
149+
local REQ_Registry = ngx.ctx.REQ_Registry
147150
local cookie_lib = Registry['VANILLA_COOKIE_LIB']
148151
local cookie = cookie_lib()
149152
local no_cache_uris = Registry['APP_PAGE_CACHE_CONF']['no_cache_uris']
150153
for _, uri in ipairs(no_cache_uris) do
151-
if ngx_re_find(Registry['REQ_URI'], uri) ~= nil then return false end
154+
if ngx_re_find(REQ_Registry['REQ_URI'], uri) ~= nil then return false end
152155
end
153-
Registry['COOKIES'] = cookie:getAll()
156+
REQ_Registry['COOKIES'] = cookie:getAll()
154157
if Registry['APP_PAGE_CACHE_CONF']['cache_on'] then
155-
if Registry['COOKIES'] and Registry['COOKIES'][Registry['APP_PAGE_CACHE_CONF']['no_cache_cookie']] then return false else return true end
158+
if REQ_Registry['COOKIES'] and REQ_Registry['COOKIES'][Registry['APP_PAGE_CACHE_CONF']['no_cache_cookie']] then return false else return true end
156159
else
157160
return false
158161
end
@@ -181,25 +184,27 @@ local function build_url_key(args)
181184
end
182185

183186
page_cache = function ()
184-
Registry['USE_PAGE_CACHE'] = use_page_cache()
185-
if not Registry['USE_PAGE_CACHE'] then ngx.header['X-Cache'] = 'PASSBY' return end
187+
local ngx_var = ngx.var
188+
local REQ_Registry = ngx.ctx.REQ_Registry
189+
REQ_Registry['USE_PAGE_CACHE'] = use_page_cache()
190+
if not REQ_Registry['USE_PAGE_CACHE'] then ngx.header['X-Cache'] = 'PASSBY' return end
186191
local cache_lib = Registry['VANILLA_CACHE_LIB']
187192
Registry['page_cache_handle'] = Registry['APP_PAGE_CACHE_CONF']['cache_handle'] or 'shared_dict'
188193
local cache = cache_lib(Registry['page_cache_handle'])
189-
Registry['APP_PAGE_CACHE_KEY'] = Registry['REQ_URI'] .. ngx.encode_args(clean_args(Registry['REQ_ARGS_ARR']))
194+
REQ_Registry['APP_PAGE_CACHE_KEY'] = REQ_Registry['REQ_URI'] .. ngx.encode_args(clean_args(REQ_Registry['REQ_ARGS_ARR']))
190195

191-
if Registry['APP_CACHE_PURGE'] then
192-
cache:del(Registry['APP_PAGE_CACHE_KEY'])
196+
if REQ_Registry['APP_CACHE_PURGE'] then
197+
cache:del(REQ_Registry['APP_PAGE_CACHE_KEY'])
193198
ngx.header['X-Cache'] = 'XX'
194199
return
195200
end
196201

197-
local rs = cache:get(Registry['APP_PAGE_CACHE_KEY'])
202+
local rs = cache:get(REQ_Registry['APP_PAGE_CACHE_KEY'])
198203
if rs then
199204
ngx.header['X-Cache'] = 'HIT'
200205
ngx_var.va_cache_status = 'HIT'
201206
ngx.header['Power_By'] = 'Vanilla-Page-Cache'
202-
if ngx_re_find(Registry['REQ_HEADERS']['accept'], 'json') then
207+
if ngx_re_find(REQ_Registry['REQ_HEADERS']['accept'], 'json') then
203208
ngx.header['Content_type'] = 'application/json'
204209
else
205210
ngx.header['Content_type'] = 'text/html'

‎vanilla/base/registry.lua

+25-29
Original file line numberDiff line numberDiff line change
@@ -3,66 +3,62 @@
33
-- @author idevz <zhoujing00k@gmail.com>
44
-- version $Id$
55

6+
local ok, new_tab = pcall(require, "table.new")
7+
if not ok or type(new_tab) ~= "function" then
8+
new_tab = function (narr, nrec) return {} end
9+
end
10+
611
local setmetatable = setmetatable
712

8-
local Registry = {}
13+
local Registry = new_tab(0, 6)
914

1015
function Registry:del(key)
11-
VANILLA_REGISTRY[self.namespace][key] = nil
12-
return true
13-
end
14-
15-
function Registry:get(key)
16-
if VANILLA_REGISTRY[self.namespace][key] ~= nil then
17-
return VANILLA_REGISTRY[self.namespace][key]
18-
else
19-
return false
16+
local data = rawget(self, "_data")
17+
if data[self.namespace] ~= nil and data[self.namespace][key] ~= nil then
18+
data[self.namespace][key] = nil
2019
end
20+
return true
2121
end
2222

2323
function Registry:has(key)
24-
if VANILLA_REGISTRY[self.namespace][key] ~= nil then
25-
return true
24+
local data = rawget(self, "_data")
25+
if data[self.namespace][key] ~= nil then
26+
return true
2627
else
2728
return false
2829
end
2930
end
3031

31-
function Registry:set(key, value)
32-
VANILLA_REGISTRY[self.namespace][key] = value
33-
return true
34-
end
35-
3632
function Registry:dump(namespace)
37-
if namespace ~= nil then return VANILLA_REGISTRY[namespace] else return VANILLA_REGISTRY end
33+
local data = rawget(self, "_data")
34+
if namespace ~= nil then return data[namespace] else return data end
3835
end
3936

4037
function Registry:new()
38+
local data = new_tab(0, 36)
4139
local instance = {
4240
namespace = 'vanilla_app',
4341
del = self.del,
44-
get = self.get,
4542
has = self.has,
4643
dump = self.dump,
47-
set = self.set
44+
_data = data
4845
}
49-
setmetatable(instance, Registry)
50-
return instance
46+
return setmetatable(instance, {__index = self.__index, __newindex = self.__newindex})
5147
end
5248

5349
function Registry:__newindex(index, value)
54-
-- ngx.say('----__newindex----')
55-
if VANILLA_REGISTRY[self.namespace] == nil then VANILLA_REGISTRY[self.namespace] = {} end
50+
local data = rawget(self, "_data")
51+
if data[self.namespace] == nil then data[self.namespace] = {} end
5652
if index ~= nil then
57-
VANILLA_REGISTRY[self.namespace][index]=value
53+
data[self.namespace][index]=value
5854
end
5955
end
6056

6157
function Registry:__index(index)
62-
-- ngx.say('----__index----')
63-
if VANILLA_REGISTRY[self.namespace] == nil then VANILLA_REGISTRY[self.namespace] = {} end
64-
local out = rawget(VANILLA_REGISTRY[self.namespace], index)
58+
local data = rawget(self, "_data")
59+
if data[self.namespace] == nil then data[self.namespace] = {} end
60+
local out = data[self.namespace][index]
6561
if out then return out else return false end
6662
end
6763

68-
return Registry
64+
return Registry

‎vanilla/v/response.lua

+3-2
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,18 @@ function Response:prependBody(prepend_body)
5252
end
5353

5454
function Response:response()
55+
local REQ_Registry = ngx.ctx.REQ_Registry
5556
local vanilla_version = Registry['VANILLA_VERSION']
5657
ngx.header['Power_By'] = 'Vanilla-' .. vanilla_version
5758
ngx.header['Content_type'] = ngx.header['Content_type'] or 'text/html'
5859
local body = {[1]=self.append_body, [2]=self.body, [3]=self.prepend_body}
5960

6061
ngx.print(body)
61-
if Registry['USE_PAGE_CACHE'] then
62+
if REQ_Registry['USE_PAGE_CACHE'] then
6263
local cache_lib = Registry['VANILLA_CACHE_LIB']
6364
local page_cache = cache_lib(Registry['page_cache_handle'])
6465
local rs = table.concat( body, "")
65-
page_cache:set(Registry['APP_PAGE_CACHE_KEY'], rs, self.page_cache_timeout)
66+
page_cache:set(REQ_Registry['APP_PAGE_CACHE_KEY'], rs, self.page_cache_timeout)
6667
end
6768
return true
6869
end

0 commit comments

Comments
 (0)
Please sign in to comment.