nginx用lua脚本读取redis(脚本篇)
来源:互联网 发布:腾讯软件下载官方网站 编辑:程序博客网 时间:2024/05/18 18:45
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 numlocal radmnum=math.random(0, 10000000) --生成随机数local radmkey="BASE:USER:STRING.U"..radmnum --根据存放key的规则ngx.say("radmkey is:",radmkey)--get local res, err = cache:get(radmkey) if not res then ngx.say("failed to get key:",radmkey, err) return end if res == ngx.null then ngx.say(radmkey," not found.") return end value = json.encode(res)ngx.say(value)--close local ok, err = cache:close() if not ok then ngx.say("failed to close:", err) return end4.lua脚本(hash类型)
--前面连接和关闭代码同string类型,不重复写了--random numlocal radmnum=math.random(0, 10000000)local radmkey="BASE:USER:HASH.U"..radmnumngx.say("radmkey is:",radmkey)--hgetalllocal res, err = cache:hgetall(radmkey) if not res then ngx.say("failed to get key:",radmkey,err) return end if res == ngx.null then ngx.say(radmkey," not found.") return end value = json.encode(res)ngx.say(value)5.测试
访问:curl http://localhost/test
0 0
- nginx用lua脚本读取redis(脚本篇)
- nginx用lua脚本读取redis(安装篇)
- nginx嵌入lua脚本操作redis
- redis Lua脚本(一)
- redis Lua脚本(二)
- Redis Lua脚本原理
- redis执行Lua脚本
- Redis与Lua脚本
- Redis 与 Lua 脚本
- Redis 与 Lua 脚本
- redis使用Lua脚本
- Redis lua脚本
- redis执行lua脚本
- redis-lua脚本
- Redis调用lua脚本
- Redis学习-LUA脚本
- C++读取lua脚本
- Nginx 内嵌lua脚本,结合Redis使用
- dll的概念、dll导出类
- web工程图片下载解决方案
- WIN 7 64位安装ORACLE 11g 64位问题
- 线程、事件与QObject
- MySQL错误代码
- nginx用lua脚本读取redis(脚本篇)
- JavaSE接触知识收集
- 10件开发者和老板都要知道的HTML5的那些事
- HTML三种样式
- Gabor滤波器学习
- 好记性不如烂笔头34-利用java过滤器实现用户自动登录
- Hive操作语句的使用实例讲解
- javascript原生态Ajax及java注解
- Linux打包压缩命令