Nginx + Lua + redis (一)
2015-08-28 14:52
477 查看
使用 Lua 脚本语言操作 Redis。
由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua脚本文件。
要使用 content_by_lua_file,需要安装 nginx_lua_module 模块。
安装介绍,猛击这里:nginx_lua_module
大神 章亦春 提供了一个很方便的开发包,如下:
[plain] view
plaincopyprint?
git clone https://github.com/agentzh/lua-resty-redis.git
该包中,有一个 Lib 目录,将 Lib 目录下的文件和子目录拷贝至目录 /data/www/lua
在 Nginx 配置文件中,需要加一行代码,以便引入 redis.lua。
注:加在 http 段里。
[plain] view
plaincopyprint?
lua_package_path "/data/www/lua/?.lua;;";
为了使得 lua 脚本的修改能及时生效,需要加入一行代码,如下:
注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。
[plain] view
plaincopyprint?
lua_code_cache off;
在 Nginx 配置文件中,加入一个Location:
[plain] view
plaincopyprint?
location /lua {
content_by_lua_file /data/www/lua/test.lua;
}
注:引入 test.lua 脚本文件
Lua 脚本文件:test.lua。
[plain] view
plaincopyprint?
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
res, err = cache:set("dog", "an aniaml")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
测试结果如下:
[plain] view
plaincopyprint?
[root@localhost conf]# curl http://localhost/lua
set result: OK
dog: an aniaml
成功!
由于大量的 Lua 代码写在 Nginx 中,会使配置文件显得很繁琐,所以这里使用 content_by_lua_file 来引入 Lua脚本文件。
要使用 content_by_lua_file,需要安装 nginx_lua_module 模块。
安装介绍,猛击这里:nginx_lua_module
大神 章亦春 提供了一个很方便的开发包,如下:
[plain] view
plaincopyprint?
git clone https://github.com/agentzh/lua-resty-redis.git
该包中,有一个 Lib 目录,将 Lib 目录下的文件和子目录拷贝至目录 /data/www/lua
在 Nginx 配置文件中,需要加一行代码,以便引入 redis.lua。
注:加在 http 段里。
[plain] view
plaincopyprint?
lua_package_path "/data/www/lua/?.lua;;";
为了使得 lua 脚本的修改能及时生效,需要加入一行代码,如下:
注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。
[plain] view
plaincopyprint?
lua_code_cache off;
在 Nginx 配置文件中,加入一个Location:
[plain] view
plaincopyprint?
location /lua {
content_by_lua_file /data/www/lua/test.lua;
}
注:引入 test.lua 脚本文件
Lua 脚本文件:test.lua。
[plain] view
plaincopyprint?
local redis = require "resty.redis"
local cache = redis.new()
local ok, err = cache.connect(cache, '127.0.0.1', '6379')
cache:set_timeout(60000)
if not ok then
ngx.say("failed to connect:", err)
return
end
res, err = cache:set("dog", "an aniaml")
if not ok then
ngx.say("failed to set dog: ", err)
return
end
ngx.say("set result: ", res)
local res, err = cache:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
end
if res == ngx.null then
ngx.say("dog not found.")
return
end
ngx.say("dog: ", res)
local ok, err = cache:close()
if not ok then
ngx.say("failed to close:", err)
return
end
测试结果如下:
[plain] view
plaincopyprint?
[root@localhost conf]# curl http://localhost/lua
set result: OK
dog: an aniaml
成功!
相关文章推荐
- redis获取自增长序号
- Maven中Spring-Data-Redis存储对象(redisTemplate)
- linux 下安装、使用 redis
- Redis及phpredis安装配置
- 通过Gearman实现MySQL到Redis的数据复制
- redis 安装与php扩展
- Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式)介绍
- redis事务中的WATCH命令和基于CAS的乐观锁
- Redis HASH 操作类型封装
- nginx+tomcat+redis完成session共享
- Redis管道(Pipelining)操作
- Jedis使用总结【pipeline】【分布式的id生成器】【分布式锁【watch】【multi】】【redis分布式】
- redis同时删除多个表
- Redis主从配置详细过程
- redis-有序集合
- maven构建项目实现对redis的操作(jedis)
- redis的数据类型 (一) 字符串
- redis常见命令
- zabbix针对redis监控