example
1 <?php 2 # basic example: 3 # imagine that we have a source file, in our case /tmp/toCrunchData.txt 4 # based on that file we create something. and the output will be cached. 5 # whenever the source file changes, we have to drop the cache and 6 # "crunch" our data again. 7 8 #include dependencies 9 require_once($_SERVER['DOCUMENT_ROOT'] . '../global.conf.php'); 10 include_once($APP['path']['core'] . 'file/Bs_FileCache.class.php'); 11 12 # 1) create the instance 13 $cacher =& new Bs_FileCache(); 14 15 # 2) Try to get the cached data using the path of the source file as key. 16 $data = $cacher->fetch('/tmp/toCrunchData.txt'); 17 18 # 3) Test the return value 19 if ($data === FALSE) { // ERROR 20 echo "ERROR: " . $cacher->getLastError(); 21 } elseif ($data === NULL){ // cache is "Out Of Date", or not cached yet. 22 // Crunch the data (Replace with *YOUR* crunching function.) 23 $crunchedData = myCruncher('/tmp/toCrunchData.txt'); 24 25 # 4) Cache the data as serialized data 26 $cacher->store('/tmp/toCrunchData.txt', serialize($crunchedData)); 27 } else { // SUCCESS 28 $crunchedData = unserialize($data); 29 } 30 31 # 5) do something with $crunchedData 32 dump($crunchedData); 33 34 35 # this is your cruncher function. of course you would do more 36 # than just reading the data. 37 function myCruncher($fullPath) { 38 return join('', file($fullPath)); 39 } 40 ?>
|