使用 eAccelerator加速PHP代码的方法

前端之家收集整理的这篇文章主要介绍了使用 eAccelerator加速PHP代码的方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

eAccelerator真是一个好东西(它的前身是truck-mmcache)。 简单来讲它是一套配合PHP支持PHP5)运作的缓存系统,通过共享内存或磁盘文件方式交换数据。 它被广为使用的是PHP源码“编码”(不太贴切的称为“加密”)和缓存PHP执行的中间码以加速。关于eA的安装使用的文章已经很多而且也很详细了,这次我想推荐的是用它辅助程序设计缓存,它提供了一组API如下: 是一个非常便捷而又稳定的本机缓存实现方式,目前这部分设计似乎只支持于共享内存,所以只能用于Unix-LikeOS了,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)
ForExample:
<?phpeaccelerator_cache_output('test','echotime();phpinfo();',30);?> 8.eaccelerator_cache_result($key,$ttl=0)
将$eval_code代码的执行结果缓存$ttl秒,($ttl参数同eacclerator_put),类似cache_output
ForExample:
<?phpeaccelerator_cache_result('test','time()."Hello";',30);?> 9.eaccelerator_cache_page($key,$ttl=0)
将当前整页缓存$ttl秒。
ForExample:
<?PHP
eaccelerator_cache_page($_SERVER['PHP_SELF'].'?GET='.serialize($_GET),30);
echotime();
PHPinfo();
?> 10.eaccelerator_rm_page($key)
删除由eaccelerator_cache_page()执行的缓存,参数也是$key __
(作个简单例子看看它的威力,注意在cli模式下可能无效!)
<?PHPclasstest_cache{var$pro='hello';functiontest_cache(){echo"ObjectCreated!
/n";}functionfunc(){echo',theworld!';}functionnow($t){echodate('Y-m-dH:i:s',$t);}}$tt=eaccelerator_get("test_tt");if(!$tt){$tt=newtest_cache;eaccelerator_put("test_tt",$tt);echo"nocached!
/n";}else{echo"cached
/n";}echo$tt->pro;$tt->func();$tt->now(time()+86400);?>

原文链接:https://www.f2er.com/php/29837.html

猜你在找的PHP相关文章