我正在使用Doctrine 1.2和Zend Framework 1。
我有很多控制器,我在我的布局中有左侧栏和右侧栏,我在活动会话中有访问者和认证用户。
我已经做了一个基本的缓存方法,如:$frontend= array('lifetime' => 3600);
$backend= array('cache_dir' => '/data/cache/FileName');
$cache = Zend_Cache::factory('core', 'File', $frontend, $backend);
if ((!$result = $cache->load('fileName'))) {
/* my code here*/
$cache->save($page, 'fileName');
} else {
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->appendBody($result);
}
但是每次我需要在控制器中兑现页面时,我都会在每个控制器中重复此代码,所以我想创建一个助手来管理所有情况和场景的现金,其中包含一组参数,如(文件名,生命周期,登录用户,其他)和从左侧栏等布局中兑现文件,并能够删除现金文件。所以我只从我的控制器调用方法,并从一个地方处理它。
在Zend框架中构建这个缓存技术助手的最佳方法是什么,如果有的话请举例帮助我,并为我提供最好的方法来建立它。
谢谢。
你可以使用Zend_Cache_Frontend_Page在你的zend bootstrap.php.
:
$frontendOptions = array(
'lifetime' => '604800000',
'content_type_memorization' => false,
'default_options' => array(
'cache' => true,
'make_id_with_cookie_variables' => false,
'make_id_with_session_variables' => false,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_files_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
'$' => array('cache' => true),
)
);
$backendOptions = array('cache_dir' => $yourDirectoryPath);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
$cache->start();
遵循Zend Cache手册中的教程
http://framework.zend.com/manual/1.12/en/zend.cache.theory.html如果您真的需要缓存页面,为什么不缓存昂贵的数据呢?
在Zend框架1中,插件加载在每个控制器上,ZF1自定义插件
或仅在某些地方使用动作帮助器ZF1动作帮助器