|
目录
来源:http://trac.seagullproject.org/wiki/Howto/Caching 概述
Seagull框架通过SGL_Cache类实现对缓存的支持。SGL_Cache类事实上只是PEAR的Cache_Lite类的封装类。它支持下列的缓存类型
使用要缓存任何数据之前你必须先获取一个SGL_Cache实例: $cache = &SGL_Cache::singleton(); 然后取回缓存的结果或者执行数据调用,并将调用结果保存到缓存以备下次调用。典型的操作如下:
$cache = & SGL_Cache::singleton();
if ($serialized = $cache->get('all_users', 'perms')) {
$aPerms = unserialize($serialized);
SGL::logMessage('perms from cache', PEAR_LOG_DEBUG);
} else {
require_once SGL_MOD_DIR . '/user/classes/UserDAO.php';
$da = & UserDAO::singleton();
$aPerms = $da->getPermsByModuleId();
$serialized = serialize($aPerms);
$cache->save($serialized, 'all_users', 'perms');
SGL::logMessage('perms from db', PEAR_LOG_DEBUG);
}
你无需加载SGL_Cache库文件,因为Seagull已经帮你加载。 如何加载各种类型的缓存默认返回Cache_Lite实例: 1. $cache = &SGL_Cache::singleton(); 在调用时指定相应的参数返回Cache_Lite_Function参数: 2. $cache = &SGL_Cache::singleton('function', array(
'dontCacheWhenTheResultIsFalse' => true,
'dontCacheWhenTheResultIsNull' => true,
'lifeTime' => 3,
'debugCacheLiteFunction' => true,
));
下面的代码则返回Cache_Lite_Output实例: 3. $cache = &SGL_Cache::singleton('output');
强制返回Cache_Lite_Function实例: 4. $cache = &SGL_Cache::singleton('function', array(), true);
向后兼容的强制缓存代码: 5. $cache = &SGL_Cache::singleton(true); 清除缓存你可采用下列的代码清除缓存: SGL_Cache::clear('group_name');
clear方法可以静态调用。 |