gpt4 book ai didi

php - 加速非常慢的 PHP 脚本生成大型 JSON 数组的速度

转载 作者:行者123 更新时间:2023-11-29 11:16:01 26 4
gpt4 key购买 nike

我目前有一个在 AppEngine 上运行的 Web 应用程序,它使用 PHP 最初生成一个巨大的 JSON 数组,然后 Javascript 使用该数组填充我的 Web 应用程序的元素。

下面的代码工作完美并且可以完成我想要的一切,但是执行需要大约 10 秒。听起来可能不多,但确实感觉不合适。我想知道我应该采取什么不同的做法来加快初始数组的创建速度。

到目前为止,我已经考虑过通过几种方式来解决这个问题;

  • 我最初可以加载更少数据,从而减少加载时间从字面上看,加载更少。这会影响整体性能不过,现在一旦加载了数据,每个“部分”都会即时填充且速度超快。
  • 我可以使用 App Engine 缓存一些数据内置 Memcache。这是一个好主意,但是生成的数据经常改变。通常情况下,缓存无法做到这一点。
  • 使用网络而是用 socket 。我想最终做到这一点,并且它在待办事项中列表,但这意味着重写大量代码,所以我不太喜欢现在很喜欢这个。

我确信我的代码中也存在一些“不好的做法”,请随意指出并责骂我...... Here's the same code below as a Gist if you prefer it that way.

<?php
use google\appengine\api\cloud_storage\CloudStorageTools;


# ===================================== #
# ==== Generate App Settings Array ==== #
# ===================================== #
//Existing Class (not shown) already generated $app for us...
$app_settings_array = array('app_version' => $app->app_version, 'app_server' => $_SERVER['SERVER_SOFTWARE'], 'app_id' => $app->app_id, 'app_name' => $app->app_name, 'app_logo_dark' => $app->app_logo_dark, 'app_logo_light' => $app->app_logo_light, 'app_motd' => $app->app_motd, 'app_domain' => $app->app_domain);

# ===================================== #
# ==== Generate User Details Array ==== #
# ===================================== #
$currentuser = $_SESSION['user_id']; //Get current user id from session
$user_info = $db->prepare("SELECT * FROM hr_personal WHERE employee_id = :user_id LIMIT 1");
$user_info->bindParam(':user_id', $currentuser);
$user_info->execute();
$user = $user_info->fetch(PDO::FETCH_OBJ);

$email_hash = hash_hmac('sha256', $user->employee_id, '_redactedkey');

$log_info = $db->prepare("SELECT log_time FROM app_log WHERE log_user = :user_id ORDER BY log_time ASC LIMIT 1"); //Get
$log_info->bindParam(':user_id', $currentuser);
$log_info->execute();
$first_seen = $log_info->fetch(PDO::FETCH_OBJ);

$user_details_array = array('id' => $user->employee_id, 'id_hash' => $email_hash, 'first_seen' => $first_seen->log_time, 'title' => $user->employee_title, 'name' => $user->employee_knownas, 'avatar' => $user->employee_avatar, 'mobile' => $user->employee_mobile, 'email' => $user->employee_email);


# ============================= #
# ==== Generate Chat Array ==== #
# ============================= #
$currentuser = $_SESSION['user_id'];
$currentapp = $app->app_id;

$chat_array = array();

foreach (range('A', 'Z') as $char) {
$individuals = array();
$countrow = $db->prepare("SELECT * FROM hr_personal where employee_knownas LIKE '$char%' AND employee_id IN (SELECT notification_submitter FROM notification_sent WHERE notification_id IN (SELECT notification_id FROM notification_wait WHERE user_id = '$currentuser' AND method = 'online' ORDER BY notification_id DESC))");
$countrow->execute();
if ($countrow->rowCount() > 0) {
$mesage_query = "SELECT * FROM hr_personal where employee_knownas LIKE '$char%' AND employee_id IN (SELECT notification_submitter FROM notification_sent WHERE notification_id IN (SELECT notification_id FROM notification_wait WHERE user_id = '$currentuser' AND method = 'online' ORDER BY notification_id DESC))";
$message_query_run = $db->query($mesage_query);
while($row = $message_query_run->fetch(PDO::FETCH_ASSOC)) {
if(!empty($row['employee_avatar'])){
$options = ['size' => 200, 'crop' => true];
$image_file = "gs://rouic-cdn/internal/".$row['employee_avatar'];
$image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
$image_url = preg_replace("/^http:/i", "https:", $image_url);
} else {
$image_url = "";
}

$whileUser = $row['employee_id'];
$message_chain = array();

$chain_query = "SELECT * FROM notification_wait WHERE method = 'online' AND user_id = '$currentuser' AND notification_id IN (SELECT notification_id FROM notification_sent WHERE notification_submitter = '$whileUser')";
$chain_query_run = $db->query($chain_query);
while($chainRow = $chain_query_run->fetch(PDO::FETCH_ASSOC)) {
array_push($message_chain, array('id' => $chainRow['notification_id'], 'reply_id' => $chainRow['reply_id'], 'content' => $chainRow['message'], 'time' => $chainRow['time'], 'state' => $chainRow['state']));
}

array_push($individuals, array('id' => $row['employee_id'], 'group' => $char, 'name' => $row['employee_knownas'], 'avatar' => $image_url, 'message_chain' => $message_chain));
}
array_push($chat_array, array('group' => $char, 'data' => $individuals));
}

}


