我正在使用默认的Codeigniter页面缓存,例如:
$this->output->cache(n);
我的问题是我在两个不同的控制器中使用它并获得一个重复的缓存页面,即两者都返回相同的页面。我相信这是因为使用了一个子域,例如:
mobile.mysite.com =>控制器1
mysite.com =>控制器2
当我在两者上启用缓存时,我会返回相同的页面。
如何为每个生成不同的缓存?
问候,本。
答案 0 :(得分:0)
默认情况下,输出缓存是基于控制器的。因此,当您看到控制器的名称是否相同时,它将生成或使用相同的缓存(如果缓存目录在两个位置都相同)。
您最好的解决方法是使用缓存驱动程序并手动存储缓存。以下是控制器代码的示例:
public function index()
{
// If we have a cache just return it and be done.
if ($mobile = $this->cache->get('page_mobile') AND $this->agent->is_mobile())
{
$this->output->set_output($mobile);
return TRUE;
}
elseif ($page = $this->cache->get('page))
{
$this->output->set_output($page);
return TRUE;
}
$vars = array();
// Save a cache and output the page.
if ($this->template->is_mobile)
{
$home = $this->load->view('page_mobile', $vars, TRUE);
$this->cache->save('controller_mobile', $home, 500);
$this->output->set_output($home);
}
else
{
$home = $this->load->view('page', $vars, TRUE);
$this->cache->save('controller', $home, 500);
$this->output->set_output($home);
}
}