apache+php+memcached+mysql 访问加速方案

来源:互联网 发布:淘宝网首页女装冬装 编辑:程序博客网 时间:2024/04/28 12:08

   Memcache简单介绍:

Memcached 是一个自由和开放源码、高性能、分布的内存对象缓存系统。可用于加速动态web应用程序,减轻数据库的负载。通过在内存里维护一个统一的巨大的hash表,能够来存储各种格式的数据,包括图像、视频、文件以及数据库查询的结果等。

这里的memcache要跟php中的memcache区别开来,php中的memcache是php的支持扩展,而memcached是服务端主程序文件,服务端安装程序,如果要使用memcache来缓存系统,memcache和memcached都要安装。Memcache是一种内存缓存,把经常存取的数据或者对象,缓存在内存中,内存中缓存的这些数据是通过API的方式被存取的,数据就像是一个大的hash表,通过缓存常用的数据或者对象,以此来减轻数据库的压力,提高网站的响应速度。通俗的讲,客户端发出请求到达memcache,如果请求的数据存在memcache中,那么就直接将请求的数据返回,不再对数据进行任何操作。如果请求的数据不存在memcache中,那就去查询数据库,把从数据库中获取的数据返回给客户端,同时把数据缓存一份到memcache中。每次更新数据库的同时,更新memcache中的数据,保证memcache 中的数据与数据库中的数据一致。   


memcache的简单原理示意图


-------------------------------------------------------------------------

安装配置

简单的介绍完了原理,开始安装配置,基础是lamp,这里就不介绍了

memcache 、memcached

[root@localhost softs]# wget http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz

[root@localhost softs]# wget http://pecl.php.net/get/memcache-2.2.6.tgz

[root@localhost softs]# ls memcache* -l
-rw-r--r-- 1 root root  35957 Oct  4  2010 memcache-2.2.6.tgz         ------------------memcache的php扩展
-rw-r--r-- 1 root root 320751 Feb  3  2012 memcached-1.4.13.tar.gz  ----------------------memcache服务端软件

安装memcached

安装该软件时需要libevent的支持,为了防止软件版本太低,所以不建议yum安装

安装libevent

[root@localhost softs]# wget http://cloud.github.com/downloads/libevent/libevent/libevent-2.0.21-stable.tar.gz

[root@localhost softs]# tar xf libevent-2.0.21-stable.tar.gz -C /usr/src/
[root@localhost softs]# cd /usr/src/libevent-2.0.21-stable/

[root@localhost libevent-2.0.21-stable]# ./configure && make && make install

安装 memcached

[root@localhost softs]# tar xf memcached-1.4.13.tar.gz -C /usr/src/
[root@localhost softs]# cd /usr/src/memcached-1.4.13/

[root@localhost memcached-1.4.13]# ./configure --with-libevent=/usr/local && make && make install

[root@localhost run]# memcached -m 32m -p 11211 -d -u root   -P /var/run/memcached.pid -c 256  ----------启动memcached进程

-p  使用的tcp端口,默认为11211;-m最大内存块,默认为64M;-d 作为守护进程在后台运行;-c最大运行的并发数,默认为1024;-P设置保存memcached的pid文件;

-u 表示运行memcached的用户,默认不能由root启动,所以当前用户为root时,必须要指定;-l  是监听服务器的地址,如果有多个地址的话。
[root@localhost memcached-1.4.13]# ps -e | grep mem
16499 ?        00:00:00 memcached
[root@localhost memcached-1.4.13]# netstat -tunlp | grep mem
tcp        0      0 0.0.0.0:11211               0.0.0.0:*                   LISTEN      16499/memcached     
udp        0      0 0.0.0.0:11211               0.0.0.0:*                               16499/memcached     
[root@localhost memcached-1.4.13]# 

关闭memcached

[root@localhost run]# cat /var/run/memcached.pid 
16532
[root@localhost run]# kill -9 16532

安装php的memcache扩展

[root@localhost softs]# tar xf memcache-2.2.6.tgz -C /usr/src/
[root@localhost softs]# cd /usr/src/memcache-2.2.6/

