Location: PHPKode > scripts > PHP DataCache > php-datacachea1/DataCacheDaoFile.php
<?php
/**
 *	DataCacheDaoFile class definiton
 *
 *	concrete file based DAO for the DataCache class
 *
 *	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: DataCacheDaoFile.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-26
 *	@package		DataCache
 *	@subpackage		DAO
 *	@version		$Id: DataCacheDaoFile.php,v 1.1.1.1 2003/12/11 05:22:10 jsweat Exp $
 */

/**
 *	location where cache files are kept
 *
 *	assumed in the rest of the code to be full path with a trailing slash
 */
define('DATACACHE_PATH', '/home/sweatje/phpc/cache/data/');

/**
 *	template for caching data as readable php code
 */
define('DATACACHE_TEMPLATE', <<<EOS
<?php
\$m_cache_data = unserialize(stripslashes('%s'));
#?>

EOS
);

/**
 *	template for caching data as zipped data
 */
define('DATACACHE_ZIP_TEMPLATE', <<<EOS
<?php
\$m_cache_data = unserialize(gzuncompress(base64_decode('%s')));
#?>

EOS
);

/**
 * concrete file based DAO for the DataCache class
 *
 * @package		DataCache
 * @subpackage	DAO
 */
class DataCacheDaoFile
{
	/**
	 * @access	private
	 * @var	string	$_msAppl		the name of the application
	 */
	var $_msAppl;
	
	/**
	 * constructor
	 *
	 * @param	string	$psAppl	the application identifier
	 * @return	void
	 */
	function DataCacheDaoFile($psAppl)
	{
		if (!is_dir(DATACACHE_PATH.$psAppl)) {
			umask(0007);
			mkdir(DATACACHE_PATH.$psAppl);
		}
		$this->_msAppl = $psAppl;
	}
	
	/**
	 * return the full path and file name for a cache file
	 *
	 * @access	private
	 * @param	string	$psId		identifier for the data being cached
	 * @return	string				the file path and name
	 */
	function _GetFileName($psId)
	{
		return DATACACHE_PATH.$this->_msAppl.'/'.$psId.'.php';
	}
	
	
	/**
	 * 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)
	{
		$s_file = $this->_GetFileName($psId);
		$s_template = ($pbZip) ? DATACACHE_ZIP_TEMPLATE : DATACACHE_TEMPLATE;
		$s_data = ($pbZip)	? base64_encode(gzcompress(serialize($pmValue))) 
							: addslashes(serialize($pmValue));
		umask(0117);
		$r_fh = fopen($s_file, 'w');
		flock($r_fh, LOCK_EX);
		$b_ret = fwrite($r_fh, 
						sprintf($s_template, 
								$s_data
								)
						);
		flock($r_fh, LOCK_UN);
		fclose($r_fh);
		return $b_ret;
	}
	
	/**
	 * 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)
	{
		return file_exists($this->_GetFileName($psId));
	}

	
	/**
	 * retrieve some cached data
	 *
	 * @param	string	$psId		identifier for the data being cached
	 * @return	mixed				the data cached
	 */
	function GetCache($psId)
	{
		if ($this->IsCached($psId)) {
			require $this->_GetFileName($psId);
			return $m_cache_data;
		} else {
			trigger_error("identifier '$psId' does not have a valid cache");
		}
		
	}
	
	/**
	 * 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->IsCached($psId)) {
			return filemtime($this->_GetFileName($psId));
		} else {
			trigger_error("identifier '$psId' does not have a valid cache");
		}
	}
	
	/**
	 * remove cached data if valid
	 *
	 * @param	string	$psId		identifier for the cached data
	 * @return	boolean				result of removal
	 */
	function RemoveCache($psId)
	{
		if ($this->IsCached($psId)) {
			return unlink($this->_GetFileName($psId));
		} else {
			trigger_error("identifier '$psId' does not have a valid cache");
		}
	}
	
	/**
	 * 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)
	{
		$i_del = 0;
		$s_re = '/^('.$psPattern.'\w*)\.php$/';

		$o_dir = dir(DATACACHE_PATH.$this->_msAppl.'/');
		while (false !== ($s_entry = $o_dir->read())) {
			preg_match($s_re, $s_entry, $a_match);
			if (2 == count($a_match)) {
				$b_ret = $this->RemoveCache($a_match[1]);
				if ($b_ret) {
					$i_del++;
				} else {
					trigger_error("Problem deleting cache id '{$a_match[1]}'");
				}
			}
		}
		$o_dir->close();

		if ($i_del) {
			return true;
		} else {
			trigger_error("identifier pattern '$psPattern' does not have any valid cached data");
			return false;
		}
	}
	
	
}


#?>
Return current item: PHP DataCache