PHP
  Home arrow PHP arrow Page 7 - Caching With PHP Cache_Lite
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Caching With PHP Cache_Lite
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 18
    2003-06-06

    Table of Contents:
  • Caching With PHP Cache_Lite
  • The Food Chain
  • Return Of The Jedi
  • Digging Deeper
  • In And Out
  • Bits And Bytes
  • No News Is Good News
  • Cache Cow
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Caching With PHP Cache_Lite - No News Is Good News


    (Page 7 of 9 )

    Here's another, more realistic example, which caches a database result set and uses the cached set in order to reduce the time spent on executing subsequent queries for the same data. The result set is obtained via a function called get_headlines(), converted into a PHP array and cached using the call() method of the Cache_Lite_Function class.

    
    

    <?php


    // returns an array of headlines from a database

    function get_headlines()

    {

    // open connection to database

    $connection = mysql_connect("localhost", "joe", "secret")
    or die ("Unable to connect!");

    mysql_select_db("db111") or die ("Unable to select database!");


    // formulate and execute query

    $query = "SELECT headline FROM news ORDER BY timestamp DESC";

    $result = mysql_query($query) or die("Error in query: " . mysql_error());



    // iterate over result set and create array

    while ($row = mysql_fetch_object($result))

    {

    $arr[] = $row->headline;

    }



    // close connection

    mysql_close($connection);



    // return array of records

    return $arr;

    }


    // include the class

    require_once('Function.php');


    // configure the cache

    $options = array(

    'cacheDir' => 'cache/',

    'lifeTime' => 600

    );


    // create an instance of the Cache_Lite_Function object $objCache = new Cache_Lite_Function($options);


    // cache the call to the get_headlines() function

    // if return value is present in cache,

    // Cache_Lite_Function will fetch it from the cache

    // else the function will be called and the result returned $data = $objCache->call('get_headlines');


    // iterate over return value and print elements

    echo "<ul>";

    foreach($data as $h)

    {

    echo "<li>$h";

    }

    echo "</ul>";


    ?>

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway