| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 | <?php/* * Manage data in cache *  * author: xuchangzhang *  * */require_once(ONU_ROOT . 'application/lib/data/Base.class.php');class CacheMg{		public static $Key_PromoteUserCnt    = 'CACHE_KEY_PROMOTE_USER_CNT';	public static $Key_UidIpPromCntLimit = 'CACHE_KEY_UID_IP_PROMOTE_CNT_LIMIT';	private static $memcache;	private static $db;		private static function getCacheIns(){				if(!isset(self::$memcache)){			global  $_ONU_CONFIG;			self::$memcache = new Mcache($_ONU_CONFIG['_memcache_conf']);		}		return 	self::$memcache;	}		private function getDbIns(){				if(!isset(self::$db)){			global  $_ONU_CONFIG;			self::$db = new MysqlHandler($_ONU_CONFIG['_db_conf']);				}		return self::$db;	}		public static function get($key){		$cache = self::getCacheIns();		return $cache->get($key);	}		public static function set($key,$val){		$cache = self::getCacheIns();		return $cache->set($key,$val);	}		public static function getPromoteUserCnt($uid){				$cache      = self::getCacheIns();		$userCntSet = $cache->get(self::$Key_PromoteUserCnt);				if(empty($userCntSet)){			$userCntSet = array();		}		else{			$userCntSet = json_decode($userCntSet,true);		}				$uNumber = $userCntSet[$uid];		// no data in cache get from db		if(empty($uNumber)){			$res = self::getPromoteUserCntDB($uid);			$uNumber = (is_array($res))? $res[0]['cnt'] : 0;			// update data to cache			$userCntSet[$uid] = $uNumber;			$cache->set(self::$Key_PromoteUserCnt, json_encode($userCntSet));		}						return $uNumber;	}		private static function getPromoteUserCntDB($uid){		$db  = self::getDbIns();		$effectiveLevel = EFFECTIVE_PLAYER_LEVEL;		$sql = "select count(id) cnt from onu_promote where uid='$uid' and player_grade =>$effectiveLevel;";		return $db->query($sql);	}		// set method must be called after get method	public static function setPromoteUserCnt($uid,$number){		$cache      = self::getCacheIns();		$userCntSet = $cache->get(self::$Key_PromoteUserCnt);		$userCntSet = json_decode($userCntSet,true);		if(is_array($userCntSet)){			$userCntSet[$uid] = $number;			$cache->set(self::$Key_PromoteUserCnt, json_encode($userCntSet));		}	}		//	private static function getDayTimeKey_BJ(){		date_default_timezone_set("Asia/Chongqing");		$today = date("Ymd");		return strtotime($today);		}		//	// one ip can registe at most 5 users for one promote user in 3 days	//	public static function setUidIpPrmoteCnt_Plus($uid,$ip,$num){				$cache   = self::getCacheIns();		$timeKey = self::getDayTimeKey_BJ();		$itemKey = "$uid-$ip";		$dataKey = '';				$dataSet = $cache->get(self::$Key_UidIpPromCntLimit);		$dataSet = json_decode($dataSet, true);				if(empty($dataSet)) $dataSet = array();		// set time period key		if(count($dataSet) == 0){			$dataSet[$timeKey] = array();			$dataKey = $timeKey;		}				// set new key and clear the first key		$oldPeriodKey = NULL;		foreach($dataSet as $key=>$val){						// over 3 days			if( ($timeKey - $key) >= 259200){				$oldPeriodKey = $key;			}			else{				$dataKey = $key;			}			break;		}				if(!empty($oldPeriodKey)){			$dataSet[$timeKey] = array();			$dataKey = $timeKey;			unset($dataSet[$oldPeriodKey]);		}				// set item value		if(empty($dataSet[$dataKey][$itemKey])){			$dataSet[$dataKey][$itemKey] = $num;		}		else{			$dataSet[$dataKey][$itemKey] ++;		}		// update cache		$cache->set(self::$Key_UidIpPromCntLimit,json_encode($dataSet));					}		public static function getUidIpPrmoteCnt($uid,$ip){				$cache   = self::getCacheIns();		$itemKey = "$uid-$ip";		$dataKey = '';				$dataSet = $cache->get(self::$Key_UidIpPromCntLimit);		$dataSet = json_decode($dataSet, true);				if(empty($dataSet)) return 0;				foreach($dataSet as $key=>$val){			$dataKey = $key;		}				return $dataSet[$dataKey][$itemKey];	}//Class end}
 |