Procházet zdrojové kódy

#834 3. optimize most frequently used queries

Alex Trofimov před 9 roky
rodič
revize
523e29c959

+ 15 - 3
inc/classes/BxDolAccount.php

@@ -12,6 +12,7 @@ bx_import('BxDolAcl');
 class BxDolAccount extends BxDolFactory implements iBxDolSingleton
 {
     protected $_iAccountID;
+    protected $_aInfo;
     protected $_oQuery;
 
     /**
@@ -76,7 +77,8 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
      */
     public function id()
     {
-        return $this->_oQuery->getIdById($this->_iAccountID);
+        $a = $this->getInfo($this->_iAccountID);
+        return isset($a['id']) ? $a['id'] : false;
     }
 
     /**
@@ -104,6 +106,7 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
             $this->sendConfirmationEmail($iId);
 
         if ($this->_oQuery->updateEmailConfirmed($isConfirmed, $iId)) {
+            $this->_aInfo = false;
             bx_alert('account', $isConfirmed ? 'confirm' : 'unconfirm', $iId);
             return true;
         }
@@ -125,6 +128,8 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
         if (!$this->_oQuery->updateCurrentProfile($iId, $iSwitchToProfileId))
             return false;
 
+        $this->_aInfo = false;
+            
         bx_alert('account', 'switch_context', $iId, $iSwitchToProfileId, array('profile_id_old' => $aInfo['profile_id']));
 
         return true;
@@ -158,8 +163,15 @@ class BxDolAccount extends BxDolFactory implements iBxDolSingleton
      * Get account info
      */
     public function getInfo($iAccountId = false)
-    {
-        return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
+    {        
+        if ($iAccountId && $iAccountId != $this->_iAccountID)
+            return $this->_oQuery->getInfoById((int)$iAccountId ? (int)$iAccountId : $this->_iAccountID);
+
+        if ($this->_aInfo)
+            return $this->_aInfo;
+
+        $this->_aInfo = $this->_oQuery->getInfoById($this->_iAccountID);
+        return $this->_aInfo;
     }
 
     /**

+ 11 - 2
inc/classes/BxDolAclQuery.php

@@ -130,6 +130,7 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
 
     function getActions($aParams, &$aItems, $bReturnCount = true)
     {
+        $sMemoryKey = '';
         $aMethod = array('name' => 'getAll', 'params' => array(0 => 'query'));
         $sSelectClause = $sJoinClause = $sWhereClause = $sGroupClause = $sOrderClause = $sLimitClause = "";
 
@@ -144,6 +145,7 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
                 );
 
                 $sWhereClause .= " AND `taa`.`Name` IN(" . $this->implode_escape($aParams['value']) . ") AND `taa`.`Module` = :module ";
+                $sMemoryKey = 'BxDolAclQuery::getActions' . $aParams['type'] . $aParams['module'] . $sWhereClause;
                 break;
 
             case 'by_names':
@@ -200,6 +202,13 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
                 `taa`.`DisabledForLevels` AS `disabled_for_levels`" . $sSelectClause . "
             FROM `sys_acl_actions` AS `taa` " . $sJoinClause . "
             WHERE 1 " . $sWhereClause . " " . $sGroupClause . " " . $sOrderClause . " " . $sLimitClause;
+
+        if ($sMemoryKey) {
+            array_unshift($aMethod['params'], $sMemoryKey, $aMethod['name']);
+            $aItems = call_user_func_array(array($this, 'fromMemory'), $aMethod['params']);
+            return $bReturnCount ? count($aItems) : !empty($aItems);
+        }
+
         $aItems = call_user_func_array(array($this, $aMethod['name']), $aMethod['params']);
 
         if(!$bReturnCount)
@@ -237,7 +246,7 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
             ORDER BY `sys_acl_levels_members`.DateStarts DESC
             LIMIT 1", $iTime, $iTime, $iProfileId);
 
-        return $this->getRow($sSql);
+        return $this->fromMemory('BxDolAclQuery::getLevelCurrent' . $iProfileId . $iTime, 'getRow', $sSql);
     }
 
     function getLevelByIdCached($iLevel)
@@ -268,7 +277,7 @@ class BxDolAclQuery extends BxDolDb implements iBxDolSingleton
             FROM `sys_acl_actions` AS `taa`
             LEFT JOIN `sys_acl_matrix` AS `tam` ON `tam`.`IDAction` = `taa`.`ID` AND `tam`.`IDLevel` = ?
             WHERE `taa`.`ID` = ?", $iMembershipId, $iActionId);
-        return $this->getRow($sQuery);
+        return $this->fromMemory('BxDolAclQuery::getAction' . $iMembershipId . $iActionId, 'getRow', $sQuery);
     }
 
     function getActionTrack($iActionId, $iProfileId)

+ 13 - 2
inc/classes/BxDolProfile.php

@@ -15,6 +15,7 @@ define('BX_PROFILE_ACTION_EXTERNAL', 2); ///< action peformed by external servic
 class BxDolProfile extends BxDolFactory implements iBxDolProfile
 {
     protected $_iProfileID;
+    protected $_aProfile;
     protected $_oQuery;
 
     /**
@@ -120,7 +121,8 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
      */
     public function id()
     {
-        return $this->_oQuery->getIdById($this->_iProfileID);
+        $aProfile = $this->getInfo($this->_iProfileID);
+        return isset($aProfile['id']) ? $aProfile['id'] : false;
     }
 
     /**
@@ -189,7 +191,14 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
      */
     public function getInfo($iProfileId = 0)
     {
-        return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
+        if ($iProfileId && $iProfileId != $this->_iProfileID)
+            return $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
+
+        if ($this->_aProfile)
+            return $this->_aProfile;
+        
+        $this->_aProfile = $this->_oQuery->getInfoById((int)$iProfileId ? $iProfileId : $this->_iProfileID);
+        return $this->_aProfile;
     }
 
     /**
@@ -461,6 +470,8 @@ class BxDolProfile extends BxDolFactory implements iBxDolProfile
         if (!$this->_oQuery->changeStatus($iProfileId, $sStatus))
             return false;
 
+        $this->_aProfile = array();
+
         // alert about status changing
         bx_alert('profile', $sAlertActionName, $iProfileId, false, array('action' => $iAction));
 

+ 1 - 1
inc/classes/BxDolProfileQuery.php

@@ -76,7 +76,7 @@ class BxDolProfileQuery extends BxDolDb implements iBxDolSingleton
     public function getProfileByContentAndType ($iContentId, $sType)
     {
         $sSql = $this->prepare("SELECT * FROM `sys_profiles` WHERE `content_id` = ? AND `type` = ?", $iContentId, $sType);
-        return $this->getRow($sSql);
+        return $this->fromMemory( 'BxDolProfileQuery::getProfileByContentAndType' . $iContentId . $sType, 'getRow', $sSql);
     }
 
     /**