From: Marcel Werk Date: Wed, 30 May 2012 16:02:14 +0000 (+0200) Subject: Small session system / cronjob optimization X-Git-Tag: 2.0.0_Beta_1~1094 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=05c21784f6b16778b466323fef634a9d485ff945;p=GitHub%2FWoltLab%2FWCF.git Small session system / cronjob optimization --- diff --git a/com.woltlab.wcf/cronjob.xml b/com.woltlab.wcf/cronjob.xml index 549c13d7da..c94b823ff0 100644 --- a/com.woltlab.wcf/cronjob.xml +++ b/com.woltlab.wcf/cronjob.xml @@ -28,10 +28,10 @@ - wcf\system\cronjob\CleanUpCronjobLogCronjob - Deletes old entries from cronjob log + wcf\system\cronjob\DailyCleanUpCronjob + Daily Cleanup 0 - 12 + 1 * * * @@ -41,29 +41,29 @@ - wcf\system\cronjob\CleanUpSessionLogCronjob - Deletes old entries from session log + wcf\system\cronjob\HourlyCleanUpCronjob + Hourly Cleanup 0 - 10 + * * * * 1 - 0 - 0 + 1 + 1 - wcf\system\cronjob\CleanupListenerCronjob - Executes the cleanup system - 5,20,35,50 + wcf\system\cronjob\SessionCleanUpCronjob + Deletes expired sessions + */30 * * * * 1 - 0 - 0 + 1 + 1 \ No newline at end of file diff --git a/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php b/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php index 9c90afb88d..87a305d37e 100644 --- a/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php +++ b/wcfsetup/install/files/lib/data/acp/session/ACPSessionEditor.class.php @@ -1,6 +1,7 @@ $userIDs + */ + public static function deleteUserSessions(array $userIDs = array()) { + $conditionBuilder = new PreparedStatementConditionBuilder(); + if (count($userIDs)) $conditionBuilder->add('userID IN (?)', array($userIDs)); + + $sql = "DELETE FROM ".call_user_func(array(static::$baseClass, 'getDatabaseTableName'))." + ".$conditionBuilder; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute($conditionBuilder->getParameters()); + } + + /** + * Deletes the expired sessions. + * + * @param integer $timestamp + */ + public static function deleteExpiredSessions($timestamp) { + $sql = "DELETE FROM ".call_user_func(array(static::$baseClass, 'getDatabaseTableName'))." + WHERE lastActivityTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array($timestamp)); + } } diff --git a/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php new file mode 100644 index 0000000000..a0d183b465 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/AbstractCronjob.class.php @@ -0,0 +1,23 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +abstract class AbstractCronjob implements ICronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + EventHandler::getInstance()->fireAction($this, 'execute'); + } +} diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php deleted file mode 100644 index 7d69381b24..0000000000 --- a/wcfsetup/install/files/lib/system/cronjob/CleanUpCronjobLogCronjob.class.php +++ /dev/null @@ -1,28 +0,0 @@ - - * @package com.woltlab.wcf - * @subpackage system.cronjob - * @category Community Framework - */ -class CleanUpCronjobLogCronjob implements ICronjob { - /** - * @see wcf\system\ICronjob::execute() - */ - public function execute(Cronjob $cronjob) { - $sql = "DELETE FROM wcf".WCF_N."_cronjob_log - WHERE execTime < ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array( - (TIME_NOW - (86400 * 7)) - )); - } -} diff --git a/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php deleted file mode 100644 index 13a0d844de..0000000000 --- a/wcfsetup/install/files/lib/system/cronjob/CleanUpSessionLogCronjob.class.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @package com.woltlab.wcf - * @subpackage system.cronjob - * @category Community Framework - */ -class CleanUpSessionLogCronjob implements ICronjob { - /** - * @see wcf\system\ICronjob::execute() - */ - public function execute(Cronjob $cronjob) { - // delete access log - $sql = "DELETE FROM wcf".WCF_N."_acp_session_access_log - WHERE sessionLogID IN ( - SELECT sessionLogID - FROM wcf".WCF_N."_acp_session_log - WHERE lastActivityTime < ? - )"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array( - (TIME_NOW - (86400 * 30)) - )); - - // delete session log - $sql = "DELETE FROM wcf".WCF_N."_acp_session_log - WHERE lastActivityTime < ?"; - $statement = WCF::getDB()->prepareStatement($sql); - $statement->execute(array( - (TIME_NOW - (86400 * 30)) - )); - } -} diff --git a/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php new file mode 100644 index 0000000000..6b87d66308 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/DailyCleanUpCronjob.class.php @@ -0,0 +1,51 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +class DailyCleanUpCronjob extends AbstractCronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + // clean up cronjob log + $sql = "DELETE FROM wcf".WCF_N."_cronjob_log + WHERE execTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + (TIME_NOW - (86400 * 7)) + )); + + // clean up session access log + $sql = "DELETE FROM wcf".WCF_N."_acp_session_access_log + WHERE sessionLogID IN ( + SELECT sessionLogID + FROM wcf".WCF_N."_acp_session_log + WHERE lastActivityTime < ? + )"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + (TIME_NOW - (86400 * 30)) + )); + + // clean up session log + $sql = "DELETE FROM wcf".WCF_N."_acp_session_log + WHERE lastActivityTime < ?"; + $statement = WCF::getDB()->prepareStatement($sql); + $statement->execute(array( + (TIME_NOW - (86400 * 30)) + )); + } +} diff --git a/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php new file mode 100644 index 0000000000..f27d803674 --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/HourlyCleanUpCronjob.class.php @@ -0,0 +1,25 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +class HourlyCleanUpCronjob extends AbstractCronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + // TODO + } +} diff --git a/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php b/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php new file mode 100644 index 0000000000..c59052ecea --- /dev/null +++ b/wcfsetup/install/files/lib/system/cronjob/SessionCleanUpCronjob.class.php @@ -0,0 +1,28 @@ + + * @package com.woltlab.wcf + * @subpackage system.cronjob + * @category Community Framework + */ +class SessionCleanUpCronjob extends AbstractCronjob { + /** + * @see wcf\system\cronjob\ICronjob::execute() + */ + public function execute(Cronjob $cronjob) { + parent::execute($cronjob); + + ACPSessionEditor::deleteExpiredSessions(TIME_NOW - SESSION_TIMEOUT); + SessionEditor::deleteExpiredSessions(TIME_NOW - SESSION_TIMEOUT); + } +} diff --git a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php index 7a368ee042..1f2b71088b 100644 --- a/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php +++ b/wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php @@ -29,14 +29,14 @@ class ACPSessionFactory { // call shouldInit event if (!defined('NO_IMPORTS')) { - EventHandler::getInstance()->fireAction($this, 'shouldInit'); + EventHandler::getInstance()->fireAction($this, 'beforeInit'); } $this->init(); // call didInit event if (!defined('NO_IMPORTS')) { - EventHandler::getInstance()->fireAction($this, 'didInit'); + EventHandler::getInstance()->fireAction($this, 'afterInit'); } } diff --git a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php index ca1f6a1ee5..d108e0bd62 100644 --- a/wcfsetup/install/files/lib/system/session/SessionHandler.class.php +++ b/wcfsetup/install/files/lib/system/session/SessionHandler.class.php @@ -333,11 +333,18 @@ class SessionHandler extends SingletonFactory { $this->user = new User(null); } + if ($this->user->userID != 0) { + // user is no guest + // delete all other sessions of this user + call_user_func(array($this->sessionEditorClassName, 'deleteUserSessions', array($this->user->userID))); + } + // save session $this->session = call_user_func(array($this->sessionEditorClassName, 'create'), array( 'sessionID' => $sessionID, 'packageID' => PACKAGE_ID, 'userID' => $this->user->userID, + 'username' => $this->user->username, 'ipAddress' => UserUtil::getIpAddress(), 'userAgent' => UserUtil::getUserAgent(), 'lastActivityTime' => TIME_NOW, @@ -460,7 +467,7 @@ class SessionHandler extends SingletonFactory { $sessionTable = call_user_func(array($this->sessionClassName, 'getDatabaseTableName')); if ($user->userID) { - // user is not a gest, delete all other sessions of this user + // user is not a guest, delete all other sessions of this user $sql = "SELECT sessionID FROM ".$sessionTable." WHERE sessionID <> ?