[ class tree: phpchartPlus ] [ index: phpchartPlus ] [ all elements ]

Source for file CacheToPEARDB_Strategy.php

Documentation is available at CacheToPEARDB_Strategy.php

  1. <?php
  2.  
  3. /*
  4. **************************************************
  5. Class: CacheToPEARDB_Strategy.php
  6. **************************************************
  7. Author: Tsiavos Chris <jaames@freemail.gr>
  8. Date: October 2004
  9. **************************************************/
  10.  
  11. /**
  12. *Includes the CachingStrategy_Interface
  13. */
  14. require_once("CachingStrategy_Interface.php");
  15.  
  16. /**
  17. *Includes the PEAR DB class
  18. */
  19. require_once("DB.php");
  20.  
  21. /**
  22. *Caches the generated image in a database
  23. *Advice: For feasible caching of large images use MEDIUMBLOB and LONGBLOB
  24. *instead of BLOB for column "Field_ImageData" to avoid errors
  25. *@author Tsiavos Chris <jaames@freemail.gr>
  26. *@uses PEAR DB Abstraction Layer http://pear.php.net
  27. *@license http://opensource.org/licenses/gpl-license.php GNU Public License
  28. */
  29. class CacheToPEARDB_Strategy implements CachingStrategy_Interface {
  30. private $PEAR_DB;
  31. private $RecordExists;
  32. private $DB_Type;
  33. private $DB_Username;
  34. private $DB_Passwd;
  35. private $DB_Host;
  36. private $DB_Name;
  37. private $DB_Table;
  38. private $Field_ImageName;
  39. private $Field_ImageData;
  40. private $Field_ImageModification;
  41. /**
  42. *Initializes the strategy object.
  43. *@param string[assoc] $Params $Params["DB_Type"]<br/>
  44. * $Params["DB_Username"]<br/>
  45. * $Params["DB_Passwd"]<br/>
  46. * $Params["DB_Host"]<br/>
  47. * $Params["DB_Name"]<br/>
  48. * $Params["DB_Table"]<br/><br/>
  49. * $Params["Field_ImageName"]<br/>
  50. * $Params["Field_ImageData"]<br/>
  51. * $Params["Field_ImageModification"]
  52. */
  53. public function Initialize($CacheImageType,$CacheForMinutes,$Params) {
  54. $this->DB_Type=$Params["DB_Type"];
  55. $this->DB_Username=$Params["DB_Username"];
  56. $this->DB_Passwd=$Params["DB_Passwd"];
  57. $this->DB_Host=$Params["DB_Host"];
  58. $this->DB_Name=$Params["DB_Name"];
  59. $this->DB_Table=$Params["DB_Table"];
  60. $this->Field_ImageName=$Params["Field_ImageName"];
  61. $this->Field_ImageData=$Params["Field_ImageData"];
  62. $this->Field_ImageModification=$Params["Field_ImageModification"];
  63. $this->PEAR_DB=DB::connect($this->DB_Type."://".$this->DB_Username.":".$this->DB_Passwd."@".$this->DB_Host."/".$this->DB_Name);
  64. if (DB::isError($this->PEAR_DB))
  65. throw new CachingStrategy_Exception("CacheToPEARDB_Strategy: ".$this->PEAR_DB->getMessage());
  66. $name=explode(".",basename($_SERVER["PHP_SELF"]));
  67. $CacheImage=$name[0].".cached";
  68. $Query="SELECT ".$this->Field_ImageName.",".$this->Field_ImageData.",".$this->Field_ImageModification." FROM ".$this->DB_Table." WHERE ".$this->Field_ImageName."='$CacheImage'";
  69. $DB_Query=$this->PEAR_DB->getAll($Query,DB_FETCHMODE_ASSOC);
  70. if (DB::isError($DB_Query))
  71. throw new CachingStrategy_Exception("CacheToPEARDB_Strategy: ".$DB_Query->getMessage());
  72. if (count($DB_Query)==1) {
  73. $this->RecordExists=1;
  74. $CacheTime=(time()-$DB_Query[0][$this->Field_ImageModification])/60;
  75. if ($CacheTime<$CacheForMinutes) {
  76. header("Content-type: image/$CacheImageType");
  77. header("Cache-Control: no-cache");
  78. print($DB_Query[0][$this->Field_ImageData]);
  79. $this->PEAR_DB->disconnect();
  80. return 1;
  81. }
  82. }
  83. ob_start();
  84. }
  85. public function CacheImage() {
  86. $name=explode(".",basename($_SERVER["PHP_SELF"]));
  87. $CacheImage=$name[0].".cached";
  88. if ($this->RecordExists)
  89. $Query="UPDATE ".$this->DB_Table. " SET ".$this->Field_ImageName."='$CacheImage',".$this->Field_ImageData."='".addslashes(ob_get_contents())."',".$this->Field_ImageModification."=".time()." WHERE ".$this->Field_ImageName."='$CacheImage'";
  90. else
  91. $Query="INSERT INTO ".$this->DB_Table."(".$this->Field_ImageName.",".$this->Field_ImageData.",".$this->Field_ImageModification.") VALUES ('$CacheImage','".addslashes(ob_get_contents())."',".time().")";
  92. $InsertQuery=$this->PEAR_DB->query($Query);
  93. if (DB::isError($InsertQuery))
  94. throw new CachingStrategy_Exception("CaheToPEARDB_Strategy: ".$InsertQuery->getMessage());
  95. $this->PEAR_DB->disconnect();
  96. }
  97.  
  98. }
  99.  
  100. ?>

Documentation generated on Sun, 3 Oct 2004 14:58:52 +0300 by phpDocumentor 1.3.0RC3