nginx与memcache

1.nginx的memcached_module模块

"The ngx_http_memcached_module module is used to obtain responses from a memcached server. The key is set in the$memcached_key variable. A response should be put in memcached in advance by means external to nginx."

ngx_http_memcached是核心模块,不需要另外编译。他只能get,不能set,对于set情况,需要通过后端处理。

 location ^~ /memcache {
         set $memcached_key  $arg_key;
         memcached_pass     localhost:11211;
         add_header         X-mem $memcached_key;
         error_page         403  404 502 504 = @fallback;
         default_type       text/html;
}
location @fallback{
        rewrite ^/.*$ /setmemcache.php?key=$arg_key;
       }
// setmemcache.php
header("x-mem:mis");
$key = isset($_GET['key']) ? $_GET['key'] : null ;
if($key !== null){
  $value = "you make it by your work";
  $m = new Memcache;
  $m->connect('localhost',11211);
  $m->set($key,$value);
  
}
测试如下:

2.nginx与HttpMemcModule

编译添加模块

--prefix=/usr/local/nginx81 --add-module=/usr/local/src/memc-nginx-module-0.14/ --add-module=/usr/local/src/echo-nginx-module-0.53/

make 

cp  objs/nginx  /pathto/nginx

配置

location /amem {
         set $memc_key $arg_key;
         set $memc_cmd $arg_cmd;
         set $memc_value $arg_val;
         set $memc_exptime $arg_exptime;
         memc_pass 127.0.0.1:11211;
         add_header X-Memc-key $memc_key;
     }

测试
set :http://localhost:81/amem?key=ab&cmd=set&val=1   
get :http://localhost:81/amem?key=ab&cmd=get&val=1   
delete :http://localhost:81/amem?key=ab&cmd=delete&val=1   
flush_all :http://localhost:81/amem?key=ab&cmd=flush_all&val=1   
version :http://localhost:81/amem?key=ab&cmd=version&val=1   
incr:http://localhost:81/amem?key=ab&cmd=incr&val=1   
decr:http://localhost:81/amem?key=ab&cmd=decr&val=1  
stats:http://localhost:81/amem?key=ab&cmd=stats&val=1 


 可见,常见的方法,他都可以支持。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

anssummer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值