2017-06-14 73 views
0

你好,我做了一個管理員面板來控制我的vps,專用服務器,所以我有很多數據fe cpu使用情況,內存使用情況,磁盤存儲使用情況,負載等我使用這個進度條是這樣的:Laravel 5.3從ajax加載數據(很多數據)

<li class="content"> <span>Cpu usage </span> 
    <div class="progress progress-mini progress-danger active progress-striped"> 
     <div style="width: 99%;" class="bar"></div> 
    </div> 
    <span class="percent">87%</span> 
    <div class="stat">Cores: 100</div> 
</li> 

,我想用Ajax加載這個(我知道該怎麼做) 但

Route::get('/ajax/cpuUsage',function(){ 
    //mysql query here 
    return response()->json('MyData'); 
}); 

,但是當我有我的第10名觀衆這條路線將被稱爲十次,並且我有十個請求每fe 5sec是一個小的ammount,但是當我哈哈有1k觀衆可以被殺。所以我想緩存數據,但laravel緩存分鐘1分鐘,我該怎麼辦?索裏對我的英語,但我想你可以undestard我說什麼:)

+0

這僅僅是想法,應該有人confir m,但我認爲nginx而不是apache應該是有幫助的(我認爲你有apache)。另一件事是使用節點js ...我懷疑PHP會適合在這裏最好 –

+0

緩存在這裏沒有意義,因爲這樣的「使用」統計數據是要顯示在實時數據。 – Ohgodwhy

+0

是的,但我想緩存5秒,所以我可以調用這個實時數據,我不能讓每個用戶1請求頁面上,我該怎麼辦? – lenyAxe

回答

0

您可以使用Ajax這樣的:

$(function() { 
     $.ajax({ 
      url: '/ajax/cpuUsage', 
      type: "GET", 
      success: function (data) { 
       console.log(data); 

       // do some logic here... 

      } 
     }); 
    }); 

你或許應該將你的邏輯CPU使用率數據提取到一個控制器:

<?php 

namespace App\Http\Controllers; 

use Illuminate\Http\Request; 

class UsageController extends Controller 
{ 




/** 
    * Display a listing of the resource. 
    * 
    * @return \Illuminate\Http\Response 
    */ 
    public function index() 
    { 
     // logic here 
    return response()->json($mydata); 
    } 

/** 
* Show the form for creating a new resource. 
* 
* @return \Illuminate\Http\Response 
*/ 
public function create() 
{ 
    // 
} 

/** 
* Store a newly created resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @return \Illuminate\Http\Response 
*/ 
public function store(Request $request) 
{ 
    // 
} 

/** 
* Display the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function show($id) 
{ 

} 

/** 
* Show the form for editing the specified resource. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function edit($id) 
{ 
    // 
} 

/** 
* Update the specified resource in storage. 
* 
* @param \Illuminate\Http\Request $request 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function update(Request $request, $id) 
{ 
    // 
} 

/** 
* Remove the specified resource from storage. 
* 
* @param int $id 
* @return \Illuminate\Http\Response 
*/ 
public function destroy($id) 
{ 
    // 
} 
} 

然後讓你的路由/ web.php路線是這樣的:

Route::get('/ajax/cpuUsage', '[email protected]'); 
+0

是的,我知道,但是當我打電話/ ajax/cpuUsage,我的路由功能將作出查詢,所以如果我已經在頁面1k的vievers這個功能被稱爲1k次是正確的?如果是正確的,那麼對於我的數據庫是很多的;/ – lenyAxe

+0

我不認爲你真的需要擔心這一點。但是你可以緩存查詢$ users = DB :: table('cpuUsage') - >記住(3) - > get();這樣你的數據庫就不會被淹沒。我相信如果您有一千個用戶,您的查詢將每三分鐘運行一次或您設置的任何內容。如果他們不斷從這條路線獲取數據。 –

+0

好吧,我發現我可以把緩存記住浮動,所以我讓: $ time = Cache :: remember('key',0.08,function(){ 返回Carbon :: now() - > second; }); 和這個緩存美元時間爲5秒,所以對我來說是好的我想我會用這個查詢:)但謝謝你的幫助:) – lenyAxe