Memcache和memcached

来源:互联网 发布:淘宝的五星评价在哪里 编辑:程序博客网 时间:2024/06/08 12:32

Memcache和memcached

memcached 是以LiveJournal 旗下Danga Interactive 公司的Brad Fitzpatric 为首开发的一款软件。现在已成为 mixi、 hatena、 Facebook、 Vox、LiveJournal等众多服务中提高Web应用扩展性的重要因素。

许多Web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示。但随着数据量的增大、访问的集中,就会出现RDBMS的负担加重、数据库响应恶化、网站显示延迟等重大影响。

    这时就该memcached大显身手了。memcached是高性能的分布式内存缓存服务器。一般的使用目的是,通过缓存数据库查询结果,减少数据库访问次数,以提高动态Web应用的速度、提高可扩展性。


为什么会有Memcache和memcached两种名称?
其实Memcache是这个项目的名称,而memcached是它服务器端的主程序文件名,
 
Memcache官方网站:http://www.danga.com/memcached

 

一、memcached 的编译安装
准备:
Memcached下载地址:http://www.danga.com/memcached/
libevent下载地址: http://monkey.org/~provos/libevent/
下载:
wget http://memcached.googlecode.com/files/memcached-1.4.2.tar.gz
wget http://monkey.org/~provos/libevent-1.4.12-stable.tar.gz
wget http://pecl.php.net/get/memcache-2.2.5.tgz

libevent,编译安装
     # tar zxvf libevent-1.4.12-stable.tar.gz
     # cd libevent-1.4.12-stable
     # ./configure --prefix=/usr/local/libevent
     # make && make install

memcached 编译安装
# tar zxvf memcached-1.4.2.tar.gz
# cd memcached-1.4.2
# ./configure --prefix=/usr/local/memcached --with-libevent=/usr/local/libevent/
# make && make install

 

作软连接,否则运行memcached的时候将找不到libevent模块
# ln -s /usr/local/libevent/lib/libevent-1.4.so.2 /usr/lib/

 

二、安装Memcache的PHP扩展


# tar zxvf memcache-2.2.5.tgz
# /usr/local/php/bin/phpize

(注:此处可能会出现如下错误:

 Cannot find config.m4.

  Make sure that you run /usr/local/bin/phpize in the top level source directory of the module

此问题解决办法,在文章末尾给与说明。)
########################################################################################
若出现:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
通过安装 autoconf 可以解决
centos下执行 yum install autoconf 即可
ubuntu下执行 apt-get install autoconf 即可
#######################################################################################

# ./configure --enable-memcache --with-php-config=/usr/local/php/bin/php-config --with-zlib-dir
# make
# make installl

安装完后会有类似这样的提示:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-zts-20060613/

把这个记住,然后修改php.ini,把

extension_dir = "./"

修改为

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-zts-20060613/"

并添加一行来载入memcache扩展:extension=memcache.so

# vi /usr/local/php/lib/php.ini


启动Memcache的服务器端:

# /usr/local/memcached/bin/memcached  -d -m 10 -u root -l 127.0.0.1 -p 12000 -c 256 -P /tmp/memcached.pid

-d选项是启动一个守护进程,

-m是分配给Memcache使用的内存数量,单位是MB;

-u是运行Memcache的用户;

-l是监听的服务器IP地址;

-p是设置Memcache监听的端口,最好是1024以上的端口;

-c选项是最大运行的并发连接数,默认是1024;

-P是设置保存Memcache的pid文件,这里是保存在 /tmp/memcached.pid;

如果要结束Memcache进程,执行:

# kill `cat /tmp/memcached.pid`

也可以启动多个守护进程,不过端口不能重复。



附上 /usr/local/php/bin/phpize  出错的解决方法:

    [root@ns root]# phpize

  Cannot find config.m4.

  Make sure that you run /usr/local/bin/phpize in the top level source directory of the module

  [root@ns root]# phpize

  Cannot find config.m4.

  Make sure that you run /usr/local/bin/phpize in the top level source directory of the module

  你会看到这两种结果实际上你查看了这个脚本,很轻松的就会发现是怎么来处理的。

  我的php源码包放在 

    /usr/src/php/php-5.2.11下

  我们在/usr/src/php/php-5.2.11/ext下找到这个工具

  来建立一个php扩展的一个框架

  [root@ns ext]#cd /usr/src/php/php-5.2.11/ext/

  [root@ns ext]# ./ext_skel --extname=sdomain

  Creating directory sdomain

  Creating basic files: config.m4 .cvsignore sdomain.c php_sdomain.h CREDITS EXPERIMENTAL tests/001.phpt sdomain.php