[root@localhost memcache-2.2.6]# ls
CREDITS       memcache.php
README        memcache_consistent_hash.c
config.m4     memcache_queue.c
config.w32    memcache_queue.h
config9.m4    memcache_session.c
example.php   memcache_standard_hash.c
memcache.c    php_memcache.h
memcache.dsp
[root@localhost memcache-2.2.6]# phpize 
Configuring for:
PHP Api Version:         20090626
Zend Module Api No:      20090626
Zend Extension Api No:   220090626

[root@localhost memcache-2.2.6]# ls
CREDITS          config.sub    memcache.php
Makefile.global  config.w32    memcache_consistent_hash.c
README           config9.m4    memcache_queue.c
acinclude.m4     configure     memcache_queue.h
aclocal.m4       configure.in  memcache_session.c
autom4te.cache   example.php   memcache_standard_hash.c
build            install-sh    missing
config.guess     ltmain.sh     mkinstalldirs
config.h.in      memcache.c    php_memcache.h
config.m4        memcache.dsp  run-tests.php
[root@localhost memcache-2.2.6]# 

[root@localhost memcache-2.2.6]# ./configure --enable-memcache --with-php-config=/usr/local/bin/php-config 

 [root@localhost memcache-2.2.6]# make 

[root@localhost memcache-2.2.6]# make install
Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-zts-20090626/

[root@localhost memcache-2.2.6]# cd modules/
[root@localhost modules]# ls
memcache.so
[root@localhost modules]# pwd
/usr/src/memcache-2.2.6/modules

接下来修改php配置文件php.ini

[root@localhost lib]# vi php.ini

extension="/usr/local/lib/php/extensions/no-debug-zts-20090626/"

extension=memcache.so ---------------在文件中加上这两行

测试memcache的php扩展是否安装成功

[root@localhost lib]# memcached -m 32m -p 11211 -u root -P /var/run/memcached.pid -c 256 -d 
[root@localhost lib]# /etc/init.d/apache start

[root@localhost ~]# cd /www/   --------------------/www是我的apache的Document Root

[root@localhost www]# vi mem_test.php 
<?php
  $mem=new Memcache;
  $mem->connect("127.0.0.1",11211);
  $mem->set('hello','world',0,60);
  $val=$mem->get('hello');
  echo $val;
?>

[root@localhost www]# elinks --dump 127.0.0.1/mem_test.php 
   world

如果可以看到world,那你的memcachephp扩展就成功了

-------------------------------------------------------------------------

memcache与php的配合使用

php与memcache结合测试代码

[root@localhost www]# vi php-mem.php
<?php
  //connect
  $mem = new Memcache;    --------初始化一个memcache对象
  $mem->connect('localhost',11211);     ---------------连接到memcache服务器,IP及端口
  
  //save data
  $mem->set('key1','this is first value',0,60);  -------key 数据的key,用来定位一个数据;第二个参数是需要保存的数据内容;第三个参数是标记,一般为0,第四个参数是混村的有效时间,超时就会被清除。
  $val = $mem->get('key1');   -------------get就是要获取数据,获取key1数据
  echo "Get key1 value:".$val."<br/>";
  
  //replace data
  $mem->replace('key1','this is replace value',0,60);
  $val = $mem->get('key1');
  echo "Get key1 value:".$val."<br/>";
  
  //save data group
  $arr = array('aa','bb','cc');
  $val = $mem->get('key1');
  echo "Get key1 value:".$val."<br/>";


  //replace data
  $mem->replace('key1','this is replace value',0,60); -------------替换key1
  $val = $mem->get('key1'); -------------保存key1
  echo "Get key1 value:".$val."<br/>";


  //save data group
  $arr = array('aa','bb','cc');
  $mem->set('key2',$arr,0,60);
  $val2 = $mem->get('key2');
  echo "Get key2 value:";
  print_r($val2);
  echo "<br/>";


  //delete data
  $mem->delete('key1'); -----------删除key1
  $val=$mem->get('key1');--------------保存key1
  echo "get key1 value:".$val."<br/>";


  //close connetions 
  $mem->close();  --------关闭

?>
"php-mem.php" [New] 36L, 678C written
[root@localhost www]# elinks --dump 127.0.0.1/php-mem.php
   Get key1 value:this is first value
   Get key1 value:this is replace value
   Get key2 value:Array ( [0] => aa [1] => bb [2] => cc )
   get key1 value:

现在就成功了。