caching:

Easy caching for PHP

0

I just implemented this cache class
http://www.phpguru.org/static/Caching on one of my sites.

The product detail page
ALR Hyperdrive 3.0 has eight or ten queries that run every time to pull the product info, nutrition info, reviews, manufacturer info, category info, etc…  This cache class provides the ability to store either output cache (html) or data cache (query results) in a file.  Each time the page is loaded, we’ll first check if there is a cached file of the output before running all the queries.  If there is a cache file, we’ll grab it and display.  If not, we’ll run the queries as usual and then save the output to a file.  We’re able to set an expiration on the cache file so it automatically refreshes itself in case the content has changed.

Example for caching output

    if (!OutputCache::Start("myGroup", "myID", 600)) {
 
        // Generate some output (as you do)...
 
        OutputCache::End();
    }

Example for caching data structures and query results

    if (!$data = DataCache::Get("myGroup", "myOtherID")) {
 
        $result = $db->query("SELECT BIG_ASS_QUERY()");
 
        DataCache::Put("myGroup", "myOtherID", 600, $result);
    }
 
    // Do something useful with $result

The page on my site ALR Hyperdrive 3.0 took on average 9 seconds from the time I clicked the refresh button until it was completely loaded.  I added the few lines of code for output caching on the page so it would skip all product related queries and it then averaged 7.5 seconds per page load and database processing and bandwidth in the meantime.  I was hoping the pages would load in two or three seconds, but this is a great improvement for the little effort it took.

Posted in: PHP
Tags: ,

Continue Reading