eAccelerator API 详解与应用实践
利用 eAccelerator 提升 PHP 代码执行效率
eAccelerator 是一款优秀的工具,其前身名为 truck-mmcache。
简而言之,这是一套专为 PHP(支持 PHP5)设计的缓存系统,采用共享内存或磁盘文件的方式进行数据交换。
其广泛应用源于对 PHP 源码的“编码”(常被误称为“加密”)以及对 PHP 执行中间码的缓存,从而显著提升速度。关于 eAccelerator 的安装配置已有大量详尽文章,此处旨在推荐将其用于辅助程序设计中的缓存功能,提供以下一组 API:
这是一种便捷且稳定的原生缓存实现方式。目前该部分设计似乎仅支持共享内存,因此仅适用于 Unix-Like 操作系统,Windows 平台暂不支持此特性。
1. eaccelerator_put($key, $value, $ttl=0)
将 $value 以 $key 作为键名存入缓存。在 PHP4 环境下支持对象类型存储(据源码分析,Zend2 可能不再支持)。$ttl 参数定义缓存的生命周期,单位为秒;若省略该参数或设为 0,则表示缓存永不过期,直至服务器重启被清空。
2. eaccelerator_get($key)
依据 $key 从缓存中检索由 eaccelerator_put() 存入的数据。若该缓存项已过期或不存在,则返回 NULL。
3. eaccelerator_rm($key)
根据指定的 $key 移除对应的缓存内容。
4. eaccelerator_gc()
执行垃圾回收,清除所有已过期的 key。
5. eaccelerator_lock($key)
为 $key 添加锁定操作,确保在多进程或多线程环境下数据同步。需调用 eaccelerator_unlock($key) 手动释放锁,或在程序请求结束时自动释放。
示例如下:
<?php
eaccelerator_lock("count");
eaccelerator_put("count",eaccelerator_get("count")+1));
?> 6. eaccelerator_unlock($key)
依据 $key 释放之前获取的锁。
7. eaccelerator_cache_output($key, $eval_code, $ttl=0)
将 $eval_code 代码的输出结果缓存 $ttl 秒($ttl 参数用法同 eacclerator_put)。
例如:
<?php eaccelerator_cache_output('test', 'echo time(); phpinfo();', 30); ?> 8. eaccelerator_cache_result($key, $eval_code, $ttl=0)
将 $eval_code 代码的执行结果缓存 $ttl 秒($ttl 参数用法同 eacclerator_put),功能类似于 cache_output。
例如:
<?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?> 9. eaccelerator_cache_page($key, $ttl=0)
将当前整页内容缓存 $ttl 秒。
例如:
<?php
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echo time();
phpinfo();
?> 10. eaccelerator_rm_page($key)
删除由 eaccelerator_cache_page() 生成的缓存,参数同样为 $key。
______________________________________________
(以下为一个简单示例以展示其效果,注意在 CLI 模式下可能无效!)
<?php
class test_cache {
var $pro = 'hello';
function test_cache() {
echo "Object Created!
\n";
}
function func() {
echo ', the world!';
}
function now($t) {
echo date('Y-m-d H:i:s', $t);
}
}
$tt = eaccelerator_get("test_tt");
if (!$tt)
{
$tt = new test_cache;
eaccelerator_put("test_tt", $tt);
echo "no cached!
\n";
}
else {
echo "cached
\n";
}
echo $tt->pro;
$tt->func();
$tt->now(time() + 86400);
?> 以下是网友的评论:
--------------------------------------------------------------------------------
showsa 回复于:2005-12-31 19:51:56 win 也支持! http://www.arnot.info/eaccelerator/
信天翁 回复于:2006-01-04 17:17:37 最新版 eAccelerator 0.9.4-rc1 中存在一个小 bug,表现为 /var/log/httpd/error_log 出现大量 [warn] (32)Broken pipe: write pipe_of_death 的错误信息。解决方法是修改 debug.c 文件: ----------------------------------------------- /** * Close the debug system. */ void ea_debug_shutdown () { fflush (F_fp); // 源语句, 关闭文件时没有检测文件句柄 //fclose (F_fp); //改为 if (F_fp != stderr) fclose (F_fp); F_fp = NULL; }
soichiro 回复于:2006-01-10 22:01:21 eAccelerator/truck-mmcache 太恐怖了,我现在有两个负载很高的系统,一个基于 Drupal,另一个基于 PostNuke,用了 eAccelerator 后,Drual 速度提升 100 倍,PostNuke 提升 10 倍,PostNuke 提升比较少是因为它本身就很快。
wangyih 回复于:2006-04-08 10:48:11 和使用 squid 比怎么样
showsa 回复于:2006-04-08 23:23:44 怎么去和 squid 去比啊 不一样的东西 squid 是缓存页面运行结果 如果不是实时显示,squid 肯定强了 但是论坛之类的,squid 就不行了,用 eaccelerator /memcache 可以很大程度上提升效率
Yarco 回复于:2006-04-09 10:00:43 但是据说和 encode 过的代码有冲突啊... 不知道现在的和 zend 的兼容性如何?