Improve page performance, store PHP array on server?

Is it possible to store a PHP array on my server, now it is always created when someone reloads the page from a CSV file, but this is not necessary as the file only has a chance after every hour.

ATM, it takes about 9 seconds to load, which is quite long. The CSV file has 10k + lines with 9 items per line, so it would be very useful for performance if the server didn't have to process 100k items per user.

I already have a cronjob to download the csv file, so it would be nice if the parse command is executed after the download is complete, only once an 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 on every page reload:

1st: parse data in array

$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);

      

2nd: pass array to Javascript

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

      

3rd: create HTML table

//some code

      

4th: initialize data table plugin

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

      

Is there something that can be done faster?

How, as already mentioned, store the php server or maybe store the JS array with the HTML table?

-Innerwolf

+3


source to share


2 answers


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

  • take a CSV file using cron and store data in the database hourly.
  • configure the data tables component to use server side data


This way, you won't be forcing each user to load the entire array at once on every first page load. The server side script only allows you to get the number of records that should appear on that page in the table.

0


source


After analyzing your CSV, do the following:

$file = fopen('/tmp/output.js', 'w');
fwrite($file, '<script type="text/javascript">');
fwrite($file, 'var array =');
fwrite($file, json_encode( $array ));
fwrite($file, ';');
fwrite($file, '</script>');
fclose($file);

copy('/path/to/script.js', '/path/to/script.js.bak');
move('/tmp/output.js', '/path/to/script.js');

      

Then later, when you output HTML, you just need to insert:



<script type="text/javascript" src="/scripts/script.js">

      

in the title. Browser users should also cache it too. Pay attention to copy and move - you don't need to make a backup, but you MUST use move () to replace the "live" script - move () is atomic, more or less and win. Someone gets the semi-files as a result.

Also note that you will need write permissions where the script resides - there are ways to keep this pretty secure (not letting your PHP script write everything to the hard drive), but that's out of scope here.

+1


source







All Articles