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

#4646 Session optimization: shorter session idle time for guests

Alex Trofimov 1 год назад
Родитель
Сommit
61402f6c28
2 измененных файлов с 9 добавлено и 5 удалено
  1. 3 1
      inc/classes/BxDolSession.php
  2. 6 4
      inc/classes/BxDolSessionQuery.php

+ 3 - 1
inc/classes/BxDolSession.php

@@ -9,6 +9,8 @@
 
 if (!defined('BX_DOL_SESSION_IDLE_TIMEOUT'))
     define('BX_DOL_SESSION_IDLE_TIMEOUT', 1*24*60*60);
+if (!defined('BX_DOL_SESSION_IDLE_TIMEOUT_GUESTS'))
+    define('BX_DOL_SESSION_IDLE_TIMEOUT_GUESTS', 4*60*60);
 if (!defined('BX_DOL_SESSION_SKIP_UPDATE'))
     define('BX_DOL_SESSION_SKIP_UPDATE', 30);
 define('BX_DOL_SESSION_COOKIE', 'memberSession');
@@ -153,7 +155,7 @@ class BxDolSession extends BxDolFactory implements iBxDolSingleton
 
 		$this->sId = $mixedSession['id'];
 		$this->iUserId = (int)$mixedSession['user_id'];
-        $this->iTTL = (int)$mixedSession['ttl'];
+        $this->iTTL = isset($mixedSession['ttl']) ? (int)$mixedSession['ttl'] : 0;
 		$this->aData = unserialize($mixedSession['data']);
 
 		$this->oDb->update($this->sId);		//--- update session's time

+ 6 - 4
inc/classes/BxDolSessionQuery.php

@@ -26,7 +26,7 @@ class BxDolSessionQuery extends BxDolDb
     }
     function exists($sId)
     {
-        $aSession = $this->getRow("SELECT `id`, `user_id`, `ttl`, `data` FROM `" . $this->sTable . "` WHERE `id`=:id LIMIT 1", [
+        $aSession = $this->getRow("SELECT * FROM `" . $this->sTable . "` WHERE `id`=:id LIMIT 1", [
             'id' => (string)$sId
         ]);
 
@@ -69,14 +69,16 @@ class BxDolSessionQuery extends BxDolDb
     }
     function deleteExpired()
     {
-        $iTime = time() - BX_DOL_SESSION_IDLE_TIMEOUT;
-        $aRows = $this->getAll("SELECT `user_id`, `date` FROM `" . $this->sTable . "` WHERE ((`date` < :time AND `ttl` = 0) OR (`ttl` != 0 AND `ttl` < :now)) AND `user_id` != 0 ORDER BY `date` DESC LIMIT 50000", ['time' => $iTime, 'now' => time()]);
+        $iIdleUser = time() - BX_DOL_SESSION_IDLE_TIMEOUT;
+        $iIdleGuest = time() - BX_DOL_SESSION_IDLE_TIMEOUT_GUESTS;
+        $aRows = $this->getAll("SELECT `user_id`, `date` FROM `" . $this->sTable . "` WHERE ((`date` < :time AND `ttl` = 0) OR (`ttl` != 0 AND `ttl` < :now)) AND `user_id` != 0 ORDER BY `date` DESC LIMIT 50000", ['time' => $iIdleUser, 'now' => time()]);
 
         foreach ($aRows as $aRow) {
             $this->updateLastActivityAccount($aRow['user_id'], $aRow['date']);
         }
         
-        $iRet = (int)$this->query("DELETE FROM `" . $this->sTable . "` WHERE `date` < :time AND `ttl` = 0", ['time' => $iTime]);
+        $iRet = (int)$this->query("DELETE FROM `" . $this->sTable . "` WHERE `date` < :time AND `ttl` = 0 AND `user_id` = 0", ['time' => $iIdleGuest]);
+        $iRet += (int)$this->query("DELETE FROM `" . $this->sTable . "` WHERE `date` < :time AND `ttl` = 0 AND `user_id` != 0", ['time' => $iIdleUser]);
         $iRet += (int)$this->query("DELETE FROM `" . $this->sTable . "` WHERE `ttl` != 0 AND `ttl` < :now", ['now' => time()]);
         if ($iRet > 10)
             $this->query("OPTIMIZE TABLE `" . $this->sTable . "`");