提高网页加载速度,节省服务器PHP数组? [英] Improve Page Performance, save PHP array on server?

查看:108
本文介绍了提高网页加载速度,节省服务器PHP数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是可以存储一个PHP数组到我的服务器,现在它当有人从CSV文件重新加载页面但那是因为文件每一个小时后才机会永远不需要被创建。

is it possible to store a PHP-array to my server, right now it always gets created when someone reloads the page from a CSV file but that is unnecessary since the file only chances after each hour.

的ATM,所述页面需要像9秒加载,这是相当长的。该CSV文件,每行9个元素10K +行,所以这将是对性能确实不错,如果服务器没有处理为每个用户10万的元素。

ATM, the page takes like 9 seconds to load, which is quite long. The CSV file has 10k+ rows with 9 elements per row, so it would be really good for performance if the server didn't have to process 100k elements for each user.

我已经有一个cronjob下载csv文件,所以如果解析命令会在下载完成后,每小时执行,只有一次将是不错的。

I already have a cronjob for downloading the csv file so it would be good if the parse command would be executed after the download finished, only once per hour.

的cronjob:

<?php

function download_remote_file($file_url, $save_to) {
  $content = file_get_contents($file_url);
  file_put_contents($save_to, $content);
}
download_remote_file(<url here>, realpath(".") . '/dump.csv');
?>

和发生这种情况,页面重新加载每个:

and this happens with every reload of the page:

1:解析数据阵列

$url = 'dump.csv';
$csvData = file_get_contents($url);
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $line) {
$line = str_replace("\\", "&#92;", $line);
$line = str_replace("#", "&#35;", $line);
$array[] = str_getcsv($line);

第二:通过数组的Javascript

2nd: pass array to Javascript

var array = <?php echo json_encode( $array ) ?>;    

第三步:创建HTML表格

3rd: create HTML table

//some code

第四:初始化数据表插件

4th: initialise data table plugin

$(document).ready( function () {
    createtable();
    $('#scoreboard').DataTable( {
        "iDisplayLength": 50,
        language: {
            decimal: ".",       
        },
        "lengthMenu": false,
        "bLengthChange": false
    } );
} );

有什么可以做更快?

Is there something that could be done faster?

像,如前所述,保存PHP数组服务器端或者保存JS数组与HTML表格不知何故?

Like, as mentioned, save the php array server-side or maybe saving the JS array with the HTML table somehow?

-Innerwolf

-Innerwolf

推荐答案

既然你提到越来越以小时为单位,我建议如下的数据:

Since you mention getting the data on an hourly basis I suggest the following:


  1. 抢用cron中的CSV文件,并在数据库中的数据存储以小时为单位

  2. 配置您的数据表组件使用服务器端数据

  1. grab the CSV file with cron and store the data in a database on an hourly basis
  2. configure your data tables component to use server side data

这样你不会强迫每个用户一次下载整个阵列上的每个第一页的负荷。
服务器端脚本仅提取的需要在表中显示该特定网页上的记录数

This way you won't force every user to download the entire array at once on every first page load. The server side script only fetches the number of records that need to be displayed on that particular page in the table.

这篇关于提高网页加载速度,节省服务器PHP数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