redis没有直接提供一个http的接口,要是用php,python,当然也是可以实现的。
性能的比较的话,lua的能力要比php强的不少。。。
网上有很多的性能的比较,我也做过几次的压力测试,lua的性能确实很强。。。。
安装nginx,以及lua环境git clone https://github.com/simpl/ngx_devel_kit.git
git clone https://github.com/chaoslawful/lua-nginx-module.git
git clone https://github.com/agentzh/redis2-nginx-module.git
git clone https://github.com/agentzh/set-misc-nginx-module.git
git clone https://github.com/agentzh/echo-nginx-module.git
yum -y install pcre pcre-dev*
wget http://nginx.org/download/nginx-1.3.14.tar.gz
tar zxvf nginx-1.3.14.tar.gz
cd nginx-1.3.14
./configure --prefix=/usr/local/nginx --add-module=../ngx_devel_kit/ --add-module=../lua-nginx-module --add-module=../redis2-nginx-module --add-module=../set-misc-nginx-module --add-module=../echo-nginx-module
make && make install
注:在 server 段里,加入代码,如果不加此代码或者设置为 on 时,则需要重启 Nginx。
lua_code_cache off;
Nginx.conf
server{
listen 80;
server_name test.lua.com;
#http://test.lua.com/lua
location /hello {
default_type "text/plain";
content_by_lua 'ngx.say("Nginx Lua Hello!")';
}
#GET http://test.lua.com/get?key=key
location /get {
set_unescape_uri $key $arg_key;
redis2_query get $key;
redis2_pass 127.0.0.1:6379; #配置redis访问
}
#SET http://test.lua.com/set?key=key&val=value
location /set {
set_unescape_uri $key $arg_key;
set_unescape_uri $val $arg_val;
redis2_query set $key $val;
redis2_pass 127.0.0.1:6379;
}
}
重启Nginx
/etc/init.d/nginx restart
也可以直接用lua调用redis的接口
local ckid = redis.pcall('get',KEYS[1])
local meta
if ckid ~= nil then
meta = redis.call('hgetall', ckid)
else
meta = 'none'
ckid = 'none'
end
return {ckid, meta}
分享一个完成的实例:location /foo {
set $value 'first';
redis2_query set one $value;
redis2_pass 127.0.0.1:6379;
}
# GET /get?key=some_key
location /get {
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
redis2_query get $key;
redis2_pass foo.com:6379;
}
# GET /set?key=one&val=first%20value
location /set {
set_unescape_uri $key $arg_key; # this requires ngx_set_misc
set_unescape_uri $val $arg_val; # this requires ngx_set_misc
redis2_query set $key $val;
redis2_pass foo.com:6379;
}
# multiple pipelined queries
location /foo {
set $value 'first';
redis2_query set one $value;
redis2_query get one;
redis2_query set one two;
redis2_query get one;
redis2_pass 127.0.0.1:6379;
}
location /bar {
# $ is not special here...
redis2_literal_raw_query '*1\r\n$4\r\nping\r\n';
redis2_pass 127.0.0.1:6379;
}
location /bar {
# variables can be used below and $ is special
redis2_raw_query 'get one\r\n';
redis2_pass 127.0.0.1:6379;
}
# GET /baz?get%20foo%0d%0a
location /baz {
set_unescape_uri $query $query_string; # this requires the ngx_set_misc module
redis2_raw_query $query;
redis2_pass 127.0.0.1:6379;
}
location /init {
redis2_query del key1;
redis2_query lpush key1 C;
redis2_query lpush key1 B;
redis2_query lpush key1 A;
redis2_pass 127.0.0.1:6379;
}
location /get {
redis2_query lrange key1 0 -1;
redis2_pass 127.0.0.1:6379;
}