Skip to content Skip to sidebar Skip to footer

What Type Of Caching Should I Use?

I have a classifieds website, which uses PHP and MYSQL. I have several pages which uses javascript also. I need to know what type of caching to use to increase performance on my si

Solution 1:

You can use mod_expire (if you are using apache as webserver) to set the expire HTTP header on your static content(js,images,favicon,plain HTML), so the browser won't request this object until it expires. Depending on your hosting and your audience it might be a good idea to use service as akamai to host your static content (images, css, javascript).

For starting to improve the performance of the server side (PHP), you have to identify bottlenecks. A good approach for doing that would be to implement some logging on your website (SQL queries and how many seconds to get results, what page are most viewed, what function take the most time). You will let this run few weeks/days. Analyze that and you would know what SQL queries to cache, what function to refactor.

If you are in hurry a quick and dirty approach is to get the top 10 most viewed page and cache them on disk. It would work but if your website is really dynamic and need information almost in real time you going to have invalidate that cache often. Also it can create problem if there is some login/logout process in your website. Another approach is to cache some part of those page, usually the more expensive to produce (DB/access, complicate processing).

In term of tools you can have on PHP to do such cache handling:

  • APC: that tool have some caching feature, plus PHP precompilation
  • memcached: a distibuted caching system
  • eAccelator: pre compilation
  • xcache: pre compilation

Solution 2:

For a site I recently launched, I wrote some code using ob_start() to cache my PHP files to flat HTML. Here's an example:

$cacheFile = 'cache/home.html';
$cacheTime = 600;

if (file_exists($cacheFile) && time() - $cacheTime < filemtime($cacheFile)) {
    require $cacheFile;
} else {
    ob_start();

    // Your dynamic code

    $fp = fopen($indexCacheFile, 'w');
    fwrite($fp, ob_get_contents());
    fclose($fp);
    ob_end_flush();
}

The site has been around nearly a month, and has done a lot of traffic. The above code has saved my server multiple times.


Solution 3:

You could use a caching proxy like Squid or some sort of PHP accelerator. Additionally, caching MySQL query results might be a good idea if the data you're querying doesn't change much.

As another answer noted, static content will usually be cached by the users' browsers if the timestamps on the files don't change.


Post a Comment for "What Type Of Caching Should I Use?"