openResty+lua实现缓存操作
openResty介绍:一个强大的web应用服务器,简单的说就是封装好了nginx,为nginx提供了高性能的拓展程序。并且集成了LUA脚本,主要能够提供10k-1000k的并发连接量。
安装openResty:
yum install yum-utils
yum-config-manager --add-repo https://opernresty.org/package/centos/openresty.repo
yum install openresty
安装成功就会在默认目录下
usr/local/openresty
安装好了openresty之后,nginx也是被默认安装好的,关于nginx的配置这里就不详细说明。
场景重现以及测试:
1.当首页需要进行广告图的轮播时,会先在nginx缓存里面查找
2.当nginx缓存里没有数据时,会去redis里查找,并写入nginx缓存中
3.当redis中没有时,会去mysql中查找并写入redis。
关于nginx的conf:
user root root;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#定义Nginx缓存模块,模块名字叫dis_cache,容量大小128M
lua_shared_dict dis_cache 128m;
#限流设置
limit_req_zone $binary_remote_addr zone=contentRateLimit:10m rate=2r/s;
#根据IP地址来限制,存储内存大小10M
limit_conn_zone $binary_remote_addr zone=addr:10m;
#个人IP显示
limit_conn_zone $binary_remote_addr zone=perip:10m;
#针对整个服务所有的并发量控制
limit_conn_zone $server_name zone=perserver:10m;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
#监听的域名
server_name localhost;
#192.168.211.1
location /brand {
limit_conn perip 3; #单个客户端ip与服务器的连接数.
limit_conn perserver 5; #限制与服务器的总连接数
#同一个IP只允许有2个并发连接
#limit_conn addr 2;
#所有以/brand的请求,都将交给 192.168.211.1服务器的18081程序处理.
proxy_pass http://192.168.211.1:18081;
}
#表示所有以 localhost/read_content的请求都由该配置处理
location /read_content {
#使用指定限流配置,burst=4表示允许同时有4个并发连接,如果不能同时处理,则会放入队列,等请求处理完成后,再从队列中拿请求
#nodelay 并行处理所有请求
limit_req zone=contentRateLimit burst=4 nodelay;
#content_by_lua_file:所有请求都交给指定的lua脚本处理(/root/lua/read_content.lua)
content_by_lua_file /usr/local/server/lua/read_content.lua;
}
#表示所有以 localhost/update_content的请求都由该配置处理
location /update_content {
#content_by_lua_file:所有请求都交给指定的lua脚本处理(/root/lua/update_content.lua)
content_by_lua_file /usr/local/server/lua/update_content.lua;
}
}
}
读取数据的lua脚本read_content:
ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
--获取本地缓存
local cache_ngx = ngx.shared.dis_cache;
--根据ID 获取本地缓存数据
local contentCache = cache_ngx:get('content_cache_'..id);
if contentCache == "" or contentCache == nil then
--连接redis数据库
local redis = require("resty.redis");
local red = redis:new()
red:set_timeout(2000)
red:connect("ip地址", 6379)
local rescontent=red:get("content_"..id);
if ngx.null == rescontent then
--连接mysql数据库
local cjson = require("cjson");
local mysql = require("resty.mysql");
local db = mysql:new();
db:set_timeout(2000)
local props = {
host = "ip地址",
port = 3306,
database = "content",
user = "root",
password = "123456"
}
local res = db:connect(props);
local select_sql = "select url,pic from tb_content where status ='1' and category_id="..id.." order by sort_order";
res = db:query(select_sql);
local responsejson = cjson.encode(res);
red:set("content_"..id,responsejson);
ngx.say(responsejson);
db:close()
else
cache_ngx:set('content_cache_'..id, rescontent, 2*60);
ngx.say(rescontent)
end
red:close()
else
ngx.say(contentCache)
end
更新数据的lua脚本update_content:
ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")
local uri_args = ngx.req.get_uri_args()
local id = uri_args["id"]
local db = mysql:new()
db:set_timeout(1000)
local props = {
host = "ip地址",
port = 3306,
database = "content",
user = "root",
password = "123456"
}
local res = db:connect(props)
local select_sql = "select url,pic from tb_content where status ='1' and category_id="..id.." order by sort_order"
res = db:query(select_sql)
db:close()
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)
local ip ="ip地址"
local port = 6379
red:connect(ip,port)
red:set("content_"..id,cjson.encode(res))
red:close()
ngx.say("{flag:true}")