Location: PHPKode > scripts > PHP DataCache > php-datacachea1/DataCache.php
<?php
/**
 *	DataCache class definiton
 *
 *	A PHP Class to implement server side data caching
 *	Copyright (C) 2003 Jason E. Sweat
 *
 *	This library is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU Lesser General Public
 *	License as published by the Free Software Foundation; either
 *	version 2.1 of the License, or (at your option) any later version.
 *
 *	This library is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *	Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public
 *	License along with this library; if not, write to the Free Software
 *	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *	Modification History: <pre>
 *	$Log: DataCache.php,v $
 *	Revision 1.1.1.1  2003/12/11 05:22:10  jsweat
 *	Initial check-in of PHP DataCache library.
 *	
 *	</pre>
 *	@author			Jason E. Sweat
 *	@since			2003-10-24
 *	@package		DataCache
 *	@subpackage		main
 *	@version		$Id: DataCache.php,v 1.1.1.1 2003/12/11 05:22:10 jsweat Exp $
 */

/**
 *	set the path to this file
 */
define('DATACACHE_LIB_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);

/**
 *	configuration setup for the DataCache library
 */
require_once DATACACHE_LIB_PATH.'DataCacheConfig.php';

if (false) { 
//just for phpdoc dependancy parsing
//actual include done inside the class for lazy loading, 
//i.e. ADOdb library does not need to be loaded an parsed 
//if you are using the file DAO
	/**
	 *	DataCacheDaoFile class definition
	 */
	require_once DATACACHE_LIB_PATH.'DataCacheDaoFile.php';
	/**
	 *	DataCacheDaoDb class definition
	 */
	require_once DATACACHE_LIB_PATH.'DataCacheDaoPgDb.php';
	/**
	 *	ADOdb library
	 *	{@link http://php.weblogs.com/ADOdb php.weblogs.com/ADOdb}
	 */
	require_once DATACACHE_ADODB_LIB_PATH.'adodb.inc.php';
}

/**
 * A PHP Class to implement server side data caching.
 *
 * <p>
 * This class does higher level functions like validation of the correct applicaiton
 * and data identifiers.
 * <p>
 * The choice of what underlying mechanism to use for storing the cached data is handled
 * as a factory method to choose a DataCacheDAO in the constructor.
 *
 * @package		DataCache
 * @subpackage	main
 */
class DataCache
{
	/**
	 * @private
	 * @var	object	$_moDao		DataCacheDAO	holds the dao object for this instance
	 */
	var $_moDao;
	
	/**
	 * constructor
	 *
	 * @param	string	$psAppl		Application identifier
	 * @param	integer	$piDaoType	DAO type election
	 * @return	void
	 */
	function DataCache($psAppl, $piDaoType=DATACACHE_DAOFILE)
	{
		if ($this->_ValidateAppl($psAppl)) {
			switch ($piDaoType) {
			case DATACACHE_DAOFILE:
				/**
				 *	DataCacheDaoFile class definition
				 */
				require_once DATACACHE_LIB_PATH.'DataCacheDaoFile.php';
				$this->_moDao =& new DataCacheDaoFile($psAppl);
				break;
			case DATACACHE_DAOPGDB:
				/**
				 *	DataCacheDaoDb class definition
				 */
				require_once DATACACHE_LIB_PATH.'DataCacheDaoPgDb.php';
				$o_conn =& $this->_CreateDbConn($piDaoType);
				$this->_moDao =& new DataCacheDaoPgDb($psAppl, $o_conn);
				break;
			default:
				trigger_error("Invalid DAO type '$piDaoType' for DataCache");
			}
		} else {
			trigger_error("Invalid Application identifier '$psAppl' for DataCache");
		}
	}
	
	/**
	 * create an ADOdb connection object for the appropriate database type
	 *
	 * @param	integer	$piDaoType	the dao type 
	 * @return	object				ADODbConnection
	 */
	function &_CreateDbConn($piDaoType)
	{
		switch ($piDaoType) {
		case DATACACHE_DAOPGDB:
			$s_db_type = 'postgres7';
			break;
		default:
			trigger_error("Invalid DAO type '$piDaoType' to create database connection");
			return false;
		}
		
		/**
		 *	ADOdb library
		 *	{@link http://php.weblogs.com/ADOdb php.weblogs.com/ADOdb}
		 */
		require_once DATACACHE_ADODB_LIB_PATH.'adodb.inc.php';
		
		$GLOBALS['ADODB_FETCH_MODE'] = ADODB_FETCH_ASSOC;
		
		$s_conn_funct = (DATACACHE_USE_PCONNECT) ? 'PConnect' : 'Connect';
		
		$o_conn =& NewADOConnection($s_db_type);
		$o_conn->$s_conn_funct(	 DATACACHE_DB_HOST
								,DATACACHE_DB_USER
								,DATACACHE_DB_PASSWD
								,DATACACHE_DB_NAME
								);
		return $o_conn;
	}
	
	
	/**
	 * desc
	 *
	 * @param	type	$name	desc
	 * @return	type		desc
	 */
	function _ValidateAppl($psAppl)
	{
		return preg_match(DATACACHE_APPL_RE, $psAppl);
	}
	
	/**
	 * desc
	 *
	 * @param	type	$name	desc
	 * @return	type		desc
	 */
	function _ValidateIdent($psIdent)
	{
		return preg_match(DATACACHE_IDENT_RE, $psIdent);
	}

	/**
	 * cache a particular piece of data
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @param	mixed	$pmValue	value to cache
	 * @param	boolean	$pbZip		optional - compress the data, defaults to true
	 * @return	boolean				sucess
	 */
	function SetCache($psId, $pmValue, $pbZip=true)
	{
		if ($this->_ValidateIdent($psId)) {
			return $this->_moDao->SetCache($psId, $pmValue, $pbZip);
		} else {
			trigger_error("Invalid Data Identifier '$psId'");
			return false;
		}
	}
	
	/**
	 * retrieve some cached data
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	mixed				the data cached
	 */
	function GetCache($psId)
	{
		if ($this->_ValidateIdent($psId)) {
			return $this->_moDao->GetCache($psId);
		} else {
			trigger_error("Invalid Data Identifier '$psId'");
			return false;
		}
	}
	
	/**
	 * remove cached data if valid
	 *
	 * @param	string	$psId		identifier for the cached data
	 * @return	boolean				result of removal
	 */
	function RemoveCache($psId)
	{
		if ($this->_ValidateIdent($psId)) {
			return $this->_moDao->RemoveCache($psId);
		} else {
			trigger_error("Invalid Data Identifier '$psId'");
			return false;
		}
	}
	
	/**
	 * remove a set cached data with identifiers matich a patter, if valid
	 *
	 * @param	string	$psPattern		the identifier pattern for the cached data
	 * @return	boolean					result of removal
	 */
	function RemovePatternCache($psPattern)
	{
		return $this->_moDao->RemovePatternCache($psPattern);
	}
	
	/**
	 * check to see if a cached data is valid
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	boolean				result of check
	 */
	function IsCached($psId)
	{
		if ($this->_ValidateIdent($psId)) {
			return $this->_moDao->IsCached($psId);
		} else {
			trigger_error("Invalid Data Identifier '$psId'");
			return false;
		}
	}
	
	/**
	 * retrieve time when data was cached
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	mixed				the data cached
	 */
	function GetCacheTime($psId)
	{
		if ($this->_ValidateIdent($psId)) {
			return $this->_moDao->GetCacheTime($psId);
		} else {
			trigger_error("Invalid Data Identifier '$psId'");
			return false;
		}
	}
	
}

#?>
Return current item: PHP DataCache