php script sometimes takes a long time to execute

ساخت وبلاگ
I'm a high school teacher. I wrote an HTML/Javascript program that lets students submit their answers to an assignment using AJAX and a server-side PHP script.
Then there is another HTML program that calls a different PHP script, which generates a Leader Board, so I can display their scores on the projector. This lets the students compete for high score.

The HTML page calls on this PHP script every 2 seconds, so the Leader Board constantly updates.

The basic logic of the PHP script is:
Parse $GLOBALS['HTTP_RAW_POST_DATA'] into assignment name and class list
For each student, read a file which contains their score on that assignment.
Organize this data and echo it.

When I first open the Leader Board, it sometimes loads right away, but sometimes it can take several minutes, even causing the browser to kill the page.
But once it finally works, it remains working. Every 2 seconds, the PHP is called again, and it generates its response within milliseconds. The huge delay is gone.
If I close the tab that has the Leader Board, and open it again within a few minutes, it loads immediately. But if I wait an hour and open it again, I again get the huge delay the first time the PHP is called.

For debugging, I had the PHP also output the time at various points in the code. Reading the file that contains the classlist and the file that contains the assignment were each done in under a second. But reading each students' grades takes about 2-3 seconds per student. But after the first execution of the PHP, it reads all 30 students grades in less than 1 second.

I've used this program for 4 years with no issues. But this summer, I bought a new domain name and a hosting plan from the same company as my original plan. I transfered all the files over, and everything was smooth and worked exactly like last year, except for this annoying delay.

It's hosted on a Unix server.

Any idea why it takes so long to read file contents the first time?
Thanks!
CodingForums...
ما را در سایت CodingForums دنبال می کنید

برچسب : نویسنده : codingforums بازدید : 168 تاريخ : سه شنبه 25 مهر 1396 ساعت: 22:45