mirror of
https://github.com/v2board/v2board.git
synced 2025-07-09 07:09:17 +08:00
优化设备在线计数性能
This commit is contained in:
@ -44,14 +44,10 @@ class UniProxyController extends Controller
|
||||
$users = $users->toArray();
|
||||
|
||||
$users = array_map(function ($user) {
|
||||
$count =0;
|
||||
$ips_array = Cache::get('ALIVE_IP_USER_'. $user['id']);
|
||||
$count = 0;
|
||||
if ($ips_array) {
|
||||
foreach ($ips_array as $nodetypeid => $ip_array) {
|
||||
foreach ($ip_array['aliveips'] as $ip) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
$count = $ips_array['alive_ip'];
|
||||
}
|
||||
$user['alive_ip'] = $count;
|
||||
return $user;
|
||||
@ -94,22 +90,29 @@ class UniProxyController extends Controller
|
||||
], 400);
|
||||
}
|
||||
|
||||
foreach ($data as $k => $v) {
|
||||
foreach ($data as $nodeid => $ips) {
|
||||
$updateAt = time();
|
||||
$oldips_array = Cache::get('ALIVE_IP_USER_'. $k) ?? [];
|
||||
$oldips_array = Cache::get('ALIVE_IP_USER_'. $nodeid) ?? [];
|
||||
|
||||
// 更新节点数据
|
||||
$oldips_array[$this->nodeType . $this->nodeId] = ['aliveips' => $v, 'lastupdateAt' => $updateAt];
|
||||
//删除过期节点在线数据
|
||||
$oldips_array[$this->nodeType . $this->nodeId] = ['aliveips' => $ips, 'lastupdateAt' => $updateAt];
|
||||
// 删除过期节点在线数据
|
||||
$expired_array = [];
|
||||
foreach($oldips_array as $nodetypeid => $oldips) {
|
||||
if ($updateAt - $oldips['lastupdateAt'] > 300){
|
||||
if ($updateAt - $oldips['lastupdateAt'] > 120){
|
||||
$expired_array[$nodetypeid] = '';
|
||||
}
|
||||
}
|
||||
// 清理过期数据并存回缓存
|
||||
$new_array = array_diff_key($oldips_array, $expired_array);
|
||||
Cache::put('ALIVE_IP_USER_'. $k, $new_array, 120);
|
||||
$count = 0;
|
||||
foreach($new_array as $nodetypeid => $newdata) {
|
||||
foreach($newdata['aliveips'] as $newip) {
|
||||
$count++;
|
||||
}
|
||||
}
|
||||
$new_array['alive_ip'] = $count;
|
||||
Cache::put('ALIVE_IP_USER_'. $nodeid, $new_array, 120);
|
||||
}
|
||||
|
||||
return response([
|
||||
|
@ -158,11 +158,7 @@ class UserController extends Controller
|
||||
$countalive = 0;
|
||||
$ips_array = Cache::get('ALIVE_IP_USER_'. $request->user['id']);
|
||||
if ($ips_array) {
|
||||
foreach ($ips_array as $nodetypeid => $ip_array) {
|
||||
foreach ($ip_array['aliveips'] as $ip) {
|
||||
$countalive++;
|
||||
}
|
||||
}
|
||||
$countalive = $ips_array['alive_ip'];
|
||||
}
|
||||
$user['alive_ip'] = $countalive;
|
||||
|
||||
|
Reference in New Issue
Block a user