Home >php教程 >php手册 >Thinkphp缓存代理CacheDelegate类处理redis

Thinkphp缓存代理CacheDelegate类处理redis

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 19:35:171256browse

Thinkphp缓存代理CacheDelegate类处理redis 无 /** * CacheDelegate() * * @param mixed $cacheHandler * @param mixed $cacheName * @param mixed $cacheParams * @param mixed $GetFunctionDataArray * @param mixed $GetFunctionParamArray * @param mixed

Thinkphp缓存代理CacheDelegate类处理redis
/**
 * CacheDelegate()
 *
 * @param mixed $cacheHandler
 * @param mixed $cacheName
 * @param mixed $cacheParams
 * @param mixed $GetFunctionDataArray
 * @param mixed $GetFunctionParamArray
 * @param mixed $DealFunctionDataArray
 * @param mixed $DealFunctionParamArray
 * @param mixed $UpdateCache
 * @return
 */
function CacheDelegate($cacheHandler, $CacheName,$cacheParams=array(), $GetFunctionDataArray = array(), $GetFunctionParamArray =
		array(), $DealFunctionDataArray = array(), $DealFunctionParamArray = array(), $UpdateCache = false)
{
	$cacheValue = '';
	if (isset($CacheName))
	{
		$cacheSetting = C('CACHETABLE');
		$cacheSetting =$cacheSetting[$CacheName];
		$cacheKey = vsprintf($cacheSetting['cacheKey'], $cacheParams );
		if (empty($cacheKey)){
			if (APP_STATUS=='DEV_config'){
				throw_exception("缓存配置文件cachetable.php中,$CacheName 的配置错误");
			}
			return null;
		}
		$expire = $cacheSetting['expire'] ? $cacheSetting['expire'] : null;

		$keyCached = $cacheHandler->exists($cacheKey);
		if ($keyCached){
			$cacheValue = $cacheHandler->get($cacheKey);
		}
		// keyCached为false, 表示缓存不存在, 需要重新读数据库;
		if ( !$keyCached || $UpdateCache)
		{

			if (isset($GetFunctionDataArray) && is_callable($GetFunctionDataArray))
			{
				$cacheValue = call_user_func_array($GetFunctionDataArray, $GetFunctionParamArray);

				if (isset($cacheValue)){
					$cacheHandler->set($cacheKey,$cacheValue,$expire);
				}
			}
		}
		
		if (isset($DealFunctionDataArray) && is_callable($DealFunctionDataArray))
		{
			$DealFunctionParamArray = array_merge(array($cacheValue), $DealFunctionParamArray);
			$TmpcacheValue = call_user_func_array($DealFunctionDataArray, $DealFunctionParamArray);

			if (isset($cacheValue))
			{
				$cacheHandler->set($cacheKey, $TmpcacheValue, $expire);
				$cacheValue = $TmpcacheValue;
			}
		}
	}
	return $cacheValue;
}


/**
 * 字符串截取,支持中文和其他编码
 * @static
 * @access public
 * @param string $str 需要转换的字符串
 * @param string $start 开始位置
 * @param string $length 截取长度
 * @param string $charset 编码格式
 * @param string $suffix 截断显示字符
 * @return string
 */
function msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) {
	if(function_exists("mb_substr"))
		$slice = mb_substr($str, $start, $length, $charset);
	elseif(function_exists('iconv_substr')) {
		$slice = iconv_substr($str,$start,$length,$charset);
		if(false === $slice) {
			$slice = '';
		}
	}else{
		$re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
		$re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
		$re['gbk']    = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
		$re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
		preg_match_all($re[$charset], $str, $match);
		$slice = join("",array_slice($match[0], $start, $length));
	}
	$fix='';
	if(strlen($slice) < strlen($str)){
		$fix='...';
	}
	return $suffix ? $slice.$fix : $slice;
}


/**
 * 获取来源IP
 * @return Ambigous <string, unknown>
 */
function get_ip(){
	if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
		$ip = getenv("HTTP_CLIENT_IP");
	else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
		$ip = getenv("HTTP_X_FORWARDED_FOR");
	else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
		$ip = getenv("REMOTE_ADDR");
	else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
		$ip = $_SERVER['REMOTE_ADDR'];
	else
		$ip = "unknown";
	return($ip);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:常用PHP日期处理函数Next article:计算工作日的天数