[done].

To use your new extension, you will have to execute the following steps:

  1. $ cd ..

  2. $ vi ext/sdomain/config.m4

  3. $ ./buildconf

  4. $ ./configure --[with|enable]-sdomain

  5. $ make

  6. $ ./php -f ext/sdomain/sdomain.php

  7. $ vi ext/sdomain/sdomain.c

  8. $ make

  执行了这个步骤以后你会看到这样的结果

  Repeat steps 3-6 until you are satisfied with ext/sdomain/config.m4 and

  step 6 confirms that your module is compiled into PHP. Then, start writing

  code and repeat the last two steps as often as necessary.

  这样以后我们会在这个目录下生成一个目录叫sdomain

  进入这里面我们看看

  [root@ns ext]# cd sdomain/

  [root@ns sdomain]# ls

  config.m4 EXPERIMENTAL     sdomain.php  tests

  CREDITS  sdomain.c php_sdomain.h

  然后我们要修改文件顺序是

  configue.m4

  sdomain.c

  php_sdomain.h

  使用文本编辑器打开config.m4文件,文件内容大致如下:

  dnl $Id$d

  dnl config.m4 for extension my_module

  dnl dont forget to call PHP_EXTENSION(my_module)

  dnl Comments in this file start with the string dnl.

  dnl Remove where necessary. This file will not work

  dnl without editing.

dnl If your extension references something external, use with:

  dnl PHP_ARG_WITH(my_module, for my_module support,

  dnl Make sure that the comment is aligned:

  dnl [ --with-my_module       Include my_module support])

  dnl Otherwise use enable:

  dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

  dnl Make sure that the comment is aligned:

  dnl [ --enable-my_module      Enable my_module support])

  if test $PHP_MY_MODULE != no; then

  dnl If you will not be testing anything external, like existence of

  dnl headers, libraries or functions in them, just uncomment the

  dnl following line and you are ready to go.

  dnl Write more examples of tests here...

  PHP_EXTENSION(my_module, $ext_shared)

  Fi

  根据你自己的选择将

  dnl PHP_ARG_WITH(my_module, for my_module support,

  dnl Make sure that the comment is aligned:

  dnl [ --with-my_module       Include my_module support])

  修改成

  PHP_ARG_WITH(my_module, for my_module support,

  [ --with-my_module       Include my_module support])

  或者将

  dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

  dnl Make sure that the comment is aligned:

  dnl [ --enable-my_module      Enable my_module support])

修改成

  PHP_ARG_ENABLE(my_module, whether to enable my_module support,

  [ --enable-my_module      Enable my_module support])

        (其实就是将这部分的dnl去掉,在这个文件里dnl就是注释的意思,相当于我们PHP里面的#或// 另外把他中间的一句描术也去掉)

  我这里用了后者

  然后保存退出

  然后在编辑

  Vi sdomain.c

  将文件其中的下列代码进行修改

/* Every user visible function must have an entry in my_module_functions[].
*/
function_entry my_module_functions[] = {
    PHP_FE(say_hello,    NULL) /* ?添加着一行代码 */
    PHP_FE(confirm_my_module_compiled,   NULL) /* For testing, remove later. */
    {NULL, NULL, NULL}   /* Must be the last line in my_module_functions[] */
};

  在文件的最后添加下列代码

PHP_FUNCTION(say_hello)
{
    zend_printf("hello sdomain!");
}

再修改:php_sdomain.h

vi php_sdomain.h

在PHP_FUNCTION(confirm_my_module_compiled ); /* For testing, remove later. */ 这行的下面添加一行:

PHP_FUNCTION(say_hello); /* For testing, remove later. */

  保存文件退出

  然后我们就可以在这个目录下使用上面的命令了

  /usr/local/php/bin/phpize
#############################################################
原创粉丝点击