# ========================================== #
# ==== Generate Admin Groups (if admin) ==== #
# ========================================== #
$admin_array = array();
if($auth->checkPage('admin', $_SESSION['user_id']) == false){ //Existing CheckPage class returns true if user is allowed on page
array_push($admin_array, array('access' => 'denied'));
} else {
//Generate All Clients
$client_array = array();
$client_query = "SELECT * FROM clients WHERE client_app = '$currentapp'";
$client_query_run = $db->query($client_query);
while($row = $client_query_run->fetch(PDO::FETCH_ASSOC)) {
if(!empty($row['client_avatar'])){

$options = ['size' => 200, 'crop' => true];
$image_file = "gs://rouic-cdn/internal/".$row['client_avatar'];
$image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
$image_url = '<span class="thumbnail-wrapper d48 circular inline m-t-5">
<img id="dynamicavy" src="'.preg_replace("/^http:/i", "https:", $image_url).'" width="48" height="48">
</span>';

} else {
$image_url = "";
}

$short_desc = strlen($row['client_desc']) > 30 ? substr($row['client_desc'],0,30)."..." : $row['client_desc'];
$desc = '<span class="expandable" full-length="'.$row['client_desc'].'">'.$short_desc.'</span>';
$actions = '<button class="btn btn-xs btn-primary">Edit Details</button>';
array_push($client_array, array($row['client_id'], $image_url, "<b>".ucwords($row['client_name'])."</b>", $row['client_dob'], $row['client_phone'], $row['client_address'], $desc, $actions));
}

//Generate All Departments
$department_array = array();
$department_query = "SELECT * FROM departments WHERE dep_app = '$currentapp'";
$department_query_run = $db->query($department_query);
while($row = $department_query_run->fetch(PDO::FETCH_ASSOC)) {
if(!empty($row['dep_avatar'])){

$options = ['size' => 200, 'crop' => true];
$image_file = "gs://rouic-cdn/internal/".$row['dep_avatar'];
$image_url = CloudStorageTools::getImageServingUrl($image_file, $options);
$image_url = '<span class="thumbnail-wrapper d48 circular inline m-t-5">
<img id="dynamicavy" src="'.preg_replace("/^http:/i", "https:", $image_url).'" width="48" height="48">
</span>';

} else {
$image_url = "";
}

$short_desc = strlen($row['dep_description']) > 30 ? substr($row['dep_description'],0,30)."..." : $row['dep_description'];
$desc = '<span class="expandable" full-length="'.$row['dep_description'].'">'.$short_desc.'</span>';
$actions = '<button class="btn btn-xs btn-primary">Edit Details</button>';
array_push($department_array, array($row['dep_id'], $image_url, "<b>".ucwords($row['dep_name'])."</b>", $desc, $actions));
}
array_push($admin_array, array('access' => 'granted', 'allDepartments' => $department_array, 'allClients' => $client_array));
}


... About 4 More sections redacted ...


# ===================== #
# ==== Final Array ==== #
# ===================== #
$init_array_compare = array('chat' => $chat_array, 'admin' => $admin_array, 'app_details' => $app_settings_array, 'user_details' => $user_details_array, 'user_permissions' => $user_permissions_array);
$hash = md5(json_encode($init_array_compare)); //I'm basically creating a hash of the results here so I can see if anything has changed if generated again

$init_array = array('hash' => $hash, 'chat' => $chat_array, 'admin' => $admin_array, 'app_details' => $app_settings_array, 'user_details' => $user_details_array, 'user_permissions' => $user_permissions_array);


echo json_encode($init_array);

最佳答案

MonkeyZeus 完全正确,结果是 CloudStorageTools::getImageServingUrl() 导致挂起。编写脚本来缓存生成的 URL 后,脚本现在运行时间不到一秒。

关于php - 加速非常慢的 PHP 脚本生成大型 JSON 数组的速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39732565/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com