使用eAccelerator加速PHP代码的方法
2023-06-13 09:13:58 时间
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 模式下可能无效!)
<?phpclass 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(支持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 模式下可能无效!)
<?phpclass 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 中js跳转页面跳转页面,js跳转代码_PHP页面跳转 Js页面跳转代码[通俗易懂]
- PHP对接chatGPT代码,openai接口对接,php 实现chatGPT功能
- Windows环境下php开启GD库的方法详解编程语言
- PHP操作MySQL:实现数据库写入操作(php写入mysql)
- php Yaf_Loader::import引入文件报错的解决方法详解编程语言
- PHP过滤换行的方法详解编程语言
- PHP专业连接MySQL:实现快速代码.(php连接mysql数据库代码)
- Linux下构建高性能PHP环境(linux下的php)
- PHP与MySQL结合使用实现关联查询的方法(php关联mysql查询)
- 使用 PHP 查询 MySQL 数据库的方法详解(php查询mysql数据)
- PHP实现Redis监控的简便方法(php监控redis)
- 快速学习PHP Redis的使用方法(php redis怎么用)
- 使用PHP编辑器轻松编写Linux平台代码(php编辑器linux)
- PHP管理MSSQL的方法探索(Php管理mssql)
- PHP连接MSSQL实现注册功能(php注册写入mssql)
- PHP如何支持使用MS SQL 进行数据库操作(php怎么还用mssql)
- MySQL和PHP:构建更先进的开发体系(mysql 和php)
- 使用PHP实现Redis自旋锁(redis自旋锁php)
- 网站加速PHP缓冲的免费实现方法
- PHP文件上传进度条的两种实现方法的代码
- IIS6中配置PHP的方法200911
- PHP安全技术之实现php基本安全
- PHP执行zip与rar解压缩方法实现代码
- php做下载文件的实现代码及文件名中乱码解决方法
- php下将多个数组合并成一个数组的方法与实例代码
- 不重新编译PHP为php增加openssl模块的方法
- PHP新手NOTICE错误常见解决方法
- PHP输入流php://input介绍
- PHP学习笔记IIS7下安装配置php环境
- php另类上传图片的方法(PHP用Socket上传图片)
- CI框架Session.php源码分析