Home >Backend Development >PHP Tutorial >Zend Framework cache Cache usage simple example, zendframework_PHP tutorial
The example in this article describes the usage of Zend Framework Cache. Share it with everyone for your reference, the details are as follows:
<?php 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');//保存变量到换存 } /*这只是一种应用,还可以轻松缓存整页;也可将缓存存到数据库或者内存。*/ ?>
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.