nginx用lua脚本读取redis(脚本篇)

本文旨在介绍如何在Nginx中通过Lua脚本生成随机key并从Redis获取对应的string和hash类型值。通过配置Nginx,可以实现访问特定URL触发Lua脚本与Redis进行交互。
摘要由CSDN通过智能技术生成

1.目的

     实现生成随机的key,从redis中取出该key的值(string类型和hash类型)

2.配置nginx

lua_package_path "/usr/local/lua/?.lua;;";     //在http中增加

location /test {  
			default_type 'text/plain';
			content_by_lua_file /usr/local/luascript/radmstr.lua;  
		} 

3.lua脚本(string类型)

local redis = require "resty.redis"  
local json = require "json"

--connect  
local cache = redis.new()  
  
local ok, err = cache.connect(cache, '127.0.0.1', '6379')  
  
cache:set_timeout(1000)  
  
if not ok then  
        ngx.say("failed to connect:", err)  
        return  
end  

--random num
local radmnum=math.random(0, 10000
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值