Home >Backend Development >PHP Tutorial >Zend Cache usage examples (share)

Zend Cache usage examples (share)

WBOY
WBOYOriginal
2016-07-25 08:57:22967browse
This article introduces an example of using Zend Cache for reference by friends in need.

This section shares an example of zend_cache in zf.

The code is as follows:

<?php
/**
* Zend Cache用法举例
* edit by bbs.it-home.org
*/
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');//保存变量到换存
}
/*使用Zend Cache还可以轻松缓存整页;且可将缓存存到数据库或者内存。大家好好研究下哦。*/ 
?> 


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn