rtre 发表于 2015-9-25 08:45:45

高效缓存服务器Memcached(二)

一、Memcache 应用一 php与memcache
1、PHP的memcache的客户端
   PHP有两个memcache客户端:php memcache和php memcached。php memcache独立用php实现,是老客户端,从我们实践中已发现有多个问题,而且功能少,属性也可设置的少;php memcached是基于原生的c的libmemcached的扩展,更加完善,建议替换为php memcached。
2、PHP memcache的问题
(1).分布式问题
php memcache默认会自动切换实例,所以有时取到老数据,并且value飘忽不定
(2).高并发下稳定性问题
php memcache换成php memcached,在高并发下稳定下极大提高;
(3).秒超时间隔没法修改问题
php memcache客户端有个1秒超时间隔没法修改问题:bool Memcache::connect ( string $host [, int $port [, int $timeout ]] )第三个参数本来可设置timeout,单位秒,但无法修改。
测试了以下三种修改timeout的方法都无效:
用memcache api Memcache::setServerParams不能修改;
改memcache 源代码vi php_memcache.h宏定义不能修改;
php.ini内这个配置:default_socket_timeout = 60对本timeout无效。
(4).memcache和memcached对比
注,PHP memcache这个老客户端在属性设置方面可设置的很少,出错码粒度很粗,出错后难以定位,而且功能欠缺一些,如下图:

注,综合上述建议大家使用php memcached。还有更多知识点请参考官方文
3、PHP 安装memcached扩展

1
# yum -y install gcc+ gcc-c++




解决memcached的依赖关系需要安装libmemcached,注意这里最好安装1.0.16版本,我试过17、18版本一直无法装上:

1
2
3
4
5
6
# tar -xf libmemcached-1.0.16.tar.gz
# cd libmemcached-1.0.16
# mkdir/usr/local/libmemcached
# ./configure -prefix=/usr/local/libmemcached --with-memcached
# make
# make install




安装PHP memcached扩展:

1
2
3
4
5
6
7
8
9
10
# tar -xf memcached-2.2.0.tgz
# cd memcached-2.2.0
# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20131106
Zend Module Api No:      20131226
Zend Extension Api No:   220131226
# ./configure--enable-memcached --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached --disable-memcached-sasl
#make && make install
Installing shared extensions:   /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/




添加配置文件:

1
2
# vim /etc/php.d/memcached.ini
extension= "/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcached.so"




添加测试页:

1
2
3
4
# cat /www/a.com/index.php
<?php
    phpinfo();
?>




浏览器测试:

4、PHP 安装memcache扩展
PECL 简介
   PECL 的全称是 The PHP Extension Community Library ,是一个开放的并通过 PEAR(PHP Extension and Application Repository,PHP 扩展和应用仓库)打包格式来打包安装的 PHP 扩展库仓库。通过 PEAR 的 Package Manager 的安装管理方式,可以对 PECL 模块进行下载和安装。与以往的多数 PEAR 包不同的是,PECL 扩展包含的是可以编译进 PHP Core 的 C 语言代码,因此可以将 PECL 扩展库编译成为可动态加载的 .so 共享库,或者采用静态编译方式与 PHP 源代码编译为一体的方法进行扩展。PECL 扩展库包含了对于 XML 解析,数据库访问,邮件解析,嵌入式的 Perl 以及 Pthyon 脚本解释器等诸多的 PHP 扩展模块,因此从某种意义上来说,在运行效率上 PECL 要高于以往诸多的 PEAR 扩展库。
PECL常用选项:

[*]search 查找一下模块

[*]info 查看模块信息

[*]install 安装查找到的模块

使用PECL安装memcache模块:

1
2
3
4
5
6
# /usr/local/php/bin/pecl install memcache
Build process completed successfully
Installing '/usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so'
install ok: channel://pecl.php.net/memcache-2.2.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=memcache.so" to php.ini




接下来我们来增加php memcache的配置文件:

1
2
3
4
5
# vim /etc/php.d/memcache.ini
extension = "memcache.so"
# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpmdone




下面我们来查看一下php的模块:


1
2
3
# /usr/local/php/bin/php -m|grep memcache
memcache
memcached




浏览器测试:

添加Memcached的测试脚本:

1
2
3
4
5
6
7
8
9
10
11
# cat memcached.php
<?php
$mem = new Memcache;
$mem->connect("192.168.1.6", 11211)or die("Could not connect");
$version = $mem->getVersion();
echo "Server's version: ".$version."<br/>\n";
$mem->set('hellokey', 'Hello World', 0, 600) or die("Failed to save data at the memcached server");
echo "Store data in the cache (data will expire in 600 seconds)<br/>\n";
$get_result = $mem->get('hellokey');
echo "$get_result is from memcached server.";         
?>






1
2
3
4
5
6
7
8
# telnet 192.168.1.6 11211
Trying 192.168.1.6...
Connected to 192.168.1.6.
Escape character is '^]'.
get hellokey
VALUE hellokey 0 11
Hello World
END






二、Memcache 应用二 nginx与memcache
1、memcached_module模块
   nginx的memcached_module模块可以直接从memcached服务器中读取内容后输出,后续的请求不再经过应用程序处理,如php-fpm、django,大大的提升动态页面的速度。nginx只负责从memcached服务器中读取数据,要往memcached写入数据还得需要后台的应用程序来完成,主动的将要缓存的页面缓存到memcached中,可以通过404重定向到后端去处理的。ngx_http_memcached_module可以操作任何兼用memcached协议的软件。如ttserver、membase等。
