Contents
Login Register Wishes Forum About
Language Reference » pCache functions » pCache
Help topics
Latest updates
4574d  replaceImageMapTitle
4574d  replaceImageMapValues
4574d  createFunctionSerie
4574d  setAbsicssaPosition
4574d  negateValues
Toolbox
  Download as PDF
  Print this page
  Share this page
  Create an account
  Feed the updates!
Google AdSense
pCache - Reduce your CPU time

This class implements a simple caching method for your charts. You can use it to reduce the web server CPU time, speed up the web transaction and improve the user experience while people will browse your pages.

Caching can only work efficiently if the users of your web site are displaying the same picture many time, in the case that you are displaying real-time data with a small refresh time, caching will be useless.

The pCache class makes use of two flat files : index.db that contains for each cached entries :

The unique hash. (or key)
Theposition of the object in the raw database.
The size of the object.
The date it has been rendered.
The number of hits for this object.

..and cache.db that contains the raw pictures. The web server must be able to get full access to this files. If you also want to make use of the cache removal function, the best is to create a cache folder and grant full access to the web server user on it.

The basic cache workflow is described bellow :



Using this class will require that you change a bit your way of coding. Cache can be tested even before creating the pDraw object. If you use it wisely this will drasticaly lower the CPU & memory use of your web server.

Calling this function
pCache($Settings="");

Settings is an optional array containing the cache configuration parameters.


Cache settings - Override default settings!

The Settings array allow you to redefine the path to the cache folder and the name of the database files. By default, the class will assume the existence of a "cache" folder at the same level of your calling script. The database index file will be called index.db and the database solid file will be called cache.db.

The name of the cache folder can be changed with CacheFolder.
The name of the index file can be changed with CacheIndex.
The name of the database solid file can be changed with CacheDB


Sample script



 /* Include all the classes */ 
 include("class/pDraw.class.php"); 
 include("class/pImage.class.php"); 
 include("class/pData.class.php");
 include("class/pCache.class.php");

 /* Create your dataset object */ 
 $myData = new pData(); 
 
 /* Add data in your dataset */ 
 $myData->addPoints(array(1,3,4,3,5));

 /* Create the cache object */
 $myCache = new pCache();

 /* Compute the hash linked to the chart data */
 $ChartHash = $myCache->getHash($myData);

 /* Test if we got this hash in our cache already */
 if ( $myCache->isInCache($ChartHash))
  {
   /* If we have it, get the picture from the cache! */
   $myCache->saveFromCache($ChartHash,"cache.png");
  }
 else
  {
   /* Create a pChart object and associate your dataset */ 
   $myPicture = new pImage(700,230,$myData);

   /* Choose a nice font */
   $myPicture->setFontProperties(array("FontName"=>"fonts/Forgotte.ttf","FontSize"=>11));

   /* Define the boundaries of the graph area */
   $myPicture->setGraphArea(60,40,670,190);

   /* Draw the scale, keep everything automatic */ 
   $myPicture->drawScale();

   /* Draw the scale, keep everything automatic */ 
   $myPicture->drawSplineChart();

   /* Do some cosmetics */
   $myPicture->drawGradientArea(0,0,700,20,DIRECTION_VERTICAL,array("StartR"=>0,"StartG"=>0,"StartB"=>0,"EndR"=>50,"EndG"=>50,"EndB"=>50,"Alpha"=>100));
   $myPicture->setFontProperties(array("FontName"=>"fonts/Silkscreen.ttf","FontSize"=>6));
   $myPicture->drawText(10,13,"Test of the pCache class",array("R"=>255,"G"=>255,"B"=>255));

   /* Push the rendered picture to the cache */
   $myCache->writeToCache($ChartHash,$myPicture);

   /* Render the picture */
   $myPicture->Render("cache.png");
  }

The first time that this code will be executed, the picture will be rendered and pushed in the cache, the next times, it will be retrieved from the cache directly saving CPU and execution time.
Last updated on 08/23/2010 
by Jean-Damien 
Linked resources
Community comments
  No comments have been posted yet.
© Copyrights
Components used on this web site : Famfamfam icons has been made by Mark James, Rounded corners lite has been coded by Cameron Cooke and Tim Hutchison, SyntaxHighlighter has been written by Alex Gorbatchev. pChart and this web site have been created by Jean-Damien POGOLOTTI. This documentation contains 185 pages and 56 comments. 415 users have registered. This page has been rendered in 0,20 seconds. Wiki revision 1.37.