Skip to content

Commit d3b81d9

Browse files
author
rain
committed
通过火焰图优化: shared dict 改成 lrucache 性能提升30%
1 parent ccfdfac commit d3b81d9

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

lua_ad/libs/cache.lua

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,16 @@
33
local mysql = require("resty.mysql")
44
local cjson = require("cjson")
55
local comm = require("libs.common")
6+
local lrucache = require("resty.lrucache")
67

78
local _M = {}
89

10+
-- we need to initialize the cache on the lua module level so that
11+
-- it can be shared by all the requests served by each nginx worker process
12+
local lru_cache = lrucache.new(10)
913

10-
local function cache_mysql(cache_ad)
14+
15+
local function cache_mysql()
1116
ngx.log(ngx.INFO, "缓存mysql数据到共享内存")
1217

1318
--当前时间
@@ -77,7 +82,7 @@ local function cache_mysql(cache_ad)
7782
local orig_ad_stage = {}
7883

7984
for _, v in ipairs(res_stage) do
80-
ngx.say(v.originalityId)
85+
ngx.log(ngx.ERR, '缓存广告创意: ' .. v.originalityId)
8186
local res_orig, err, errno, sqlstate =
8287
db:query("select * from orig where originalityId = '" .. v.originalityId .. "'")
8388

@@ -110,7 +115,7 @@ local function cache_mysql(cache_ad)
110115
end
111116

112117
if not comm.table_is_empty(orig_ad_stage) then
113-
cache_ad:set("ad", cjson.encode(orig_ad_stage), 1800)
118+
lru_cache:set("ad", orig_ad_stage, 1800)
114119
end
115120

116121
local ok, err = db:set_keepalive(60000, 100)
@@ -123,8 +128,7 @@ end
123128

124129

125130
function _M.cache()
126-
local cache_ad = ngx.shared.cache_ad
127-
local ad = cache_ad:get("ad")
131+
local ad = lru_cache:get("ad")
128132

129133
--未过期
130134
if ad then
@@ -135,9 +139,21 @@ function _M.cache()
135139
local lock = require "resty.lock"
136140
local lock = lock:new("locks_ad")
137141
lock:lock("lock_ad")
138-
cache_mysql(cache_ad)
142+
ad = lru_cache:get("ad")
143+
if ad then
144+
lock:unlock()
145+
return
146+
end
147+
cache_mysql()
139148
lock:unlock()
140149
end
141150

142151

152+
function _M.get_ad()
153+
local ad = lru_cache:get("ad")
154+
155+
return ad
156+
end
157+
158+
143159
return _M

lua_ad/libs/match.lua

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
local cjson = require("cjson")
44
local comm = require("libs.common")
55
local redis = require("libs.redis")
6+
local lru = require("libs.cache")
67

78
local _M = {}
89

@@ -185,17 +186,14 @@ function _M.match(data)
185186
end
186187

187188
--获取所有候选的广告 创意
188-
local cache_ad = ngx.shared.cache_ad
189-
local orig_ad_stage = cache_ad:get("ad")
189+
local orig_ad_stage_json = lru.get_ad()
190190
--ngx.say(orig_ad_stage)
191191

192-
if not orig_ad_stage then
192+
if not orig_ad_stage_json then
193193
ngx.log(ngx.INFO, '没有候选广告可选择')
194194
return nil
195195
end
196196

197-
local orig_ad_stage_json = cjson.decode(orig_ad_stage)
198-
199197
if comm.table_is_empty(orig_ad_stage_json) then
200198
ngx.log(ngx.INFO, '没有候选广告可选择')
201199
return nil

0 commit comments

Comments
 (0)