How to speed up a page

I heard someone mention that the categories page loaded slowly. That’s to be expected. It’s figuring out the last commit in each category. Calculating the number of ports in each category. And it’s doing this for both physical categories and virtual categories. There are about 16000 rows in the ports table, and about 94 categories. It takes a while to do that on a Pentium III.

Tonight I did something about it. I started caching. It was easier than I thought. I have done it before, for other pages, but this was the first time in a long time.

I have a cache directory, into which I put all the cached data. The categories.php page looks in this directory for a file, and reads it. That’s it. The code looks like this:

if (file_exists(CACHE_CATEGORIES) && is_readable(CACHE_CATEGORIES)) {
        readfile(CACHE_CATEGORIES);
}

There’s more to it. There’s also an else:

} else {
?>
<p>
big>Oops!</big>
Sorry, the category summary it not available just now.  It should appear within five minutes.
If it does not, please feel free to notify the webmaster who will promptly fix the problem.
</td></tr>
</table>
<?php
}

There is a cronjob that runs every five minutes. It refreshes the cache by fetching a total different URL (i.e. not http://www.freshports.org/categories.php) and stores the results in the cach directory. The cron job only refreshes the cache if there’s a flag set. This flag is in the form of a file (e.g. /tmp/FreshPorts/refresh_cache). If that file exists, it’s time to refresh the cache. That file is created by the commit processing code. It’s simple. And it works.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Comment

Scroll to Top