结构图如下:

memcached的key可以通过memcached_key变量来设置,如以$uri。如果命中,那么直接输出内容,没有命中就意味着nginx需要从应用程序请求页面。同时,我们还希望该应用程序将键值对写入到memcached,以便下一个请求可以直接从memcached获取。如果键值不存在,nginx将报告not found错误。最好的方法是使用error_page指定和location请求处理。同时包含”Bad Gateway”错误和”Gateway Timeout”错误,如:error_page 404 502 504 = @app;。注意:需要设置default_type,否则可能会显示不正常。
2、模块说明

[*]memcached_bind :指定从哪个IP来连接memcached服务器

语法: memcached_bind address | off;   
默认值: none   
配置段: http, server, location   

[*]memcached_buffer_size :读取从memcached服务器接收到响应的缓冲大小。尽快的将响应同步传给客户端。   

语法: memcached_buffer_size size;   
默认值: 4k|8k;   
配置段: http, server, location   

[*]memcached_connect_timeout :与memcached服务器建立连接的超时时间。通常不超过75s。

语法:memcached_connect_timeout time;   
默认值:60s;   
配置段:http, server, location   

[*]memcached_gzip_flag :测试memcached服务器响应标志。如果设置了,将在响应头部添加了Content-Encoding:gzip。   

语法:memcached_gzip_flag flag;   
默认值:none   
配置段:http, server, location   

[*]memcached_next_upstream :指定在哪些状态下请求将转发到另外的负载均衡服务器上,仅当memcached_pass有两个或两个以上时使用。   

语法: memcached_next_upstream error | timeout | invalid_response | not_found | off …;   
默认值: error timeout;   
配置段: http, server, location   

[*]memcached_pass :指定memcached服务器地址。使用变量$memcached_key为key查询值,如果没有相应的值则返回error_page 404。

语法:memcached_pass address:port or socket;   
默认值:none   
配置段:location, if in location   

[*]memcached_read_timeout :定义从memcached服务器读取响应超时时间。

语法:memcached_read_timeout time;   
默认值:60s;   
配置段:http, server, location   

[*]memcached_send_timeout:设置发送请求到memcached服务器的超时时间。   

语法:memcached_send_timeout   
默认值:60s   
配置段:http, server, location   
用法:$memcached_key变量:memcached key的值。
3、nginx memcached的增强版ngx_http_enhanced_memcached_module基于nginx memcached 模块的,添加的新特性有:

[*]自定义HTTP头,如Content-Type, Last-Modified。

[*]hash键可超过250个字符,memcached受限。

[*]通过HTTP请求将数据存储到memcached。

[*]通过HTTP请求从memcached删除数据。

[*]通过HTTP请求清除所有memcached缓存数据。

[*]通过HTTP请求获取memcached状态数据。

[*]键名空间管理,来部分刷新缓存。

[*]缓存通过If-Modified-Since头和内容Last-Modified来回复304Not Modified请求。

4、nginx整合memcache

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
      listen       80;
      server_namewww.test.com;
      #charset koi8-r;
      #access_loglogs/host.access.logmain;
      location / {
                set $memcached_key $uri;
                memcached_pass   192.168.1.6:11211;
                default_type       text/html;
                error_page         404 @fallback;
      }
      location @fallback {
                proxy_pass http://192.168.1.9;
      }
}




从上面的配置文件我们可以看出,一个请求到达后,会其uri作为key去Memcached服务器192.168.1.6:11211上查找value,如果没有命中,则返回404。这时通过error_page将404接收转到@fallback,返回给Web服务去处理请求。

三、Memcache 应用三 session与memcache
1、配置node1与node2安装php memcache扩展
注,上面已经演示过怎么安装php memcache扩展,这里就不再演示。
2、安装Memcache服务器
注,同上大家自己安装一下。
3、修改php配置文件

1
2
3
# vim /etc/php.ini
session.save_handler = memcache
session.save_path = "tcp://192.168.1.6:11211?persistent=1&weight=1&timeout=1&retry_interval=15"




4、增加测试页面
新建php页面setsess.php,为客户端设置启用session。

1
2
3
4
5
6
7
8
9
<?php
session_start();
if (!isset($_SESSION['www.example.com'])) {
$_SESSION['www.example.com'] = time();
}
print $_SESSION['www.example.com'];
print "<br><br>";
print "Session ID: " . session_id();
?>




说明:新建php页面showsess.php,获取当前用户的会话ID。

1
2
3
4
5
6
7
8
<?php
session_start();
$memcache_obj = new Memcache;
$memcache_obj->connect('192.168.1.6', 11211);
$mysess=session_id();
var_dump($memcache_obj->get($mysess));
$memcache_obj->close();
?>





四、memcache 图形管理工具
1、解压memadmin

1
2
# tar -xf memadmin-1.0.12.tar.gz
# cd memadmin




2、移动memadmin到页面目录下

1
2
# mv ../memadmin /www/a.com/
# cd /www/a.com/




3、查看一下配置文件

1
2
3
# vim memadmin/config.php
$config['user'] = "admin"; // your username
$config['passwd'] = "admin"; // your password




默认的用户名是admin,密码是admin。
4、浏览器测试





页: [1]
查看完整版本: 高效缓存服务器Memcached(二)