ソースを参照

#5177 Content cache: cache implementation, profile switcher cache

Alex Trofimov 1 年間 前
親
コミット
e66fbcce6c

+ 4 - 0
inc/classes/BxDolAccountQuery.php

@@ -272,6 +272,10 @@ class BxDolAccountQuery extends BxDolDb implements iBxDolSingleton
         if ($bResult = $this->_updateField ($iID, 'profile_id', $iProfileId))
             $this->cleanMemory('BxDolProfileQuery::getCurrentProfileByAccount' . $iID);
 
+        // clean acount swither cache
+        bx_content_cache_del("profile_switcher_{$iID}_0");
+        bx_content_cache_del("profile_switcher_{$iID}_1");
+
         return $bResult;
     }
 

+ 10 - 1
inc/classes/BxDolProfileQuery.php

@@ -215,7 +215,16 @@ class BxDolProfileQuery extends BxDolDb implements iBxDolSingleton
      */
     public function changeAccountId ($iProfileId, $iAccountId)
     {
-        return $this->_updateField ($iProfileId, 'account_id', $iAccountId);
+        $aInfoOld = $this->getInfoById($iProfileId);
+        $bRet = $this->_updateField ($iProfileId, 'account_id', $iAccountId);
+        if ($bRet && $aInfoOld) {
+            // invalidate profile switcher cache for old and new account
+            bx_content_cache_del("profile_switcher_" . $aInfoOld['account_id'] . "_0");
+            bx_content_cache_del("profile_switcher_" . $aInfoOld['account_id'] . "_1");
+            bx_content_cache_del("profile_switcher_" . $iAccountId . "_0");
+            bx_content_cache_del("profile_switcher_" . $iAccountId . "_1");
+        }
+        return $bRet;
     }
 
     /**

+ 37 - 0
inc/utils.inc.php

@@ -2818,4 +2818,41 @@ function bx_get_context_workspace($bFromCache = true)
     return ($GLOBALS['bxWorkspaceContext'] = array_pop($a));
 }
 
+function bx_content_cache_get($sKey, $iTTL = false)
+{
+    // TODO: if (!getParam('sys_content_cache_enable'))
+    //    return null;
+
+    $oCache = bx_content_cache_obj();
+    return $oCache->getData('content_' . $sKey . bx_site_hash() . '.php', $iTTL);
+}
+
+function bx_content_cache_set($sKey, $mixedData, $iTTL = false)
+{
+    // TODO: if (!getParam('sys_content_cache_enable'))
+    //    return false;
+
+    $oCache = bx_content_cache_obj();
+    return $oCache->setData('content_' . $sKey . bx_site_hash() . '.php', $mixedData, $iTTL);
+}
+
+function bx_content_cache_del($sKey)
+{
+    $oCache = bx_content_cache_obj();
+    return $oCache->delData('content_' . $sKey . bx_site_hash() . '.php');
+}
+
+function bx_content_cache_obj()
+{
+    if (isset($GLOBALS['bxCacheContentObj']))
+        return $GLOBALS['bxCacheContentObj'];
+
+    $sEngine = 'File';// TODO: $this->getParam('sys_content_cache_engine');
+    $oCacheObject = bx_instance('BxDolCache' . $sEngine);
+    if(!$oCacheObject->isAvailable())
+        $oCacheObject = bx_instance('BxDolCacheFile');
+
+    return $GLOBALS['bxCacheContentObj'] = $oCacheObject;
+}
+
 /** @} */

+ 10 - 0
modules/base/profile/classes/BxBaseModProfileFormsEntryHelper.php

@@ -134,6 +134,10 @@ class BxBaseModProfileFormsEntryHelper extends BxBaseModGeneralFormsEntryHelper
 
         if(isset($CNF['FIELD_BIRTHDAY']) && isset($aContentInfo[$CNF['FIELD_BIRTHDAY']]))
             $oForm->addTrackFields($CNF['FIELD_BIRTHDAY'], $aContentInfo);
