美好的一天,
我在Zend_Cache_Frontend_Page中遇到了一些问题。我想使用Zend_Cache_Frontend_Page来缓存一些页面。但它不起作用。
protected function _initBookPageCaching()
{
Zend_Controller_Front::getInstance()->setParam('disableOutputBuffering',true);
// cache book pages
$frontendOptionsPages = array(
'lifetime' => 2592000,
'debug_header' => true, // for debugging
'regexps' => array(
// cache the whole IndexController
'^/$' => array('cache' => true),
'^/download-free-ebook/.*' => array('cache' => true)
)
);
$backendOptionsPages = array(
'cache_dir' => APPLICATION_PATH.'/../cache'
);
$cacheBookPages = Zend_Cache::factory(
'Page',
'File',
$frontendOptionsPages,
$backendOptionsPages);
$cacheBookPages->start();
}
我将上面的方法设置为Bootstrap.php文件。
请帮我解决这个问题。
感谢您提前。
答案 0 :(得分:3)
设置application.ini
resources.frontController.params.disableOutputBuffering = true
设置引导程序或操作
$frontendOptions = array(
'lifetime' => 900,
'automatic_serialization' => true,
'default_options' => array(
'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,
'make_id_with_get_variables' => true,
'make_id_with_post_variables' => true,
'make_id_with_session_variables' => true,
'make_id_with_files_variables' => true,
'make_id_with_cookie_variables' => true,
'cache'=>true
),
);
$backendOptions = array(
'cache_dir' => APPLICATION_PATH . '/servercache/'
);
$cache = Zend_Cache::factory('Page', 'File', $frontendOptions, $backendOptions);
$cache->start();
我们走了!!!!!!!!!!!!!!!!!!
答案 1 :(得分:2)
我从来没有在我的bootstrap中使用Zend_Controller_Front :: getInstance(),而是使用:
$this->bootstrap('frontController');
接下来是:
$front = $this->getResource('frontController');
你可以尝试:
$front->setParam('disableOutputBuffering', true);
另外,我不确定可接受的正则表达式对于控制器是什么,什么不是。也许可以尝试将'^/download-free-ebook/.*'
更改为'^/download-free-ebook/'
。并查看该控制器的索引操作是否至少缓存。
修改强>
我发现了这个问题。 $的cache>开始();返回false,所以我做了一些调试。您没有传递缓存ID(例如$cacheBookPages->start('some_cache_id');
),因此Zend_Cache_Frontend_Page会尝试在第261行为您制作一个。如下所示:
// from Zend/Cache/Frontend/Page.php
public function start($id = false, $doNotDie = false)
{
// ....
if (!$id) {
$id = $this->_makeId();
if (!$id) {
return false;
}
}
// ....
}
因此,在跟踪兔子踪迹之后,我意识到它正在尝试根据以下数组构建缓存ID:$ _GET,$ _POST,$ _SESSION,$ _COOKIE和$ _FILES。由于这些都没有设置,因此无法创建缓存ID!由于它无法创建缓存ID,因此无法创建缓存文件。
解决方案:
$uri = explode('/', $_SERVER['REQUEST_URI']);
$cacheKey = array();
foreach ($uri as $key) {
if (empty($key)) {
continue 1;
}
$cacheKey[] = $key;
}
$cacheBookPages->start('page_cache_' . implode('_', $cacheKey));
答案 2 :(得分:0)
是的,正则表达式可能是错误的,尤其是斜线可能会导致问题。尝试通过在每个斜杠之前放置\来逃避它,如果这不起作用,尝试将所有内容放入斜杠中: “/ ^ /下载免费的电子书/.*/'