Home arrow PHP arrow Page 7 - Caching With PHP Cache_Lite

No News Is Good News - PHP

As Web applications become more and more complex, cleverdevelopers can use application-level caching to improve the performanceof their PHP scripts. This article shows you how, discussing the PEARCache_Lite class with examples that illustrate how it can be used in alive environment.

TABLE OF CONTENTS:
  1. Caching With PHP Cache_Lite
  2. The Food Chain
  3. Return Of The Jedi
  4. Digging Deeper
  5. In And Out
  6. Bits And Bytes
  7. No News Is Good News
  8. Cache Cow
  9. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 22
June 06, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: