Location: PHPKode > scripts > phpchartPlus > phpchartplus/Sources/CacheToPEARDB_Strategy.php
<?php

/*
**************************************************
Class: CacheToPEARDB_Strategy.php
**************************************************
Author: Tsiavos Chris <hide@address.com>
Date: October 2004
**************************************************/

/**
*Includes the CachingStrategy_Interface
*/
require_once("CachingStrategy_Interface.php");

/**
*Includes the PEAR DB class
*/
require_once("DB.php");

/**
*Caches the generated image in a database
*Advice: For feasible caching of large images use MEDIUMBLOB and LONGBLOB 
*instead of BLOB for column "Field_ImageData" to avoid errors
*@author Tsiavos Chris <hide@address.com>
*@uses PEAR DB Abstraction Layer http://pear.php.net
*@license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class CacheToPEARDB_Strategy implements CachingStrategy_Interface {
	
	private $PEAR_DB;
	
	private $RecordExists;
	
	private $DB_Type;
	private $DB_Username;
	private $DB_Passwd;
	private $DB_Host;
	private $DB_Name;
	private $DB_Table;
	
	private $Field_ImageName;
	private $Field_ImageData;
	private $Field_ImageModification;
	
	/**
	*Initializes the strategy object.
	*@param string[assoc] $Params $Params["DB_Type"]<br/>
	*				            $Params["DB_Username"]<br/>
	*				            $Params["DB_Passwd"]<br/>
	*				            $Params["DB_Host"]<br/>
	*				            $Params["DB_Name"]<br/>
	*				            $Params["DB_Table"]<br/><br/>
	*					    $Params["Field_ImageName"]<br/>
	*					    $Params["Field_ImageData"]<br/>
	*					    $Params["Field_ImageModification"]
	*/
	public function Initialize($CacheImageType,$CacheForMinutes,$Params) {
		
		$this->DB_Type=$Params["DB_Type"];
		$this->DB_Username=$Params["DB_Username"];
		$this->DB_Passwd=$Params["DB_Passwd"];
		$this->DB_Host=$Params["DB_Host"];
		$this->DB_Name=$Params["DB_Name"];
		$this->DB_Table=$Params["DB_Table"];
		
		$this->Field_ImageName=$Params["Field_ImageName"];
		$this->Field_ImageData=$Params["Field_ImageData"];
		$this->Field_ImageModification=$Params["Field_ImageModification"];	
		
		$this->PEAR_DB=DB::connect($this->DB_Type."://".$this->DB_Username.":".$this->DB_Passwd."@".$this->DB_Host."/".$this->DB_Name);
		
		if (DB::isError($this->PEAR_DB))
			throw new CachingStrategy_Exception("CacheToPEARDB_Strategy: ".$this->PEAR_DB->getMessage());
		
		$name=explode(".",basename($_SERVER["PHP_SELF"]));
		$CacheImage=$name[0].".cached";	
		
		$Query="SELECT ".$this->Field_ImageName.",".$this->Field_ImageData.",".$this->Field_ImageModification." FROM ".$this->DB_Table." WHERE ".$this->Field_ImageName."='$CacheImage'";
		
		$DB_Query=$this->PEAR_DB->getAll($Query,DB_FETCHMODE_ASSOC);
		if (DB::isError($DB_Query))
			throw new CachingStrategy_Exception("CacheToPEARDB_Strategy: ".$DB_Query->getMessage());
		
		if (count($DB_Query)==1) {
		
			$this->RecordExists=1;
			$CacheTime=(time()-$DB_Query[0][$this->Field_ImageModification])/60;		
			
			if ($CacheTime<$CacheForMinutes) {	
				
				header("Content-type: image/$CacheImageType");
				header("Cache-Control: no-cache");
				
				print($DB_Query[0][$this->Field_ImageData]);
				$this->PEAR_DB->disconnect();
				
				return 1;
			}
		}
		ob_start();
	}
	
	public function CacheImage() {
		$name=explode(".",basename($_SERVER["PHP_SELF"]));
		$CacheImage=$name[0].".cached";	
		
		if ($this->RecordExists)
			$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'";
		else	
			$Query="INSERT INTO ".$this->DB_Table."(".$this->Field_ImageName.",".$this->Field_ImageData.",".$this->Field_ImageModification.") VALUES ('$CacheImage','".addslashes(ob_get_contents())."',".time().")";
		
		$InsertQuery=$this->PEAR_DB->query($Query);
		if (DB::isError($InsertQuery))
			throw new CachingStrategy_Exception("CaheToPEARDB_Strategy: ".$InsertQuery->getMessage());
			
		$this->PEAR_DB->disconnect();
	}

}

?>
Return current item: phpchartPlus