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

Ticket #5636 - API: 'Profile Counters' for current user slows down page loading.

AntonLV 1 месяц назад
Родитель
Сommit
4c2467a830

+ 17 - 12
inc/classes/BxDolLiveUpdates.php

@@ -150,21 +150,26 @@ class BxDolLiveUpdates extends BxDolFactory implements iBxDolSingleton
 
     	return $aResult;
     }
-    
-    
-    public function performApi()
+
+    public function performApi($bInit = false)
     {
-        $aRequested = $this->_getRequestedData(0, true);
+        $aCurrent = $this->_getCachedData($bInit);
+        if($bInit)
+            $aCurrent = array_intersect_key($aCurrent, array_flip($this->_aSystemsActive));
 
-        $aResult = [];
-        foreach($aRequested as $sName => $aData) {
-            if(is_array($aData))
-                $aResult[$sName] = isset($aData['count']) ? (int)$aData['count'] : 0;
-            else
-                $aResult[$sName] = (int)$aData;
-        }
+        $aRequested = $this->_getRequestedData(0, !$bInit, !$bInit ? $aCurrent : []);
 
-    	return $aResult;
+        $bUpdateCache = false;
+        foreach($aCurrent as $sName => &$iCount)
+            if(($iRequestedCount = (int)($aRequested[$sName]['count'] ?? 0)) && $iRequestedCount != (int)$iCount) {
+                $iCount = $iRequestedCount;
+                $bUpdateCache = true;
+            }
+
+        if($bUpdateCache)
+            $this->_updateCached('data', $aCurrent);
+
+        return $aCurrent;
     }
 
     protected function _addSystem($sName, $iFrequency, $sServiceCall, $mixedActive = true)

+ 2 - 2
inc/classes/BxDolProfile.php

@@ -204,8 +204,8 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         ];
         
         if ($iId == bx_get_logged_profile_id()){
-            $aRv['counters'] = bx_srv('system', 'profile_counters', [], 'TemplServiceProfiles');
-            
+            $aRv['counters'] = bx_srv('system', 'profile_counters', [defined('BX_API_PAGE')], 'TemplServiceProfiles');
+
             $oInformer = BxDolInformer::getInstance($oTemplate);
             $sRet = $oInformer ? $oInformer->display() : '';
             if ($sRet){

+ 3 - 3
template/scripts/BxBaseServiceProfiles.php

@@ -1448,10 +1448,10 @@ class BxBaseServiceProfiles extends BxDol
         return BxDolContentFilter::getInstance()->isEnabled();
     }
 
-    public function serviceProfileCounters()
+    public function serviceProfileCounters($bInit = false)
     {
-        if(bx_is_api() && ($oLiveUpdates = BxDolLiveUpdates::getInstance()) !== false)
-            return $oLiveUpdates->performApi();
+        if($this->_bIsApi && ($oLiveUpdates = BxDolLiveUpdates::getInstance()) !== false)
+            return $oLiveUpdates->performApi($bInit);
     }
     
     public function serviceProfileInfo()