MMCACHE最新版已经与PHP5严重不兼容。我看了看PHP5的源代码,意外发现一个SHM模块--shmop。
琢磨它的说明文档,我写了一个不成熟的函数:
/*author:[email protected]
我个人用这个函数来显示首页(反正是CMS内容管理系统,不需要时时刻刻更新,将$time设置成1200秒足够了),查看栏目、查看具体某一条信息,我用jpcache来缓存
*/
function getcontent($time) { $shm_id = shmop_open(0xff4, "c", 0644, 20); if($shm_id){ $shm_size = shmop_size($shm_id); $mydate = shmop_read($shm_id, 0, $shm_size); shmop_close($shm_id); $date=date("YmdHis"); if($date-$mydate>=$time){ $date=date("YmdHis"); $shm_id = shmop_open(0xff4, "c", 0644, 20); $shm_bytes_written = shmop_write($shm_id, $date, 0); shmop_close($shm_id); ob_start(); maincontent(); $data = ob_get_contents(); $shm_id = shmop_open(0xff3, "c", 0644, strlen($data)); $shm_bytes_written = shmop_write($shm_id, $data, 0); shmop_close($shm_id); echo $data; } else{ $shm_id = shmop_open(0xff3, "a", 0644, 150000); $shm_size = shmop_size($shm_id); $my_string = shmop_read($shm_id, 0, $shm_size); shmop_close($shm_id); echo $my_string; } } else{ $date=date("YmdHis"); $shm_id = shmop_open(0xff4, "c", 0644, 20); $shm_bytes_written = shmop_write($shm_id, $date, 0); shmop_close($shm_id); ob_start(); maincontent(); $data = ob_get_contents(); $shm_id = shmop_open(0xff3, "c", 0644, strlen($data)); $shm_bytes_written = shmop_write($shm_id, $data, 0); shmop_close($shm_id); echo $data; } }
其中maincontent()是一个函数,就是需要加速的页面的内容,比如:
function maincontent() { /*这个函数里面可以为任意内容。将需要加速的代码直接写在这里面即可。*/ $smarty = new Smarty; $smarty->display('title.tpl'); $smarty->display('index.tpl'); $smarty->display('footer.tpl'); }
测试结果比较:
没用这个加速代码之前,用ab -n2000 -c64 -dS http://localhost/得出的结果是:
Transfer rate: 33.25 [Kbytes/sec]
使用这个加速代码:
Transfer rate: 6382.55 [Kbytes/sec]
现在的速度是以前的6382.55/33.25=191.96倍!
函数说明:getcontent($time)中的$time为缓存时间;shmop为内存存取模块,0xff3和0xff4是我自己随意取的key。我使用的平台:win2000+apache2.1-dev+PHP5+MySQL。PHP中的具体说明:http://www.php.net/manual/zh/ref.shmop.php
没有MMCACHE一样的加速PHP5!我自己很满意。代码没有优化,有意者可以自己修改。毕竟opensource嘛。哈哈。

|