php+memcached缓存技术实例

 

一、memcached 简介

在很多场合,我们都会听到 memcached 这个名字,但很多同学只是听过,并没有用过或实际了解过,只知道它是一个很不错的东东。这里简单介绍一下,memcached 是高效、快速的分布式内存对象缓存系统,主要用于加速 WEB 动态应用程序。

二、memcached 服务端安装

首先是下载 memcached 了,目前最新版本是 1.1.12,直接从官方网站即可下载到 memcached-1.1.12.tar.gz。除此之外,memcached 用到了 libevent,我下载的是 libevent-1.1a.tar.gz

接下来是分别将 libevent-1.1a.tar.gz 和 memcached-1.1.12.tar.gz 解开包、编译、安装:

# tar -xzf libevent-1.1a.tar.gz
# cd libevent-1.1a
# ./configure --prefix=/usr
# make
# make install
# cd ..
# tar -xzf memcached-1.1.12.tar.gz
# cd memcached-1.1.12
# ./configure --prefix=/usr
# make
# make install

安装完成之后,memcached 应该在 /usr/bin/memcached。

(当然也可以使用aptitude install memcached或者yum install memcached来安装,即使这样安装可能会安装一些稍旧的版本,但方面多了,而且不容易出错)

三、运行 memcached 守护程序

运行 memcached 守护程序很简单,只需一个命令行即可,不需要修改任何配置文件(也没有配置文件给你修改 ):

/usr/bin/memcached -d -m 128 -l 192.168.1.1 -p 11211 -u httpd

参数解释:

-d 以守护程序(daemon)方式运行 memcached;-m 设置 memcached 可以使用的内存大小,单位为 M; -l 设置监听的 IP 地址,如果是本机的话,通常可以不设置此参数;-p 设置监听的端口,默认为 11211,所以也可以不设置此参数; -u 指定用户,如果当前为 root 的话,需要使用此参数指定用户。

当然,还有其它参数可以用,man memcached 一下就可以看到了。

四、memcached 的工作原理

首先 memcached 是以守护程序方式运行于一个或多个服务器中,随时接受客户端的连接操作,客户端可以由各种语言编写,目前已知的客户端 API 包括 Perl/PHP/Python/Ruby/Java/C#/C 等等。PHP 等客户端在与 memcached 服务建立连接之后,接下来的事情就是存取对象了,每个被存取的对象都有一个唯一的标识符 key,存取操作均通过这个 key 进行,保存到 memcached 中的对象实际上是放置内存中的,并不是保存在 cache 文件中的,这也是为什么 memcached 能够如此高效快速的原因。注意,这些对象并不是持久的,服务停止之后,里边的数据就会丢失。

五、php版memcached客户端的安装 
php有两个版本的memcached客户端 

5.1.memcached 
这个是新版的客户端基于libmemcached,所以必须要安装libmemcached 
先安装libmemcached 
下载地址:http://download.tangent.org/libmemcached-0.26.tar.gz

  1. [root@localhost tools]# tar zxvf libmemcached-0.26.tar.gz  
  2. [root@localhost tools]# cd libmemcached-0.26  
  3. [root@localhost libmemcached-0.26]# ./configure --prefix=/usr/local/libmemcached/ --with-libmemcached-dir=/usr/local/libmemcached/  
  4. [root@localhost libmemcached-0.26]# make  
  5. [root@localhost libmemcached-0.26]# make install  

安装php memcached客户端 
下载地址:http://pecl.php.net/get/memcached 

  1. [root@localhost tools]# tar zxvf memcached-0.1.4.tgz  
  2. [root@localhost tools]# cd memcached-0.1.4  
  3. [root@localhost memcached-0.1.4]# ./configure --with-php-config=/usr/local/php/bin/php-config --with-libmemcached-dir=/usr/local/libmemcached/  
  4. [root@localhost memcached-0.1.4]# make  
  5. [root@localhost memcached-0.1.4]# make install  

修改php.ini添加extension = "memcached.so"就可以了。 
如果出现错误

  1. checking for libmemcached location... configure: error: memcached support requires libmemcached. Use --with-libmemcached-dir=<DIR> to specify the prefix where libmemcached headers and library are located  

请先用whereis libmemcached找到路径,然后添加选项--with-libmemcached-dir=libmemcached路径 

5.2.memcache 
下载地址:http://pecl.php.net/get/memcache 

  1. [root@localhost tools]# tar zxvf memcache-3.0.3.tgz   
  2. [root@localhost tools]# cd memcache-3.0.3  
  3. [root@localhost tools]# /usr/local/php/bin/phpize  
  4. [root@localhost tools]# ./configure --with-php-config=/usr/local/php/bin/php-config  
  5. [root@localhost tools]# make  
  6. [root@localhost tools]# make install 

修改php.ini添加extension = "memcache.so"就可以了。 
提示:如果php找不到so文件,请设置extension_dir。

posted @ 2013-07-05 12:58  beanmoon  阅读(497)  评论(0编辑  收藏  举报