Memcache.class.php 2.96 KB
<?php
/**
 * 传入options项目结构为
 * [config] = 配置文件名
 * [domain] = 文件存储域
 */
class Util_Server_Adapter_Cache_Memcache implements Util_Server_Adapter_Interface {
	const SECTION = 'memcache'; //配置文件中有关cache的配置段
	const PRENAME = 'servers'; //cache配置段中的名称
	CONST TIMEOUT = 20;

	private $_config = '';
	
	/**
	 * 创建函数
	 *
	 */
	public function __construct($options = null) {
		if (! extension_loaded ( 'memcache' ))
			throw new Util_Server_Exception ( get_class ( $this ) . ':load memcache module first please!' );
		$this->_config = Util_Common_Core::loadCacheMem ();
	}
	
	public function __destruct() {
	}
	
	/**
	 * 得到可用文件服务器的配置host, port列表
	 * 使用:$mem = new Memcache.class();$mem->addServer(host, port)方式生成对象。
	 *
	 * @return array or false
	 */
	public function loadBalanceServer() {
		$return = $this->loadServers ();
		return empty ( $return ) ? false : $return;
	}
	
	/**
	 * 读取Mogile服务器列表,配置中格式:定义再mogilefs段,格式为:192.168.4.133:6001,192.168.4.134:6001
	 *
	 * @param string $filename
	 * @return array
	 */
	public function loadServers() {
		$svrobj = Util_Common_Core::loadConfig ( $this->_config, self::SECTION );
		return $this->_getServersFromString ( str_replace ( ' ', '', $svrobj->{self::PRENAME} ) );
	}
	
	/**
	 * 检查服务器是否正常
	 *
	 * @param string $host 192.168.4.133
	 * @return true or string true 代表正常字符串为出错信息 
	 */
	private function _checkServer($host, $port) {
		$errno = 0;
		$errstr = '';
		$fs = @fsockopen ( $host, $port, $errno, $errstr, self::TIMEOUT );
		if (is_resource($fs)) {
			@fclose ( $fs );
			return true;
		} else {
//		    $log = sprintf("%s:%s memcache down at time: %s\n", $host, $port, date('Y-m-d H:i:s',time()));
//            @file_put_contents ( dirname ( __FILE__ ) . '/memcache.down.log', $log);
//            @file_put_contents('/tmp/logs/memcache.down.log', $log);
			return false;
		}
	}
	
	/**
	 * 分析取得的服务器字符串,得到服务器配置数组
	 *
	 * @param string $svrstring
	 * @return array
	 */
	private function _getServersFromString($svrstring) {
		if ($svrstring == '')
			throw new Util_Server_Exception ( get_class ( $this ) . ':服务器配置字符串为空!' );
		$tmpary = array_unique ( explode ( ',', $svrstring ) );
		if (count ( $tmpary ) == 0)
			throw new Util_Server_Exception ( get_class ( $this ) . ':服务器配置字符串格式错误' );
		$return = array ();
		$counter = count ( $tmpary );
		for($i = 0; $i < $counter; $i ++) {
			if (($tmp = parse_url ( $tmpary [$i] )) !== false && $this->_checkSvrAry ( $tmp ))
				$return [] = $tmp;
		}
		return $return;
	}
	
	/**
	 * 测试服务器配置数组是否格式正常
	 *
	 * @param array $ary
	 * @return boolean
	 */
	private function _checkSvrAry(array $ary) {
		return isset ( $ary ['host'] ) && isset ( $ary ['port'] ) ? $this->_checkServer($ary['host'], $ary['port']): false;
	}
}