Caching PHP -> HTML
Posted: Sun Aug 08, 2010 3:26 am
Hey all, I'm new here, I've been playing around with PHP for a while now but I wanted to move on to more complex practices, and caching is one of them.
See, I have this crontab that updates a mysql database every hour. Then, I have a php file that generates a table with the mysql table contents. Everything works OK, I can see my table and it gets updated just fine. However, I thought of caching that PHP file into an HTML file, because a lot of users access that table and that means a couple of mysql queries for every request for every user. I don't know if that's ok or if it affects my website/database performance. I thought of caching that PHP into an HTML with a cron that would execute 5 min after the normal database update.
So basically, I have a crontab that does something like this:
That code works great, as it creates an HTML file from the PHP output, which means, every user now gets a cached HTML file, and don't query the database at all. So basically there are only around 15-20 request per day to the database (for caching) and another 15-20 mysql queries for updating the database.
I was wondering if this is acceptable as "caching". I really have little knowledge about this concept, I know the basics but I'm not sure if I'm doing it right. Or maybe there's a better way. I'm trying not to involve third party applications since I really want to keep learning PHP and its best practices by myself. I have to be honest, I haven't read much about caching and how it should be implemented, but I'm really satisfied with the results of this script I made myself. So now I want an opinion from more experienced developers, am I doing it right? Is there a better way?
Thanks a lot for your help!
See, I have this crontab that updates a mysql database every hour. Then, I have a php file that generates a table with the mysql table contents. Everything works OK, I can see my table and it gets updated just fine. However, I thought of caching that PHP file into an HTML file, because a lot of users access that table and that means a couple of mysql queries for every request for every user. I don't know if that's ok or if it affects my website/database performance. I thought of caching that PHP into an HTML with a cron that would execute 5 min after the normal database update.
So basically, I have a crontab that does something like this:
Code: Select all
<?php
$contents = file_get_contents("http://mywebsite.com/mydata.php");
$FileHandle = fopen("/home/user/public_html/mytable.html", 'w') or die("can't open file");
fwrite($FileHandle, $contents);
fclose($FileHandle);
?>
I was wondering if this is acceptable as "caching". I really have little knowledge about this concept, I know the basics but I'm not sure if I'm doing it right. Or maybe there's a better way. I'm trying not to involve third party applications since I really want to keep learning PHP and its best practices by myself. I have to be honest, I haven't read much about caching and how it should be implemented, but I'm really satisfied with the results of this script I made myself. So now I want an opinion from more experienced developers, am I doing it right? Is there a better way?
Thanks a lot for your help!