OpenResty、lua实现多级缓存

1 篇文章 0 订阅
1 篇文章 0 订阅

在这里插入图片描述

lua安装

curl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gz
tar zxf lua-5.3.5.tar.gz
cd lua-5.3.5
make linux test

注意:此时安装,有可能会出现如下错误:
在这里插入图片描述
此时需要安装lua相关依赖库的支持,执行如下命令即可:
yum install libtermcap-devel ncurses-devel libevent-devel readline-devel

此时再执行lua测试看lua是否安装成功

[root@localhost ~]# lua
Lua 5.1.4  Copyright (C) 1994-2008 Lua.org, PUC-Rio

OpenResty安装

# 添加仓库执行命令
 yum install yum-utils
 yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
# 执行安装
yum install openresty
# 安装成功后会有这个文件夹 /usr/local/openresty

nginx安装

默认已经安装好了nginx,在目录:/usr/local/openresty/nginx 下。
修改/usr/local/openresty/nginx/conf/nginx.conf,将配置文件使用的根设置为root,目的就是将来要使用lua脚本的时候 ,直接可以加载在root下的lua脚本。

cd /usr/local/openresty/nginx/conf
vi nginx.conf

在这里插入图片描述

nginx、redis缓存脚本

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; --加载nginx缓存模块(需要在nginx.conf中定义)
--在nginx.conf中配置lua_shared_dict dis_cache 128m; 缓存模块
--根据ID 获取本地缓存数据
local contentCache = cache_ngx:get('content_cache_'..id);

if contentCache == "" or contentCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    red:connect("192.168.211.132", 6379)
    local rescontent=red:get("content_"..id);

    if ngx.null == rescontent then
        local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "192.168.211.132",
            port = 3306,
            database = "changgou_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, 10*60); --设置缓存时间10分钟
        ngx.say(rescontent)
    end
    red:close()
else
    ngx.say(contentCache)
end

nginx配置

#目的就是将来要使用lua脚本的时候 ,直接可以加载在root下的lua脚本
user  root root;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #定义Nginx缓存模块,模块名字叫dis_cache,容量大小128M(与lua脚本的缓存相对应:local cache_ngx = ngx.shared.dis_cache;)
    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.232.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/lua65/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/lua65/update_content.lua;
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值