Web Hosting Talk







View Full Version : How to cache this?


doooby
01-02-2005, 11:01 AM
Hello,

I'm using a Top 5 for my script and I want to cache all the top 5 tracks for the artists I have, What I want to do is for each artist's top 5 tracks there's a .cache file actually when I run the code below it runs the 2nd query ($dbh) for all the artists and not one artist at a time, So what Am I Doing wrong? Help Is Appreciated. Thanks

$data = array();
$fromtimes = strtotime($fromtime);
$dbr = mysql_query("SELECT artists FROM id3s WHERE artists LIKE \"Z%\" AND album != \"\" GROUP BY artists ORDER BY artists ASC");
while($result2 = mysql_fetch_array($dbr)){
print $result2['artists'];
$dbh = mysql_query("SELECT downloads.counter, id3s.iid, id3s.name FROM id3s INNER JOIN downloads ON id3s.file = downloads.file
WHERE artists LIKE \"" . $result2['artists'] . "\" AND id3s.file LIKE \"%.mp3\" GROUP BY id3s.name ORDER BY counter DESC LIMIT 5");
$result = mysql_fetch_assoc($dbh);
$data[] = $result;
$fp = fopen($result2['artists'] . ".cache", "w");
fwrite($fp, serialize($data));
fclose($fp);
}

jasong
01-02-2005, 12:04 PM
Could you explain a bit more what you are trying to do please? That was a bit confusing :)

doooby
01-02-2005, 12:09 PM
Ok, I want to use a Top 5 Tracks for the artists on my website,

and instead of executing this query on each artist page:
SELECT downloads.counter, id3s.iid, id3s.name FROM id3s INNER JOIN downloads ON id3s.file = downloads.file
WHERE artists LIKE \"" . $result2['artists'] . "\" AND id3s.file LIKE \"%.mp3\" GROUP BY id3s.name ORDER BY counter DESC LIMIT 5


I want the result of it to be cached for each artist.

Was this clear?

jasong
01-02-2005, 12:16 PM
Well, you could create another field in the db called top5 and then combine them into one string separated by and implode them separated by | 's and then insert them into the field.
Then you just take that string out of the db and explode them, that is what I would do.

The thing is though that the top 5 tracks will probably be changing frequently, and the cache wouldnt. The only way to keep it up do date is to keep update it every time or have a cron job running a script for each artist.

error404
01-02-2005, 08:16 PM
Or just check if the cache is stale when the script is hit, and if it is, regenerate it...

I don't really see why you want to cache this. Create proper indexes on your database and the query should be extremely fast, it's very simple.

If you did want to cache it, it really shouldn't be very difficult to do at all. If the cache is stale, run the query, print the fields to a text file separated by some delimiter. Read the file, split it based on whatever delimiters you choose, and use that output as normal.

thartdyke
01-02-2005, 08:36 PM
I can't help thinking that it's going to be more work for the system to maintain the cache files. If the queries get repeated that frequently, then you'll find that the database engine will cache the queries itself.

Are you already seeing a problem?