Skip to content

安装

yum install -y memcached

查看状态

systemctl start memcached memcached-tool 127.0.0.1:11211 stats

连接

yum install -y telnet telnet 127.0.0.1 11211

添加

先设置 key,再设置 value。 /foo 代表 key,key 后第一个参数代表元数据信息,第二个参数代表过期时间,第三个参数代表 value 的字节数。

set /foo 0 0 3 bar

获取

get foo

nginx + memcached

nginx
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout 60;

    server {
        listen       80;
        server_name  192.168.44.101;
       
        location / {
            # 去 memcache 中找请求的资源,如果没找到返回 404,404 反向代理去上游
            # 服务器取资源,上游服务器返回内容同时把内容写进 memcached,之后再请求就
            # 能从 memcached 中找到了。
            set $memcached_key "$uri";
            memcached_pass 127.0.0.1:11211;
	        add_header X-Cache-Satus HIT;
	        add_header Content-Type 'text/html; charset=utf-8'; 
        }

        error_page 404 = @fallback;
        location @fallback {
            proxy_set_header Memcached-Key $memcached_key;
            proxy_pass http://192.168.44.103:80;
        } 

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}