PHP中ZendCache用法的小例子

純潔的程式碼發表於2019-11-12
require'Zend/Loader.php';
Zend_Loader::loadClass('Zend_Cache');
Zend_Loader::loadClass('Zend_Config');
Zend_Loader::loadClass('Zend_Registry');
$config = new Zend_Config_Ini('configsecr/config.ini');
define('CACHE_DIR',FDROOT.'/'.'tmp/');
/*
config.ini
[cache]
cache.needcache=1
cache.frontend.name=Core
cache.frontend.lifetime=7200
cache.frontend.automatic_serialization=1
cache.backend.name=File
*/
/*選項參考手冊*/
/*建立cache物件*/
$frontendOptions = $config->cache->cache->frontend->toArray();
$backendOptions = $config->cache->cache->backend->toArray();
$frontendName = $frontendOptions['name'];
unset($frontendOptions['name']);
$backendName = $backendOptions['name'];
unset($backendOptions['name']);
if (empty($backendOptions['cache_dir']))
{
$backendOptions['cache_dir'] = CACHE_DIR;
}
$_cache = Zend_Cache::factory($frontendName, $backendName, $frontendOptions, $backendOptions);
Zend_Registry::set('cache', $_cache);
/*使用cache*/
$viewRenderer = $_cache->load('viewRenderer'); //試圖從快取載入變數
if (!$viewRenderer instanceof Something)//載入不成功
{
$viewRenderer = new Something();
/*some other work*/
$_cache->save($viewRenderer, 'viewRenderer');//儲存變數到換存
}
/*這只是一種應用,還可以輕鬆快取整頁;也可將快取存到資料庫或者記憶體。*/
?>


相關文章