我使用的是蛋糕2.1.3,目前有一個網頁每秒獲得數百個視圖,所以我已經使用了單詞緩存以更好地處理負載。問題是,一旦緩存過期,我的服務器資源以及數百個mysql連接就會出現峯值。CakePHP:爲什麼我的緩存文件在到期時會導致巨大的峯值?
我想知道如果我這樣做是錯誤的方式,如果我應該運行一個cron來緩存頁面,而不是我目前如何做,或者如果有另一種技術,我沒有想到。
這裏就是我的函數看起來像在我的控制器:
public function index() {
$this->layout = 'ajax';
if (isset($this->params['url']['callback'])) {
$callback = $this->params['url']['callback'];
}else{
$callback = 'callback';
}
$this->set('callback',$callback);
$today = date("Y-m-d");
$end_date = strtotime ('+1 day' , strtotime($today)) ;
$end_date = date ('Y-m-d' , $end_date);
$start_date = strtotime ('-1 day' , strtotime($today)) ;
$start_date = date ('Y-m-d' , $start_date);
$total = Cache::read('popular_stories', 'short');
if (!$total) {
$total = $this->TrackStoryView->find('all', array(
'fields' => array('COUNT(story_id) AS theCount', 'headline', 'url'),
'conditions' => array('date BETWEEN ? AND ?' => array($start_date,$end_date)),
'group' => 'story_id',
'order' => array('theCount DESC'),
'limit' => 20,
));
Cache::write('popular_stories', $total, 'short');
}
$this->set('story', $total);
}
這裏是我的緩存的配置看起來像我的bootstrap.php文件:
Cache::config('short', array(
'engine' => 'File',
'duration' => '+60 minutes',
'path' => CACHE,
'prefix' => 'cake_short_'
));
這是在我的視圖文件:
<?php echo $callback . '('.json_encode($story).')'; ?>
我希望,一旦ŧ他緩存文件到期,只要第一個人訪問它,它就會創建一個新的緩存文件併爲每個人提供服務,但是因爲每秒有數百人在觸碰它,所以似乎這種方法不適合我也許我應該以某種方式緩存視圖視圖,或者也許有一種不同的方式來緩存,我不是在utelizing。
+1用於在更新某些內容時重新構建緩存(如果可能,使用您的應用程序),因爲如果甚至沒有必要,這會比每隔x分鐘/小時運行一次cron更有效。 – Joseph 2013-03-12 02:00:21
我通常會同意'afterSave()'的更新,但是這對於這個特殊的實例並不適用,因爲我每次查看故事時都會用新記錄更新數據庫。 – bigmike7801 2013-03-12 18:58:10