+        if(isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']]))
+            $oForm->addTrackFields($CNF['FIELD_TITLE'], $aContentInfo);
+        if(isset($CNF['FIELD_PICTURE']) && isset($aContentInfo[$CNF['FIELD_PICTURE']]))
+            $oForm->addTrackFields($CNF['FIELD_PICTURE'], $aContentInfo);
     }
 
     public function onDataEditAfter ($iContentId, $aContentInfo, $aTrackTextFieldsChanges, $oProfile, $oForm)
@@ -171,6 +175,12 @@ class BxBaseModProfileFormsEntryHelper extends BxBaseModGeneralFormsEntryHelper
         if(isset($CNF['FIELD_BIRTHDAY']) && $oForm->isTrackFieldChanged($CNF['FIELD_BIRTHDAY']))
             BxDolContentFilter::getInstance()->updateValuesByProfile($oProfile->getInfo());
 
+        // reset profile switcher cache
+        if ($oEditedProfile->id() !== bx_get_logged_profile_id() && $oForm->isTrackFieldChanged($CNF['FIELD_TITLE']) || $oForm->isTrackFieldChanged($CNF['FIELD_PICTURE'])) {
+            bx_content_cache_del("profile_switcher_" . $oEditedProfile->getAccountId() . "_0");
+            bx_content_cache_del("profile_switcher_" . $oEditedProfile->getAccountId() . "_1");
+        }
+
         return '';
     }
 

+ 27 - 6
template/scripts/BxBaseServiceProfiles.php

@@ -1162,16 +1162,29 @@ class BxBaseServiceProfiles extends BxDol
 
     public function serviceAccountProfileSwitcher ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = 0, $sButtonTitle = '', $sProfileTemplate = '')
     {
-    	$oTemplate = BxDolTemplate::getInstance();
+        if (!$iAccountId)
+            $iAccountId = getLoggedId();
 
         $oProfilesQuery = BxDolProfileQuery::getInstance();
-
-        $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId ? $iAccountId : getLoggedId());
+        $aProfiles = $oProfilesQuery->getProfilesByAccount($iAccountId);
         if (!$aProfiles)
             return false;
 
         if (null === $iActiveProfileId)
             $iActiveProfileId = bx_get_logged_profile_id();
+        
+        $oTemplate = null;
+        if (!$this->_bIsApi) {
+            $oTemplate = BxDolTemplate::getInstance();
+            $oTemplate->addCss('account.css');
+        }
+
+        $sCacheKey = "profile_switcher_{$iAccountId}_" . ($this->_bIsApi ? 1 : 0);
+        if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') { // cache is enabled only for default params (profile switcher in account popup)
+            $mixedCache = bx_content_cache_get($sCacheKey);
+            if ($mixedCache !== null)
+                return $mixedCache;
+        }
 
         $oModuleDb = BxDolModuleQuery::getInstance();
 
@@ -1247,13 +1260,21 @@ class BxBaseServiceProfiles extends BxDol
         }
 
         if($this->_bIsApi){
-            return [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
+            $a = [bx_api_get_block ('profile_switcher', ['active_profile_id' => $iActiveProfileId, 'profiles' => $aProfilesData])];
+            if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
+                bx_content_cache_set($sCacheKey, $a);
+            }
+            return $a;
         }
         
-        $oTemplate->addCss('account.css');
-        return array(
+        $a = array(
             'content' => $oTemplate->parseHtmlByName('profile_switch_row.html', $aVars),
         );
+        if ($sUrlProfileAction == '' && $bShowAll == 0 && $sButtonTitle == '' && $sProfileTemplate == '') {
+            echoDbgLog('set: ' . $sCacheKey);
+            bx_content_cache_set($sCacheKey, $a);
+        }
+        return $a;
     }
 
     public function serviceAccountProfileSwitcherAll ($iAccountId = false, $iActiveProfileId = null, $sUrlProfileAction = '', $bShowAll = true, $sButtonTitle = '', $sProfileTemplate = '')