I apologise, for a first post this is quite a long one...
I seem to be making this more complicated than it needs to be, so am wondering if there's any hints out there that will make it not so! I'm not new to PHP, but there are probably some bits of it that I don't know as well as I probably ought...
I am working on a web player for an Internet radio station with the schedule data held in a mySQL database. The player updates on the screen according to the schedule with what show is currently on air and what is up next etc. That whole part of things works. A jquery refresh script calls the PHP file that queries the database every 10 seconds or so (the old schedule data was kept in a very large PHP array which was difficult to edit unless you knew PHP, and I've kept the same basic code for displaying it even if extracting the data has changed!).
This method though queries the database too much to gain the same data as before. As the data retrieved includes the time the next programme starts, it seems that I should be able to only query the database when the programme has changed. An obvious way of doing this is to write a cache file that the player page calls, and use that data unless the programme has changed.
The bit I'm currently struggling to wrap my head around is how to know when to change the cache, which - as I said - given that the data retrieved contains this information seems a little odd.
Say the file that gets the data from the database is 'getcache.php' and the file that contains the data is 'onair-cache.php' (because, that actually is their names!).
When 'getcache.php' is called, it should check 'onair-cache' for the time of next show. If this time has passed, then it should query the database for the new information and write it to 'onair-cache.php'. However, surely a file can't be written to if it's open, so putting something like:
if ($programme['end_time'] <= now) {
include ('getcache.php');
}
into 'onair-cache.php' wouldn't work as 'getcache.php' needs to be able to write the new information into 'onair-cache.php'. Also, what would happen if there are many users who are connected at the same time, each one will end up then querying and writing at the same time?
(Btw, that code was just some pseudocode that probably won't work as it is, but would just give the idea of what's needed!)
As I said right at the beginning, I seem to be making this more complicated than it needs to be, so if there's a simpler way of doing it then I'd be grateful to know!
Many thanks!