PHP Programming/caching

From Wikibooks, open books for an open world
Jump to: navigation, search

A cache is a collection of duplicate data, where the original data is expensive to fetch or compute (usually in terms of access time) relative to the cache. In PHP caching is used to minimize page generation time. PHP basically has two main types of caching: 'output caching' and 'parser caching'. PHP 'output caching' saves a chunk of data somewhere which can later be read by another script faster than it can generate it. 'Parser caching' is specific feature. PHP is a scripting language and code is not compiled or optimized to a particular computer. Every PHP file must be parsed and that takes time. This type of time minimization is 'parser caching'.

parser caching[edit]

include caching[edit]

Example:

File:class.test.php

<?php
class test{
 function hello(){
    echo "Hello world!\n";
 }
}
echo "Class loaded\n"; ?>

File:program.php

<?php
require_once("class.test.php");
$obj1 = new test;
$obj1->hello();
require_once("class.test.php");
$obj2 = new test;
$obj2->hello(); ?>

output:

Class loaded
Hello world!
Hello world!

array caching[edit]

Example:

File:program.php

<?php
global $sum_cache; 
$sum_cache=array();
function sum($nr){
 global $sum_cache;
 if($sum_cache[$nr])
  {
  echo "sum(".$nr.")=".$sum_cache[$nr]." from cache\n";
  return $sum_cache[$nr];
  }
 if($nr<=0)
  {
  $sum_cache[$nr]=0;
  }
 else
  {
  $sum_cache[$nr]+=sum($nr-1)+$nr;
  }
 echo "sum(".$nr.")=".$sum_cache[$nr]." computed\n";
 return $sum_cache[$nr];
 }
sum(3);
sum(4);
?>

output:

sum(0)=0 computed
sum(1)=1 computed
sum(2)=3 computed
sum(3)=6 computed
sum(3)=6 from cache
sum(4)=10 computed

session caching[edit]

example:

file:program.php

<?php
session_start();
function find_my_name()
 {
 //perhaps some time-consuming database queries
 return "Bill"; 
 }
if(isset($_SESSION["hello"]))
 {
 echo "cached\n";
 session_destroy();
 }
else
 {
 echo "computed\n";
 $_SESSION["hello"]="My Name is ".find_my_name().".\n";
 }
echo $_SESSION["hello"];
?>

output:

computed
My Name is Bill.

output after refresh:

cached
My Name is Bill.

shared variables[edit]

Example

file:program.php

<?php
class test{
 var $list=array();
 function load_list()
  {
  //some less timeconsuming database querys
  $this->list[0]["info"]="small info nr 1";
  $this->list[1]["info"]="small info nr 2";
  $this->list[2]["info"]="small info nr 3";
  }
 function load_element_detail(&$data){
  //some very timeconsuming database querys
  $data["big"]="added big info, maybe structure of objects";
  }
 function get_element($nr){
  return $this->list[$nr];
  }
 function print_element($nr){
  echo "\$this->list[".$nr."][\"info\"]=\"".$this->list[$nr]["info"]."\"\n";
  echo "\$this->list[".$nr."][\"big\"]=\"".$this->list[$nr]["big"]."\"\n";
  }
 }
$obj = new test;
$obj->load_list();
$obj->print_element(0);
$element=&$obj->get_element(0);
$obj->load_element_detail($element);
$obj->print_element(0);
?>

output:

$this->list[0]["info"]="small info nr 1"
$this->list[0]["big"]=""
$this->list[0]["info"]="small info nr 1"
$this->list[0]["big"]="added big info, maybe structure of objects"

output caching[edit]