优化设备在线计数性能

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 = 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([