优化设备在线计数性能

This commit is contained in:
wyx2685
2023-10-20 22:21:08 +09:00
parent 6417dda334
commit 98990f841f
2 changed files with 16 additions and 17 deletions

View File

@ -44,14 +44,10 @@ class UniProxyController extends Controller
$users = $users->toArray(); $users = $users->toArray();
$users = array_map(function ($user) { $users = array_map(function ($user) {
$count =0;
$ips_array = Cache::get('ALIVE_IP_USER_'. $user['id']); $ips_array = Cache::get('ALIVE_IP_USER_'. $user['id']);
$count = 0;
if ($ips_array) { if ($ips_array) {
foreach ($ips_array as $nodetypeid => $ip_array) { $count = $ips_array['alive_ip'];
foreach ($ip_array['aliveips'] as $ip) {
$count++;
}
}
} }
$user['alive_ip'] = $count; $user['alive_ip'] = $count;
return $user; return $user;
@ -94,22 +90,29 @@ class UniProxyController extends Controller
], 400); ], 400);
} }
foreach ($data as $k => $v) { foreach ($data as $nodeid => $ips) {
$updateAt = time(); $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 = []; $expired_array = [];
foreach($oldips_array as $nodetypeid => $oldips) { foreach($oldips_array as $nodetypeid => $oldips) {
if ($updateAt - $oldips['lastupdateAt'] > 300){ if ($updateAt - $oldips['lastupdateAt'] > 120){
$expired_array[$nodetypeid] = ''; $expired_array[$nodetypeid] = '';
} }
} }
// 清理过期数据并存回缓存 // 清理过期数据并存回缓存
$new_array = array_diff_key($oldips_array, $expired_array); $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([ return response([

View File

@ -158,11 +158,7 @@ class UserController extends Controller
$countalive = 0; $countalive = 0;
$ips_array = Cache::get('ALIVE_IP_USER_'. $request->user['id']); $ips_array = Cache::get('ALIVE_IP_USER_'. $request->user['id']);
if ($ips_array) { if ($ips_array) {
foreach ($ips_array as $nodetypeid => $ip_array) { $countalive = $ips_array['alive_ip'];
foreach ($ip_array['aliveips'] as $ip) {
$countalive++;
}
}
} }
$user['alive_ip'] = $countalive; $user['alive_ip'] = $countalive;