Cache_Lite使用方法详解
Cache_Lite使用方法详解
目录
介绍 – 介绍 Cache_Lite
constructor Cache_Lite::Cache_Lite() – 构造函数
Cache_Lite::get() – 测试cache是否存在 并(如果是) 返回它
Cache_Lite::save() – 保存数据到一个cache 文件
Cache_Lite::remove() – 删除一个cache文件
Cache_Lite::clean() – 清除cache
Cache_Lite::setToDebug() –设置为调试模式
Cache_Lite::setLifeTime() – 设置新的生命周期
Cache_Lite::saveMemoryCachingState() --
Cache_Lite::getMemoryCachingState() --
Cache_Lite::lastModified() – 返回cache最后更新时间。
Cache_Lite::raiseError() – 触发一个 PEAR 错误
constructor Cache_Lite_Output::Cache_Lite_Output() -- 构造函数
Cache_Lite_Output::start() – 测试是否一个cache有效 并(如果有效)返回它输出到浏览器. 否则,激活输出缓冲.
Cache_Lite_Output::end() – 停止由start()方法开始的输出缓冲并且保存输出到一个cache文件
constructor Cache_Lite_Function::Cache_Lite_Function() -- 构造函数
Cache_Lite_Function::call() – 调用一个可缓冲的函数或方法 (如果已经有个一个cache则不进行调用)
Cache_Lite提供一个快速、轻量级、安全的 cache 系统. 它对文件容器进行了优化并且可以防止cache的讹误(因为它使用了文件锁 和/或 hash测试).
介绍
介绍 – 介绍 Cache_Lite
描述
PEAR::Cache_Lite是一个小型的cache 系统. 它对高流量的网站进行优化所以它真正的快洁和安全 (因为它使用了文件锁 和/或 反讹误测试).
目标和技术细节
速度
首先, PEAR::Cache_Lite 一定要非常的快. 这使得通过在网站利用PEAR可以在不用高端的硬件解决方案的情况下承担高访问量.
在这个文档里,你可以发现关于cache_lite技术选择的更多的细节. 但是包含PEAR.php的主要目的只是用它的错误处理机制(非常不错).
简单
因为cache系统经常被嵌入在应用层,PEAR::Cache_Lite内核必须很小并且灵活 并有一个合适的licence (LGPL). 高级的应用可以在扩展内核的文件中实现.
安全
在高访问量的网站, cache 系统一定要发防止cache 文件讹误 (因为在读/写模式下会有竞争访问的问题). 很少有cache 系统提供关于这个问题的保护措施
文件锁并不是一个完美的解决方案因为它不能用于NFS或多线程的服务器上。所以除它之外,PEAR::Cache_Lite 提供两种完全透明的机制(基于hash key)来保证数据在所有情况下的安全。要得到更多的细节,可以参考上一章所给的链接。
用法
通常用法
Cache_Lite的所用模块遵循相同的体系。
参数(与缺省的不同的部分)使用联合数租来传递到构造函数
一个cache文件由一个cache ID来定义 (最后的一个组).由于显而易见的灵活性的原因, ID的选择留给开发这决定。
内核
让我们由一个简单的例子开始: 页面构建然后用一个唯一的变量(字符串)恢复:
// 包含package require_once('Cache/Lite.php'); // 为这个cache 设置一个id $id = '123'; // 设置一些参数 $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 3600 ); // 创建一个 Cache_Lite 对象 $Cache_Lite = new Cache_Lite($options); // 测试是否为一个有效的id if ($data = $Cache_Lite->get($id)) { // 找到 Cache ! // 内容在 $data 中 // (...) } else { // 没有发现有效的cache (你需要创建页面) // Cache 丢失 ! // 把数据放到 $data 中 并cache他 // (...) $Cache_Lite->save($data); } ?> |
如果你希望每一区块使用一个cache而不是用一个全局cache, 把下面的脚本作为例子:
require_once('Cache/Lite.php'); $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 3600 ); // 创建一个 Cache_Lite 对象 $Cache_Lite = new Cache_Lite($options); if ($data = $Cache_Lite->get('block1')) { echo($data); } else { $data = 'Data of the block 1'; $Cache_Lite->save($data); }
if ($data = $Cache_Lite->get('block2')) { echo($data); } else { $data = 'Data of the block 2'; $Cache_Lite->save($data); } ?> |
内核
尽管如此,不是总是可以用一个字符串变量来恢复一个页面的所有内容。因此 要用Cache_Lite_Output来帮我们的忙 :
require_once('Cache/Lite/Output.php'); $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 10 ); $cache = new Cache_Lite_Output($options); if (!($cache->start('123'))) { // Cache 丢失 for($i=0;$i<1000;$i++) { //制作页面... echo('0123456789'); } $cache->end(); } ?> |
概念和单独区块的用法相同:
require_once('Cache/Lite/Output.php'); $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 10 ); $cache = new Cache_Lite_Output($options); if (!($cache->start('block1'))) { // Cache 丢失... echo('Data of the block 1 !'); $cache->end(); } echo('Non cached line !'); if (!($cache->start('block2'))) { // Cache 丢失... echo('Data of the block 2 !'); $cache->end(); } ?> |
非常重要的评论
为了将Cache_Lite发挥到最大的功效, 在你的页面中不要包含系统的或其他的packages. 当页面没有cache时(且一定要重算),通过使用条件包含,只载入你需要的模块.
错误的方法 :
require_once("Cache/Lite.php"); require_once("...") require_once("...") // (...) $cache = new Cache_Lite(); if ($data = $Cache_Lite->get($id)) { // 发现cache ! echo($data); } else { // 页面必须重新构造到 $data 中 // (...) $Cache_Lite->save($data); } ?> |
这是正确的方法(通常会快两倍多) :
require_once("Cache/Lite.php"); // (...) $cache = new Cache_Lite(); if ($data = $Cache_Lite->get($id)) { // cache hit ! echo($data); } else { //页面必须重新构造到 $data 中 require_once("...") require_once("...") // (...) $Cache_Lite->save($data); } ?> |
结论
要更大效率的使用 Cache_Lite, 请看例子和包中所给的技术细节。.
constructorCache_Lite::Cache_Lite()
constructor Cache_Lite::Cache_Lite() – 构造函数
大纲
require_once 'Lite.php'; |
void constructor Cache_Lite::Cache_Lite
([array $options = array(NULL)])
描述
Cache_Lite核心类的构造函数. 你可以提供一个关联数组作为参数来设置大量选项。
参数
array $options
关联数组可以设置大量的选项
表 23-1.
选项 | 数据类型 | 缺省值 | 描述 |
cacheDir | string | /tmp/ | 放置cache 文件的目录 (结尾要加上’/’) |
caching | boolean | TRUE | 启用 / 禁用 caching |
lifeTime | integer | 3600 | Cache以秒为单位的生命周期 |
fileLocking | boolean | TRUE | 启用 / 禁用 文件锁定 (可以在不利情况下避免cache讹误) |
writeControl | boolean | TRUE | 启用 / 禁用 写入控制 (启用写入控制会轻微的减慢cache写入的速度但不会影响读取, 写入控制可以检测一些讹误的cache文件,但也许他并不是最完美的控制) |
readControl | boolean | TRUE | 启用 / 禁用 读取控制(如果启用, 一个控制键会被嵌入到cache文件中,并且这个键将会和读取文件后计算出那个进行比较) |
readControlType | string | crc32 | 读取控制的类型(只有当readControl启用时). 应当为 'md5' (用一个 md5 hash控制(最好但最慢)), 'crc32' (用一个crc32 hash (更快但安全性稍低于md5)) 或'strlen' (只用一个长度测试 (最快)) |
pearErrorMode | integer | CACHE_LITE_ERROR_RETURN | pear 错误模式 (当调用raiseError时) (使用CACHE_LITE_ERROR_RETURN 只返回一个 PEAR_Error对象 或 CACHE_LITE_ERROR_DIE用来立即停止脚本(最好用于debug)) |
fileNameProtection | boolean | TRUE | 文件名保护 (如果设为 true, 你可以使用任何cache Id 或组名, 如果设为false, 它会更快 , 但是 cache id和组名将直接应用到 cache文件的文件名,所以要小心使用特殊字符...) |
automaticSerialization | boolean | FALSE | 启用 / 禁用 自动序列化 (它用于直接存储不为字符串的数据,但是它会比较慢) |
memoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Memory Caching" (NB : memory caching没有生命周期, 只到脚本结束时为止) |
onlyMemoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Only Memory Caching" (如果启用, 将不再使用文件) |
memoryCachingLimit | integer | 1000 | [BETA QUALITY] 存储在内存缓冲中的纪录的最大数量 |
automaticCleaningFactor | integer | 0 | [since 1.4.0beta1] 启用 / 禁用自动清除进程. 当一个新的cache文件写入的时候,自动清除进程销毁太旧的 (用一个给定的生命周期) cache 文件. 0 代表 "没有cache自动清除", 1 代表 "系统的 cache 清除" (slow), x>1 意味着 "当x 次cache 写入时。随机自动 清除 1 次". 一个20到200之间的值也许是好的开始. |
hashedDirectoryLevel | integer | 0 | [since 1.4.0beta1] 设置hash目录结构分层. 0 代表 "没有hash目录结构", 1 代表 "使用一层目录", 2 代表 "两层"... 只有在有很多的cache文件时。这个选项才可以加速cache_lite。只有进行具体的测试才可以帮助你找到适合的值. 也许, 1 或 2 是一个好的开始. |
抛出
throws 没有异常抛出
Note
这个函数不能静态的调用.
例子
例子 23-1. 使用最普通的选项 require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE); $cache = Cache_Lite($options); ?> |
Cache_Lite::get()
Cache_Lite::get() -- 测试cache是否存在 并(如果是) 返回它
大纲
require_once 'Lite.php'; |
string Cache_Lite::get
(string $id [, string $group = 'default'[, boolean $doNotTestCacheValidity =
FALSE]])
描述
Cache_Lite的主要方法之一 : 测试 cache 文件的有效性,如果有效的话返回它(FALSE else)
参数
string $id
cache id
string $group
cache组的名称
boolean $doNotTestCacheValidity
如果设置为TRUE, cache有效性将不进行测试
返回值
returns cache的数据 (或 false当无效时)
Note
这个函数不能静态的调用.
例子
例子 23-1. 典型用法 require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE);$cache = Cache_Lite($options); if ($data = $cache->get('id_of_the_page')) { // 发现cache ! // 在 $data 中的内容 // (...) } else { // 没有发现有效 cache (你需要制作并保存页面) // (...) } ?> |
Cache_Lite::save()
Cache_Lite::save() – 保存数据到cache文件中
大纲
require_once 'Lite.php'; |
boolean Cache_Lite::save
(string $data [, string $id = NULL [,string $group = 'default']])
描述
保存给定数据 (如果automaticSerialization设置为FALSE (缺省情况)必须是一个string类型).
参数
string $data
放进cache文件的数据( 如果automaticSerialization设置为 FALSE (缺省情况) ,不可以使用string 外的其他类型).
string$id
cache id
string $group
cache组的名称
返回值
returns 没问题的话返回true
Note
这个函数不能静态的调用.
例子
例子 23-1. 典型用法 require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE);$cache = Cache_Lite($options); if ($data = $cache->get('id_of_the_page')) { // 发现Cache ! // 内容在$data echo($data); } else { //没有发现有效 cache (你需要制作并保存页面) $data = 'this is a test'; $cache->save($data); } ?> |
Cache_Lite::remove()
Cache_Lite::remove() – 删除一个cache文件
大纲
require_once 'Lite.php'; |
boolean Cache_Lite::remove
(string $id [, string $group = 'default'])
描述
删除一个cache文件(用它的id和组指定)
参数
string $id
cache id
string $group
cache组的名称
返回值
returns 没问题的话返回true
Note
这个函数不能静态的调用.
例子
例子 23-1. 典型用法 require_once "Cache/Lite.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE);$cache = new Cache_Lite($options); $cache->remove('id_of_the_page'); if ($data = $cache->get('id_of_the_page')) { // 发现Cache ! // [不可能的 !] } else { // 没有发现有效 cache (你需要制作并保存页面) $data = 'this is a test'; $cache->save($data); } ?> |
这是一个虚拟的例子因为在脚本的开始cache已经被销毁了! So the first case ofthe if statement is impossible.
Cache_Lite::clean()
Cache_Lite::clean() – 清除cache
大纲
require_once 'Lite.php'; |
boolean Cache_Lite::clean
([string $group =
FALSE])
描述
如果没指定组就清除所有的cache文件; 否则只有指定的组被清除。
参数
string $group
cache组的名称
返回值
returns没问题的话返回true
Note
这个函数不能静态的调用..
例子
例子 23-1. 典型用法 require_once "Cache/Lite.php"; $options = array('cacheDir' => '/tmp/');$cache = Cache_Lite($options); $cache->clean(); ?> |
这个例子清除所有cache 文件.
Cache_Lite::setToDebug()
Cache_Lite::setToDebug() – 设置为调试模式
Synopsis
require_once 'Lite.php'; |
void Cache_Lite::setToDebug
()
Description
当发现错误时,脚本将停止并显示出错误信息 (只有在调试模式) ; 和在构造函数中用pearErrorMode 选项一个效果
Note
这个函数不能静态的调用.
Cache_Lite::setLifeTime()
Cache_Lite::setLifeTime() -- 设置新的生命周期
大纲
require_once 'Lite.php'; |
void Cache_Lite::setLifeTime
(int $newLifeTime)
描述
改变生命周期
参数
integer $newLifeTime
新的生命周期 (以秒为单位)
抛出
throws 没有异常抛出
Note
这个函数不能静态的调用.
Cache_Lite::saveMemoryCachingState()
Cache_Lite::saveMemoryCachingState() --
大纲
require_once 'Lite.php'; |
void Cache_Lite::saveMemoryCachingState
(string $id [, string $group = 'default'])
描述
[BETAQUALITY] 保存内存缓冲数组的当前状态到经典的cache文件
描述
string $id
cache id
string $group
cache组的名称
Note
这个函数不能静态的调用.
Cache_Lite::getMemoryCachingState()
Cache_Lite::getMemoryCachingState() --
大纲
require_once 'Lite.php'; |
void Cache_Lite::getMemoryCachingState
(string $id [, string $group = 'default'[, bool $doNotTestCacheValidity =
FALSE]])
Description
[BETAQUALITY] 从经典的cache文件中载入以前保存的内存缓冲数组的状态
Parameter
string $id
cache id
string $group
cache组的名称
boolean $doNotTestCacheValidity
如果设为TRUE, cache有效性将不进行测试
Note
这个函数不能静态的调用.
Cache_Lite::lastModified()
Cache_Lite::lastModified() -- Returnthe cache last modification time
大纲
require_once 'Lite.php'; |
int Cache_Lite::lastModified
()
描述
[ONLYFOR CACHE_LITE HACKERS] 返回cache最后修改时间
返回值
returns 最后修改时间
Note
这个函数不能静态的调用.
Cache_Lite::raiseError()
Cache_Lite::raiseError() -- Triggera PEAR error
大纲
require_once 'Lite.php'; |
void Cache_Lite::raiseError
(string $msg, int $code)
描述
继承PEAR.php 文件的用法 ,触发 PEAR 错误
为了增加性能,PEAR.php是被动态的include的。所以它只有当有错误触发时才会被include。在大多数情况下,不include它会得到更好的性能。
Parameter
string $msg
错误信息
integer $code
错误码
Note
这个函数不能静态的调用.
constructorCache_Lite_Output::Cache_Lite_Output()
constructor Cache_Lite_Output::Cache_Lite_Output() – 构造函数
大纲
require_once 'LiteOutput.php'; |
void constructor Cache_Lite_Output::Cache_Lite_Output
(array $options)
描述
Cache_Lite_OUTPUT类的构造函数. 你可以提供一个关联数组作为参数来设置大量选项。
Parameter
array $options
关联数组可以设置大量的选项
Table23-1.
选项 | 数据类型 | 缺省值 | 描述 |
cacheDir | string | /tmp/ | 放置cache 文件的目录 (结尾要加上’/’) |
caching | boolean | TRUE | 启用 / 禁用 caching |
lifeTime | integer | 3600 | Cache以秒为单位的生命周期 |
fileLocking | boolean | TRUE | 启用 / 禁用 文件锁定 (可以在不利情况下避免cache讹误) |
writeControl | boolean | TRUE | 启用 / 禁用 写入控制 (启用写入控制会轻微的减慢cache写入的速度但不会影响读取, 写入控制可以检测一些讹误的cache文件,但也许他并不是最完美的控制) |
readControl | boolean | TRUE | 启用 / 禁用 读取控制(如果启用, 一个控制键会被嵌入到cache文件中,并且这个键将会和读取文件后计算出那个进行比较) |
readControlType | string | crc32 | 读取控制的类型(只有当readControl启用时). 应当为 'md5' (用一个 md5 hash控制(最好但最慢)), 'crc32' (用一个crc32 hash (更快但安全性稍低于md5)) 或'strlen' (只用一个长度测试 (最快)) |
pearErrorMode | integer | CACHE_LITE_ERROR_RETURN | pear 错误模式 (当调用raiseError时) (使用CACHE_LITE_ERROR_RETURN 只返回一个 PEAR_Error对象 或 CACHE_LITE_ERROR_DIE用来立即停止脚本(最好用于debug)) |
fileNameProtection | boolean | TRUE | 文件名保护 (如果设为 true, 你可以使用任何cache Id 或组名, 如果设为false, 它会更快 , 但是 cache id和组名将直接应用到 cache文件的文件名,所以要小心使用特殊字符...) |
automaticSerialization | boolean | FALSE | 启用 / 禁用 自动序列化 (它用于直接存储不为字符串的数据,但是它会比较慢) |
memoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Memory Caching" (NB : memory caching没有生命周期, 只到脚本结束时为止) |
onlyMemoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Only Memory Caching" (如果启用, 将不再使用文件) |
memoryCachingLimit | integer | 1000 | [BETA QUALITY] 存储在内存缓冲中的纪录的最大数量 |
Note
这个函数不能静态的调用.
Cache_Lite_Output::start()
Cache_Lite_Output::start() -- 测试是否一个cache有效 并(如果有效)返回它输出到浏览器. 否则, 输出激活输出缓冲.
大纲
require_once 'LiteOutput.php'; |
boolean Cache_Lite_Output::start
(string $id [, string $group = 'default'])
描述
测试是否一个cache有效 并(如果有效)返回它输出到浏览器. 否则, 输出激活输出缓冲.
Parameter
string $id
cache id
string $group
cache组的名称
返回值
returns 如果有cache返回true(否则false)
Note
这个函数不能静态的调用.
例子
例子 23-1. 典型用法 require_once "Cache/Lite/Output.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE);$cache = new Cache_Lite_Output($options); if (!($cache->start('id_of_the_page'))) { // 没有发现cache !// 直到遇到end()方法 所有的输出将被缓冲// (...) $cache->end(); } ?> |
Cache_Lite_Output::end()
Cache_Lite_Output::end() -- 停止由start()方法开始的输出缓冲并且保存输出到一个cache文件
大纲
require_once 'LiteOutput.php'; |
void Cache_Lite_Output::end
()
描述
停止由start()方法开始的输出缓冲并且保存输出到一个cache文件
Note
这个函数不能静态的调用.
例子
例子 23-1. 典型用法 require_once "Cache/Lite/Output.php"; $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 7200, 'pearErrorMode' => CACHE_LITE_ERROR_DIE);$cache = Cache_Lite_Output($options); if (!($cache->start('id_of_the_page'))) { // 没有发现Cache ! // 直到遇到end()方法 所有的输出将被缓冲 // (...) $cache->end(); // 缓冲的输出现在被存储到一个cache文件中 } ?> |
constructorCache_Lite_Function::Cache_Lite_Function()
constructor Cache_Lite_Function::Cache_Lite_Function() – 构造函数
大纲
require_once 'LiteFunction.php'; |
void constructor Cache_Lite_Function::Cache_Lite_Function
([array $options = array(NULL)])
描述
Cache_Lite_Function类的构造函数. 你可以提供一个关联数组作为参数来设置大量选项。
参数
array $options
选项
Table23-1.
选项 | 数据类型 | 缺省值 | 描述 |
cacheDir | string | /tmp/ | 放置cache 文件的目录 (结尾要加上’/’) |
caching | boolean | TRUE | 启用 / 禁用 caching |
lifeTime | integer | 3600 | Cache以秒为单位的生命周期 |
fileLocking | boolean | TRUE | 启用 / 禁用 文件锁定 (可以在不利情况下避免cache讹误) |
writeControl | boolean | TRUE | 启用 / 禁用 写入控制 (启用写入控制会轻微的减慢cache写入的速度但不会影响读取, 写入控制可以检测一些讹误的cache文件,但也许他并不是最完美的控制) |
readControl | boolean | TRUE | 启用 / 禁用 读取控制(如果启用, 一个控制键会被嵌入到cache文件中,并且这个键将会和读取文件后计算出那个进行比较) |
readControlType | string | crc32 | 读取控制的类型(只有当readControl启用时). 应当为 'md5' (用一个 md5 hash控制(最好但最慢)), 'crc32' (用一个crc32 hash (更快但安全性稍低于md5)) 或'strlen' (只用一个长度测试 (最快)) |
pearErrorMode | integer | CACHE_LITE_ERROR_RETURN | pear 错误模式 (当调用raiseError时) (使用CACHE_LITE_ERROR_RETURN 只返回一个 PEAR_Error对象 或 CACHE_LITE_ERROR_DIE用来立即停止脚本(最好用于debug)) |
fileNameProtection | boolean | TRUE | 文件名保护 (如果设为 true, 你可以使用任何cache Id 或组名, 如果设为false, 它会更快 , 但是 cache id和组名将直接应用到 cache文件的文件名,所以要小心使用特殊字符...) |
automaticSerialization | boolean | FALSE | 启用 / 禁用 自动序列化 (它用于直接存储不为字符串的数据,但是它会比较慢) |
memoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Memory Caching" (NB : memory caching没有生命周期, 只到脚本结束时为止) |
onlyMemoryCaching | boolean | FALSE | [BETA QUALITY] 启用 / 禁用 "Only Memory Caching" (如果启用, 将不再使用文件) |
memoryCachingLimit | integer | 1000 | [BETA QUALITY] 存储在内存缓冲中的纪录的最大数量 |
defaultGroup | string | Cache_Lite_Function | Function缓冲的缺省cache组 |
Note
这个函数不能静态的调用.
Cache_Lite_Function::call()
Cache_Lite_Function::call() – 调用一个可缓冲的函数或方法 (如果已经有个一个cache则不进行调用)
大纲
require_once 'LiteFunction.php'; |
mixed Cache_Lite_Function::call
(string$functionName, mixed$arg1,mixed$arg2, mixed$arg3, mixed...)
描述
当没有一个函数的cache的时候,以给定的参数调用给定的函数; 否则, 函数的输出被从cache中读取出来然后发送给浏览器并且返回值也是从cache中取出并返回的。
返回值
returns 函数/方法的结果
Note
这个函数不能静态的调用.
例子
例子 23-1. 使用函数的典型用法 require_once('Cache/Lite/Function.php'); $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 3600); $cache = new Cache_Lite_Function($options); $cache->call('function_to_bench', 12, 45); function function_to_bench($arg1, $arg2) { echo "This is the output of the function function_to_bench($arg1, $arg2) !"; return "This is the result of the function function_to_bench($arg1, $arg2) !";} ?> |
例子
例子 23-1. 使用方法的典型用法 require_once('Cache/Lite/Function.php'); $options = array( 'cacheDir' => '/tmp/', 'lifeTime' => 3600); $cache = new Cache_Lite_Function($options); $obj = new bench();$obj->test = 666; $cache->call('obj->method_to_bench', 12, 45); class bench{ var $test; function method_to_bench($arg1, $arg2) { echo "\$obj->test = $this->test and this is the output of the method \$obj->method_to_bench($arg1, $arg2) !"; return "\$obj->test = $this->test and this is the result of the method \$obj->method_to_bench($arg1, $arg2) !"; } } ?> |
如果你试图以$this对象来使用Cache_Lite_Function (例如$cache->call('this->method',...),先看看这个 : this bug
例子
例子 23-1. 使用静态方法的典型用法
require_once('Cache/Lite/Function.php');
$options = array(
'cacheDir' => '/tmp/',
'lifeTime' => 3600
);
$cache = new Cache_Lite_Function($options);
$cache->call('bench::static_method_to_bench', 12, 45);
class bench
{
var $test;
function static_method_to_bench($arg1, $arg2) {
echo
"This is the output of the function static_method_to_bench($arg1, $arg2) !
";
return
"This is the result of the function static_method_to_bench($arg1, $arg2) !
";
}
}
?>