Home  >  Article  >  php教程  >  How to use memcache in zend framework

How to use memcache in zend framework

高洛峰
高洛峰Original
2017-01-03 13:49:591097browse

The example of this article describes the method of using memcache in zend framework. Share it with everyone for your reference, the details are as follows:

In the zend framework project, the following are the specific methods:

1. Find Bootstrap.php and add the following initialization method (note: Bootstrap.php is the initialization method Load all operations):

protected function _initMemcache()
{
$frontendOpts = array(
'caching' => true,
'lifetime' => 1800, //缓存生命周期3分钟,根据自己项目需求设置
'automatic_serialization' => true
);
$backendOpts = array(
'servers' =>array(
array(
'host' => '127.0.0.1',
'port' => 11211
)
),
'compression' => false
);
$memcache = Zend_Cache::factory('Core', 'Memcached', $frontendOpts, $backendOpts);
Zend_Registry::set('memcache',$memcache);
}

2. Just call it where you need it:

For example, call it in your IndexController Friendly links

public function indexAction(){
 $memcache=Zend_Registry::get('memcache');
 //友情链接
 if(!$datalink = $memcache->load('datalink')){
 $link=new Blog_Model_Friendlink();
 $datalink = $link->listshi ();//print_r($datalink);die;
 $memcache->save($datalink, 'datalink');
 }
 $this->view->datalink=$datalink;
}

I hope this article will be helpful to everyone in PHP programming.

For more related articles on how to use memcache in zend framework, please pay attention to the PHP Chinese website!

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