Skip to content

Commit

Permalink
Merge pull request idevz#81 from idevz/vanilla-0.1.0.rc6
Browse files Browse the repository at this point in the history
Vanilla 0.1.0.rc6
  • Loading branch information
idevz authored Jul 6, 2016
2 parents b149d9a + a9aecf2 commit 071c9ff
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 28 deletions.
47 changes: 28 additions & 19 deletions vanilla/base/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ init_vanilla = function ()
Registry['REQ_ARGS'] = ngx_var.args
Registry['REQ_ARGS_ARR'] = ngx_req.get_uri_args()
Registry['REQ_HEADERS'] = ngx_req.get_headers()
Registry['APP_CACHE_PURGE'] = ngx_var.arg_vapurge
Registry['APP_CACHE_PURGE'] = Registry['REQ_ARGS_ARR']['vapurge']


if Registry['VANILLA_INIT'] then return end
Expand All @@ -125,28 +125,31 @@ init_vanilla = function ()
Registry['VANILLA_INIT'] = true
end

-- local helpers = require '/Users/zhoujing/data/vanilla/framework/0_1_0_rc6/vanilla.v.libs.utils'
-- function sprint_r( ... )
-- return helpers.sprint_r(...)
-- end

--+--------------------------------------------------------------------------------+--
-- local helpers = require '/Users/zhoujing/data/vanilla/framework/0_1_0_rc6/vanilla.v.libs.utils'
-- function sprint_r( ... )
-- return helpers.sprint_r(...)
-- end

-- function lprint_r( ... )
-- local rs = sprint_r(...)
-- print(rs)
-- end
-- function lprint_r( ... )
-- local rs = sprint_r(...)
-- print(rs)
-- end

-- function print_r( ... )
-- local rs = sprint_r(...)
-- ngx.say(rs)
-- end
-- function print_r( ... )
-- local rs = sprint_r(...)
-- ngx.say(rs)
-- end


--+--------------------------------------------------------------------------------+--
local ngx_re_find = ngx.re.find
use_page_cache = function ()
local cookie_lib = Registry['VANILLA_COOKIE_LIB']
local cookie = cookie_lib()
local no_cache_uris = Registry['APP_PAGE_CACHE_CONF']['no_cache_uris']
for _, uri in ipairs(no_cache_uris) do
if ngx_re_find(Registry['REQ_URI'], uri) ~= nil then return false end
end
Registry['COOKIES'] = cookie:getAll()
if Registry['APP_PAGE_CACHE_CONF']['cache_on'] then
if Registry['COOKIES'] and Registry['COOKIES'][Registry['APP_PAGE_CACHE_CONF']['no_cache_cookie']] then return false else return true end
Expand All @@ -163,6 +166,7 @@ local function clean_args(args)
for _,v in pairs(del_keys) do
args[v] = nil
end
if args['vapurge'] ~= nil then args['vapurge'] = nil end
return args
end

Expand All @@ -176,15 +180,20 @@ local function build_url_key(args)
return tab_concat( rs, "_")
end

local ngx_re_find = ngx.re.find
page_cache = function ()
Registry['USE_PAGE_CACHE'] = use_page_cache()
if not Registry['USE_PAGE_CACHE'] then ngx.header['X-Cache'] = 'PASSBY' return end
local cache_lib = Registry['VANILLA_CACHE_LIB']
Registry['cache_handle'] = Registry['APP_PAGE_CACHE_CONF']['cache_handle'] or 'shared_dict'
local cache = cache_lib(Registry['cache_handle'])
Registry['page_cache_handle'] = Registry['APP_PAGE_CACHE_CONF']['cache_handle'] or 'shared_dict'
local cache = cache_lib(Registry['page_cache_handle'])
Registry['APP_PAGE_CACHE_KEY'] = ngx.encode_args(clean_args(Registry['REQ_ARGS_ARR']))


if Registry['APP_CACHE_PURGE'] then
cache:del(Registry['APP_PAGE_CACHE_KEY'])
ngx.header['X-Cache'] = 'XX'
return
end

local rs = cache:get(Registry['APP_PAGE_CACHE_KEY'])
if rs then
ngx.header['X-Cache'] = 'HIT'
Expand Down
2 changes: 1 addition & 1 deletion vanilla/base/registry.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ end
function Registry:__newindex(index, value)
-- ngx.say('----__newindex----')
if VANILLA_REGISTRY[self.namespace] == nil then VANILLA_REGISTRY[self.namespace] = {} end
if index ~=nil and value ~= nil then
if index ~= nil then
VANILLA_REGISTRY[self.namespace][index]=value
end
end
Expand Down
6 changes: 6 additions & 0 deletions vanilla/v/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,10 @@ local function _get(self, ...)
end
Cache.get = _get

local function _del(self, ... )
local cache_instance = self.cache_instance
return cache_instance:del(...)
end
Cache.del = _del

return Cache
7 changes: 7 additions & 0 deletions vanilla/v/cache/lru.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ local function _get(self, key)
end
Lru.get = _get

local function _del(self, key)
local key = self.parent:cacheKey(key)
local cache_instance = Registry['lrucache_instance']
return cache_instance:delete(key)
end
Lru.del = _del

return Lru
14 changes: 14 additions & 0 deletions vanilla/v/cache/mc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ local function _get(self, key)
end
Mc.get = _get

local function _del(self, key)
local cache_conf = self.cache_conf
local cache_instance = self.cache_instance
local key = self.parent:cacheKey(key)
self.parent:connect(self, key, node)
local rs, err = cache_instance:delete(key)
if err then
-- error(err)
end
self.parent:set_keepalive(self)
return rs
end
Mc.del = _del

return Mc
22 changes: 15 additions & 7 deletions vanilla/v/cache/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ local function _get(self, key)
local cache_instance = self.cache_instance
local key = self.parent:cacheKey(key)
self.parent:connect(self, key, node)
local rs, err
if type(key) == 'string' then
local flags
rs, flags, err = cache_instance:get(key)
else
rs, err = cache_instance(key)
end
local rs, err = cache_instance:get(key)
if err then
error(err)
end
Expand All @@ -54,4 +48,18 @@ local function _get(self, key)
end
Redis.get = _get

local function _del(self, key)
local cache_conf = self.cache_conf
local cache_instance = self.cache_instance
local key = self.parent:cacheKey(key)
self.parent:connect(self, key, node)
local rs, err = cache_instance:del(key)
if err then
error(err)
end
self.parent:set_keepalive(self)
return rs
end
Redis.del = _del

return Redis
7 changes: 7 additions & 0 deletions vanilla/v/cache/shdict.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ local function _get(self, key)
end
Shdict.get = _get

local function _del(self, key)
local key = self.parent:cacheKey(key)
local cache_instance = self.cache_instance
return cache_instance:delete(key)
end
Shdict.del = _del

return Shdict
2 changes: 1 addition & 1 deletion vanilla/v/response.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function Response:response()
ngx.print(rs)
if Registry['USE_PAGE_CACHE'] then
local cache_lib = Registry['VANILLA_CACHE_LIB']
local page_cache = cache_lib(Registry['cache_handle'])
local page_cache = cache_lib(Registry['page_cache_handle'])
page_cache:set(Registry['APP_PAGE_CACHE_KEY'], rs, self.page_cache_timeout)
end
return true
Expand Down

0 comments on commit 071c9ff

Please sign in to comment.