清单 7. 创建、缓存和检索 PHP 对象 <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

ball

Object added to cache

Object retrieved from cache

ball

Array

(

[72.51.41.164:11211] => Array

(

[pid] => 865

[uptime] => 3812845

[time] => 1173817644

[version] => 1.1.12

[rusage_user] => 0.043993

[rusage_system] => 0.038994

[curr_items] => 1

[total_items] => 5

[bytes] => 145

[curr_connections] => 1

[total_connections] => 8

[connection_structures] => 3

[cmd_get] => 5

[cmd_set] => 5

[get_hits] => 5

[get_misses] => 0

[bytes_read] => 683

[bytes_written] => 1098

[limit_maxbytes] => 67108864

)

)

十分简单,不是?如果 myDatum 的构造函数是典型的,则它将很可能被给定一个 ID 并将查询数据库以生成特定行(例如,查找社会安全号码为 123-45-6789 的学生)。您可以扩展构造函数以便在缓存中首先查找 ID。如果找到,则只需返回该对象。否则,构造对象,缓存并返回该对象。

   
如果有一组 Debian Linux 系统,则可以复制或导出(通过 NFS/opt/lampp 并在多个系统中运行 memcached。在两台或多台计算机中同时运行 memcached 将删除单点故障并扩展缓存的容量。使用 addServer() API 函数来构建一列可用的 memcached 服务器。

我希望速度更快!

   
使用 Memcache PHP API 十分简单,并且 memcached 十分易于部署。可能大部分工作就是(重新)构建 PHP 本身以包括适当的扩展。的确,如果用于构造对象的方法已被良好隔离,则修改代码以利用 Memcache 应当是小事一桩。

   
正如您已经看到的,一些简单的技术和一点点时间和努力就可以提高 PHP 应用程序性能。在购买更多 RAM 或另一台服务器之前,请先尝试调整现有服务器。那样做更便宜!

打开 configure-php4-oswald 并添加 --enable-memcache(如果系统没有那些数据库,您还可能发现有必要删除选项,例如特定于 Oracle PostgreSQL 的那些选项)。清单 2 展示了在测试系统上用于重新构建 PHP 的修改后的脚本。(PHP 构建过程依赖于许多实用程序和开发库,例如 FlexBisonlibxml PCRE。您可能需要安装附加软件包才能准备好进行这些构建,这取决于 Linux 发行版和 PHP 配置的内容)。

清单 2. 用于重新构建 PHP 的修改后的 XAMPP 脚本

(

cd /opt/lampp/bin

rm phpize phpextdist php-config php

rm -rf /opt/lampp/include/php

)

 

make distclean

 

export PATH="/opt/lampp/bin:$PATH"

export CFLAGS="-O6 -I/opt/lampp/include/libpng \

-I/opt/lampp/include/ncurses \

-I/opt/lampp/include -L/opt/lampp/lib"

 

./configure \

--prefix=/opt/lampp \

--with-apxs2=/opt/lampp/bin/apxs \

--with-config-file-path=/opt/lampp/etc \

--with-mysql=/opt/lampp \

--enable-inline-optimation \

--disable-debug \

--enable-memcache \

--enable-bcmath \

--enable-calendar \

--enable-ctype \

--enable-dbase \

--enable-discard-path \

--enable-exif \

--enable-filepro \

--enable-force-cgi-redirect \

--enable-ftp \

--enable-gd-imgstrttf \

--enable-gd-native-ttf \

--with-ttf \

--enable-magic-quotes \

--enable-memory-limit \

--enable-shmop \

--enable-sigchild \

--enable-sysvsem \

--enable-sysvshm \

--enable-track-vars \

--enable-trans-sid \

--enable-wddx \

--enable-yp \

--with-ftp \

--with-gdbm=/opt/lampp \

--with-jpeg-dir=/opt/lampp \

--with-png-dir=/opt/lampp \

--with-tiff-dir=/opt/lampp \

--with-freetype-dir=/opt/lampp \

--without-xpm \

--with-zlib=yes \

--with-zlib-dir=/opt/lampp \

--with-openssl=/opt/lampp \

--with-expat-dir=/opt/lampp \

--enable-xslt \

--with-xslt-sablot=/opt/lampp \

--with-dom=/opt/lampp \

--with-ldap=/opt/lampp \

--with-ncurses=/opt/lampp \

--with-gd \

--with-imap-dir=/opt/lampp \

--with-imap-ssl \

--with-imap=/opt/lampp \

--with-gettext=/opt/lampp \

--with-mssql=/opt/lampp \

--with-mysql-sock=/opt/lampp/var/mysql/mysql.sock \

--with-mcrypt=/opt/lampp \

--with-mhash=/opt/lampp \

--enable-sockets \

--enable-mbstring=all \

--with-curl=/opt/lampp \

--enable-mbregex \

--enable-zend-multibyte \

--enable-exif \

--enable-pcntl \

--with-mime-magic \

--with-iconv

 

make

 

sudo make install

 

exit 1

1

在脚本的末尾,您的 XAMPP 安装将有一个新的独立运行的能够使用 memcache PHP V4 副本。如果需要测试构建,请停止所有正在运行的 Apache MySQL 副本,包括在 XAMPP 外部的那些副本,并且运行以下命令:

$ sudo /opt/lampp/lampp start

    这将启动 XAMPP 版本的 Apache MySQL,包括新的 PHP V4 模块。如果需要使 Apache 生产服务器的运行不受干扰,则可以编辑文件 /opt/lampp/etc/httpd.conf 并把 Listen 端口参数改为 8080(或其他可用端口)。然后您可以用以下命令单独启动 XAMPP Apache 服务器:

sudo /opt/lampp/bin/apachectl start

    把浏览器指向 http://localhost,然后您应当会看到类似于图 1 的内容。

1. XAMPP for Linux 初始页面:检验 XAMPP PHP 是否已成功安装