【发布时间】:2012-04-19 12:47:12
【问题描述】:
我们目前正在运行一个使用 Zend_Cache_Backend_Static 将页面缓存到静态 html 文件的应用程序。这非常有效,除了当请求不正确的 url 时,我们的缓存会被数百个空文件和文件夹填满。如果抛出异常,有什么方法可以防止页面被缓存?我惊讶地发现这不是标准行为。
我做了一些挖掘,实际处理保存静态 html 页面的 ZF 代码如下在 Zend_Cache_Frontend_Capture 中:
public function _flush($data) {
$id = array_pop($this->_idStack);
if ($id === null) {
Zend_Cache::throwException('use of _flush() without a start()');
}
if ($this->_extension) {
$this->save(serialize(array($data, $this->_extension)), $id, $this->_tags);
} else {
$this->save($data, $id, $this->_tags);
}
return $data;
}
这个函数是 ob_start 的 output_callback。我尝试获取响应对象来测试状态,但它似乎在 _flush 中不起作用。
$response = Zend_Controller_Front::getInstance()->getResponse();
if($response->getStatus() == '200') {
// do the save as normal
}
else {
// do nothing
return false;
}
我唯一的另一个想法是测试 $data 的长度,仅在 strlen($data) > 0 似乎有效但感觉不够健壮时才进行缓存。
更新:
不幸的是,当我们点击 ErrorController 时,静态页面已经写入缓存,因此此时禁用缓存将不起作用。但是,可以根据 $_SERVER['REQUEST_URI'] 删除页面,这是首次写入页面时用作 id 的内容。这行可以添加到ErrorController中errorAction的开始:
$this->_helper->cache->removePage($_SERVER['REQUEST_URI'], true);
它工作得很好,但我不想一开始就写这个页面!
【问题讨论】:
标签: php zend-framework