OpenResty lua_shared_dict (缓存)

1 案例

1.1 get_value.lua

描述: 从redis中获取value

local redis = require "redis_conf"
local red = redis:new()


function get_from_redis(key)
    local res,err=red:get(key)
    if res then
        return res
    else
        return err
    end
end

local value=get_from_redis('value')
ngx.say("value: ", value)

1.2 nginx.conf

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    sendfile        on;
  
    keepalive_timeout  65;
    
    lua_package_path  "/usr/local/openresty/nginx/lua/conf/?.lua;;";
    
    server {
        listen       80;
        server_name  localhost;
        
       lua_code_cache on;
       
       location / {
            root   html;
            index  index.html index.htm;
       }    
       
       location /get_value {
           default_type 'text/html';
           content_by_lua_file  lua/get_value.lua;
       }   
       
    }
    
}

1.3 ab测试

#重启
/usr/local/openresty/nginx/sbin/nginx  -c /usr/local/openresty/nginx/conf/nginx.conf  -s reload
#测试10W并发 100客户端
ab -n 100000 -c 100  -k http://192.168.38.38/get_value

在这里插入图片描述

2 加入lua_shared_dict

2.1 nginx.conf

lua_shared_dict cache_ngx 128m;

在这里插入图片描述

2.2 get_value.lua


local redis = require "redis_conf"
local red = redis:new()

-- 从redis中获取
function get_from_redis(key)
    local res, err = red:get(key)
    if res then
        return res
    else
        return err
    end
end


-- 设置nginx 缓存数据
function set_to_cache(key, value, expire)
    if not expire then
        expire = 0
    end

    local cache_ngx = ngx.shared.cache_ngx
    local succ, err, forcible  = cache_ngx:set(key, value, expire)
    return succ
end


-- 从nginx缓存中获取
function get_from_cache(key)

    local cache_ngx = ngx.shared.cache_ngx
    local value = cache_ngx:get(key)

    if not value then
        value = get_from_redis(key)
        set_to_cache(key, value)
    end

    return value
end

local value = get_from_cache('value')

ngx.say("value: ", value)

2.3 ab 测试

#重启
ab -n 100000 -c 100  -k http://192.168.38.38/get_value

在这里插入图片描述

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
`ngx_upstream_module` 是 OpenResty 中的一个模块,它提供了对 upstream 的支持。Upstream 是一个概念,用于表示一组后端服务器。在使用 upstream 时,客户端的请求会被反向代理到一个或多个后端服务器上进行处理。 `ngx_upstream_module` 提供了一些方法和指令,可以动态地管理 upstream 中的服务器,包括添加、删除、修改服务器等操作。常用的指令包括: - upstream:定义一个 upstream。 - server:定义一个服务器,包括地址和端口等信息。 - keepalive:设置连接池的大小。 - least_conn:使用最少连接数的负载均衡算法。 - ip_hash:使用 IP 地址进行哈希的负载均衡算法。 常用的方法包括: - ngx.upstream.get_servers(name):获取一个 upstream 中所有的服务器。 - ngx.upstream.add_server(name, server, weight):向一个 upstream 中添加服务器。 - ngx.upstream.remove_server(name, server):从一个 upstream 中删除服务器。 - ngx.upstream.set_peer_down(name, server):将一个服务器标记为不可用。 - ngx.upstream.set_peer_up(name, server):将一个服务器标记为可用。 在使用 `ngx_upstream_module` 时,需要在编译 OpenResty 时包含 `--with-http_upstream_module` 选项。可以通过以下命令检查当前 OpenResty 是否已经安装了 `ngx_upstream_module` 模块: ```shell $ /path/to/openresty/nginx/sbin/nginx -V 2>&1 | grep -o with-http_upstream_module ``` 需要将 `/path/to/openresty` 替换为实际的 OpenResty 安装路径。如果输出 `with-http_upstream_module`,则说明已经安装了 `ngx_upstream_module` 模块。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

响彻天堂丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值