教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 eAccelerator加速PHP代码方法解析

eAccelerator加速PHP代码方法解析

发布时间:2018-05-25   编辑:jiaochengji.com
本文介绍了php编程中,使用 eAccelerator加速PHP代码的方法,有需要的朋友参考学习下。

eAccelerator 真是一个好东西(它的前身是truck-mmcache)。 
它是一套配合PHP(支持PHP5)运作的缓存系统,通过共享内存或磁盘文件方式交换数据。 
它被广为使用的是PHP源码“编码”(不太贴切的称为“加密”)和缓存PHP执行的中间码以加速。关于 eA 的安装使用的文章已经很多而且也很详细了,这次我想推荐的是用它辅助程序设计缓存,它提供了一组API如下: 
是一个非常便捷而又稳定的本机缓存实现方式,目前这部分设计似乎只支持于共享内存,所以只能用于 Unix -Like OS 了,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) 
   For Example:  
 

复制代码 代码示例:
  <?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 
   For Example:  
 

复制代码 代码示例:
  <?php eaccelerator_cache_result('test', ' time() . "Hello";', 30); ?> 

 9. eaccelerator_cache_page($key, $ttl=0) 
  将当前整页缓存 $ttl 秒。 
  For Example:  
 

复制代码 代码示例:
  <?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!<br>\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!<br>\n";
}
else {
  echo "cached<br>\n";
}
echo $tt->pro; 
$tt->func();
$tt->now(time() + 86400);
?>

您可能感兴趣的文章:
php加速工具eAccelerator 配置参数详解
PHP加速器eAccelerator配置使用指南
安装及配置eaccelerator-0.9.5加速PHP-5.2.1
又装了一次~eAccelerator [安装步聚]
如何使用eAccelerator加密PHP程序
php加速器eAccelerator配置参数详解
eAccelerator加速PHP代码方法解析
php 启动时报错的解决方法
PHP中eAccelerator API开发详解
PHP has encountered an Access Violation at 解决办法

关键词: eaccelerator  php   
[关闭]
~ ~