Просмотр исходного кода

#104 - Improvements in live updates engine.

AntonLV 11 лет назад
Родитель
Сommit
5b0e4d6a33

+ 51 - 41
inc/classes/BxDolLiveUpdates.php

@@ -20,12 +20,11 @@ class BxDolLiveUpdates extends BxDol
 {
 	protected $_oQuery;
     protected $_aSystems;
+    protected $_aSystemsActive;
 
     protected $_iInterval;
 
-    protected $_sSessionKey;
-
-	protected $_iCacheTTL; // Note. Non-zero value may lead to cache destroying in wrong time.  
+	protected $_iCacheTTL;  
 	protected $_sCacheKey;
 
     protected $_sJsClass;
@@ -36,17 +35,17 @@ class BxDolLiveUpdates extends BxDol
         parent::__construct();
 
         $this->_oQuery = new BxDolLiveUpdatesQuery();
-        $this->_aSystems = $this->_oQuery->getSystems();
 
         $this->_iInterval = (int)$this->_oQuery->getParam('sys_live_updates_interval');
 
-        $this->_sSessionKey = 'bx_lu_index';
-        
-        $this->_iCacheTTL = 0;
+        $this->_iCacheTTL = 86400;
         $this->_sCacheKey = 'sys_live_updates_' . bx_get_logged_profile_id();
 
         $this->_sJsClass = 'BxDolLiveUpdates';
     	$this->_sJsObject = 'oLiveUpdates';
+
+    	$this->_aSystemsActive = array();
+    	$this->_aSystems = $this->_getCachedSystems();
     }
 
     /**
@@ -69,7 +68,11 @@ class BxDolLiveUpdates extends BxDol
      */
     public function perform()
     {
-		$iIndex = $this->_getIndex();
+		$iIndex = (int)bx_get('index');
+
+		$mixedSystemsActive = bx_get('systems_active');
+		if($mixedSystemsActive !== false)
+			$this->_aSystemsActive = $mixedSystemsActive;
 
 		$this->_aSystems = $this->_getCachedSystems();
     	if(empty($this->_aSystems) || !is_array($this->_aSystems))
@@ -116,32 +119,30 @@ class BxDolLiveUpdates extends BxDol
      */
     public function add($sName, $iFrequency, $sServiceCall, $bActive = true)
     {
-    	if(isset($this->_aSystems[$sName]))
-    		return false;
-
-    	$this->_aSystems[$sName] = array(
-    		'name' => $sName,
-    		'frequency' => $iFrequency,
-    		'service_call' => $sServiceCall,
-    		'active' => $bActive ? 1 : 0
-    	);
+    	if(!in_array($sName, $this->_aSystemsActive))
+    		$this->_aSystemsActive[] = $sName;
+
+    	if(empty($this->_aSystems[$sName])) {
+	    	$this->_aSystems[$sName] = array(
+	    		'name' => $sName,
+	    		'frequency' => $iFrequency,
+	    		'service_call' => $sServiceCall,
+	    		'active' => $bActive ? 1 : 0
+	    	);
+
+	    	$this->_updateCached('systems', $this->_aSystems);
+    	}
 
-    	$this->_updateCached('systems', $this->_aSystems);
+    	$mixedResponce = $this->_getRequestedDataBySystem($this->_aSystems[$sName]);
+    	if($mixedResponce !== false) {
+    		$aCachedData = $this->_getCachedData();
+    		$aCachedData[$sName] = $mixedResponce['count'];
+    		$this->_updateCached('data', $aCachedData);
+    	}
 
     	return true;
     }
 
-    protected function _getIndex()
-    {
-		bx_import('BxDolSession');
-    	$oSession = BxDolSession::getInstance();
-
-		$iIndex = (int)$oSession->getValue($this->_sSessionKey);
-		$oSession->setValue($this->_sSessionKey, ($iIndex + 1));
-
-		return $iIndex;
-    }
-
     protected function _getCacheInfo()
     {
     	return array(
@@ -178,7 +179,11 @@ class BxDolLiveUpdates extends BxDol
 
 	protected function _getCachedSystems()
     {
-    	return $this->_getCached('systems');
+    	$aSystems = $this->_getCached('systems');
+    	if(!empty($aSystems) && !empty($this->_aSystemsActive))
+    		$aSystems = array_intersect_key($aSystems, array_flip($this->_aSystemsActive));
+
+    	return $aSystems;
     }
 
     protected function _getCachedData()
@@ -200,29 +205,34 @@ class BxDolLiveUpdates extends BxDol
     {
     	$aResult = array();
 
-    	foreach($this->_aSystems as $aSystem) {
+    	foreach($this->_aSystems as $sName => $aSystem) {
     		if(empty($aSystem) || !is_array($aSystem) || (int)$aSystem['active'] != 1)
     			continue;
 
 			if($bIndexCheck && $iIndex % (int)$aSystem['frequency'] != 0)
 				continue;
 
-			if(!BxDolService::isSerializedService($aSystem['service_call']))
-				continue;
-
-			$aMarkers = array();
-			if(!empty($aCachedData) && isset($aCachedData[$aSystem['name']]))
-				$aMarkers = array('count' => (int)$aCachedData[$aSystem['name']]);
-
-			$aResponce = BxDolService::callSerialized($aSystem['service_call'], $aMarkers);
-			if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count'], $aResponce['method']))
+			$mixedResponce = $this->_getRequestedDataBySystem($aSystem, (!empty($aCachedData) && isset($aCachedData[$sName]) ? (int)$aCachedData[$sName] : 0));
+			if($mixedResponce === false)
 				continue;
 
-			$aResult[$aSystem['name']] = $aResponce;
+			$aResult[$sName] = $mixedResponce;
     	}
 
     	return $aResult;
     }
+
+    protected function _getRequestedDataBySystem($aSystem, $iCachedData = 0)
+    {
+		if(!BxDolService::isSerializedService($aSystem['service_call']))
+			return false;
+
+		$aResponce = BxDolService::callSerialized($aSystem['service_call'], array('count' => (int)$iCachedData));
+		if(empty($aResponce) || !is_array($aResponce) || !isset($aResponce['count'], $aResponce['method']))
+			return false;
+
+		return $aResponce;
+    }
 }
 
 /** @} */

+ 9 - 3
inc/js/classes/BxDolLiveUpdates.js

@@ -11,8 +11,10 @@ function BxDolLiveUpdates(oOptions)
 	this._sActionsUrl = oOptions.sActionsUrl == undefined ? sUrlRoot + 'live_updates.php' : oOptions.sActionsUrl;
 	this._sObjName = oOptions.sObjName == undefined ? 'oLiveUpdates' : oOptions.sObjName;
 	this._iInterval = oOptions.iInterval == undefined ? 3000 : oOptions.iInterval;
-	this._bServerRequesting = oOptions.bServerRequesting == undefined ? {} : oOptions.bServerRequesting;
+	this._aSystemsActive = oOptions.aSystemsActive == undefined ? {} : oOptions.aSystemsActive;
+	this._bServerRequesting = oOptions.bServerRequesting == undefined ? false : oOptions.bServerRequesting;
 
+	this._iIndex = 0;
 	this._iHandler = 0;
 	this._bBusy = false;
 
@@ -45,10 +47,12 @@ BxDolLiveUpdates.prototype.perform = function() {
 	var oDate = new Date();
 
 	this._bBusy = true;
-	
-    $.get(
+
+    $.post(
     	this._sActionsUrl,
         {
+    		systems_active: this._aSystemsActive,
+    		index: this._iIndex,
     		_t: oDate.getTime()
         },
         function(aData) {
@@ -65,6 +69,8 @@ BxDolLiveUpdates.prototype.perform = function() {
         },
         'json'
     );
+
+    this._iIndex += 1;
 };
 
 /** @} */

+ 1 - 1
template/scripts/BxBaseCmts.php

@@ -90,7 +90,7 @@ class BxBaseCmts extends BxDolCmts
 		$sServiceCall = BxDolService::getSerializedService('system', 'get_live_updates_comments', array($this->_sSystem, $this->_iId, $this->_getAuthorId(), '{count}'), 'TemplCmtsServices');
 
 		bx_import('BxDolLiveUpdates');
-		BxDolLiveUpdates::getInstance()->add($this->_sSystem . '_live_updates_cmts', 1, $sServiceCall);
+		BxDolLiveUpdates::getInstance()->add($this->_sSystem . '_live_updates_cmts_' . $this->_iId, 1, $sServiceCall);
 		//add live update
 
         $sCaption = _t('_cmt_block_comments_title', $this->getCommentsCount());

+ 1 - 0
template/scripts/BxBaseLiveUpdates.php

@@ -25,6 +25,7 @@ class BxBaseLiveUpdates extends BxDolLiveUpdates
         	'sActionsUrl' => BX_DOL_URL_ROOT . 'live_updates.php',
         	'sObjName' => $this->_sJsObject,
         	'iInterval' => $this->_iInterval,
+        	'aSystemsActive' => $this->_aSystemsActive,
         	'bServerRequesting' => !empty($this->_aSystems)
         ), $aParams);