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

    - Getting More Info on Reflected Properties wi...
    - Implementing the Iterator SPL Interface
    - Building a Data Access Layer for the Data Ma...
    - Building a Singleton Database with Restricti...
    - Working with Reflected Properties with the R...
    - The Iterator, Countable and ArrayAccess SPL ...
    - Implementing the Data Mapper Design Pattern ...
    - Defining an Abstract Class with Restrictive ...
    - The Reflection API: Working with Reflected M...
    - Using Restrictive Constructors in PHP 5
    - Getting Information on a Reflected Class wit...
    - Introducing the Reflection API in PHP 5
    - Swift Mailer's Batchsend Method and Other Fe...
    - Embedding Attachments into Email Messages wi...
    - Dynamically Attaching Files with Swift Mailer


    Code Analysis Tools
    Enterprise code analysis tools that deliver quality and reliable code



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