杰克工作室 发表于 2024-2-10 16:03

Nginx缓存解决方案:SRCache

<p>前些天帮别人优化PHP程序,搞得灰头土脸,最后黔驴技穷开启了FastCGI Cache,算是勉强应付过去了吧。不过FastCGI Cache不支持分布式缓存,当服务器很多的时候,冗余的浪费将非常严重,此外还有数据一致性问题,所以它只是一个粗线条的解决方案。</p>

<p>对此类问题而言,SRCache是一个细粒度的解决方案。其工作原理大致如下:</p>

<p>&nbsp;</p>

<p>当问题比较简单的时候,通常SRCache和Memc模块一起搭配使用。网上能搜索到一些相关的例子,大家可以参考,这里就不赘述了。当问题比较复杂的时候,比如说缓存键的动态计算等,就不得不写一点代码了,此时Lua模块是最佳选择。</p>

<p>闲言碎语不多讲,表一表Nginx配置文件长啥样:</p>

<p>lua_package_path &#39;/path/to/vendor/?.lua;;&#39;;</p>

<p>init_by_lua_file /path/to/config.lua;</p>

<p>server {<br />
listen 80;<br />
server_name foo.com;</p>

<p>root /path;<br />
index index.html index.htm index.php;</p>

<p>location / {<br />
try_files $uri $uri/ /index.php$is_args$args;<br />
}</p>

<p>location ~ .php$ {<br />
set $key &quot;&quot;;<br />
set $ttl 600;<br />
set $skip 1;</p>

<p>rewrite_by_lua_file /path/to/guard.lua;</p>

<p>srcache_fetch_skip $skip;<br />
srcache_store_skip $skip;<br />
srcache_fetch GET /memcached key=$key;<br />
srcache_store PUT /memcached key=$key&amp;ttl=$ttl;</p>

<p>add_header X-Srcache-Fetch-Status $srcache_fetch_status;<br />
add_header X-Srcache-Store-Status $srcache_store_status;</p>

<p>try_files $uri =404;</p>

<p>include fastcgi.conf;<br />
fastcgi_pass 127.0.0.1:9000;<br />
}</p>

<p>location /memcached {<br />
internal;<br />
content_by_lua_file /path/to/data/memcached.lua;<br />
}<br />
}<br />
Nginx启动后,会载入config.lua中的配置信息。请求到达后,缺省情况下,SRCache为关闭状态,在guard.lua中,会对当前请求进行正则匹配,一旦匹配成功,那么就会计算出缓存键,并且把SRCache设置为开启状态,最后由memcached.lua完成读写。</p>

<p>看看「config.lua」文件的内容,它主要用来记录一些全局的配置信息:</p>

<p>config = {}</p>

<p>config[&quot;memcached&quot;] = {<br />
{host = &quot;127.0.0.1&quot;, port = &quot;11211&quot;},<br />
{host = &quot;127.0.0.1&quot;, port = &quot;11212&quot;},<br />
{host = &quot;127.0.0.1&quot;, port = &quot;11213&quot;},<br />
}</p>

<p>config[&quot;ruleset&quot;] = {<br />
{pattern = &quot;/test&quot;, fields = {x = &quot;number&quot;, y = &quot;string&quot;}},<br />
}<br />
看看「guard.lua」文件的内容,它主要用来计算缓存键,并开启SRCache模块:</p>

<p>local uri = string.match(ngx.var.request_uri, &quot;[^?]+&quot;)</p>

<p>for _, rule in ipairs(config[&quot;ruleset&quot;]) do<br />
local pattern = rule[&quot;pattern&quot;]<br />
local option = rule[&quot;option&quot;]</p>

<p>if ngx.re.match(uri, pattern, option or &quot;&quot;) then<br />
local ttl = rule[&quot;ttl&quot;]</p>

<p>if ttl then<br />
ngx.var.ttl = ttl<br />
end</p>

<p>local args = ngx.req.get_uri_args()</p>

<p>local fields = rule[&quot;fields&quot;]</p>

<p>if fields then<br />
for name in pairs(args) do<br />
if fields then<br />
if fields == &quot;number&quot; then<br />
args = tonumber(args) or 0<br />
end<br />
else<br />
args = nil<br />
end<br />
end<br />
end</p>

<p>local key = {<br />
ngx.var.request_method, &quot; &quot;,<br />
ngx.var.scheme, &quot;://&quot;,<br />
ngx.var.host, uri,<br />
}</p>

<p>args = ngx.encode_args(args);</p>

<p>if args ~= &quot;&quot; then<br />
key[#key + 1] = &quot;?&quot;<br />
key[#key + 1] = args<br />
end</p>

<p>key = table.concat(key)<br />
key = ngx.md5(key)</p>

<p>ngx.var.key = key</p>

<p>ngx.var.skip = &quot;0&quot;</p>

<p>break<br />
end<br />
end<br />
看看「memcached.lua」文件的内容,它主要通过Resty库来读写Memcached:</p>

<p>local memcached = require &quot;resty.memcached&quot;</p>

<p>local key = ngx.var.arg_key</p>

<p>local index = ngx.crc32_long(key) % #config[&quot;memcached&quot;] + 1</p>

<p>local host = config[&quot;memcached&quot;][&quot;host&quot;]<br />
local port = config[&quot;memcached&quot;][&quot;port&quot;]</p>

<p>local memc, err = memcached:new()</p>

<p>if not memc then<br />
ngx.log(ngx.ERR, err)<br />
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)<br />
end</p>

<p>memc:set_timeout(100)</p>

<p>local ok, err = memc:connect(host, port)</p>

<p>if not ok then<br />
ngx.log(ngx.ERR, err)<br />
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)<br />
end</p>

<p>local method = ngx.req.get_method()</p>

<p>if method == &quot;GET&quot; then<br />
local res, flags, err = memc:get(key)</p>

<p>if err then<br />
ngx.log(ngx.ERR, err)<br />
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)<br />
end</p>

<p>ngx.print(res)<br />
elseif method == &quot;PUT&quot; then<br />
local value = ngx.req.get_body_data()<br />
local ttl = ngx.var.arg_ttl</p>

<p>local ok, err = memc:set(key, value, ttl)</p>

<p>if not ok then<br />
ngx.log(ngx.ERR, err)<br />
ngx.exit(ngx.HTTP_SERVICE_UNAVAILABLE)<br />
end<br />
else<br />
ngx.exit(ngx.HTTP_NOT_ALLOWED)<br />
end</p>

<p>memc:set_keepalive(1000, 10)<br />
最后一个问题:如何判断缓存是否生效了?试试下面的命令:</p>

<p>shell&gt; curl -v &quot;http://foo.com/test?x=123&amp;y=abc&quot;<br />
&lt; X-Srcache-Fetch: HIT<br />
&lt; X-Srcache-Store: BYPASS<br />
关于激活SRCache前后的性能对比,视环境的不同会有所差异,不过绝对是数量级的提升,更重要的是这一切对业务层完全透明,别愣着了,快试试吧!&nbsp;</p>

<p>&nbsp;</p>

<p>原文地址:<a href="http://www.aikaiyuan.com/7212.html">http://www.aikaiyuan.com/7212.html</a></p>
页: [1]
查看完整版本: Nginx缓存解决方案:SRCache