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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PHP

Caching With PHP Cache_Lite
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 20
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek