Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steverobbins/1ad52f4ba86c4e07bb5b to your computer and use it in GitHub Desktop.
Save steverobbins/1ad52f4ba86c4e07bb5b to your computer and use it in GitHub Desktop.
Clean Magento's core_session table in background

Script to "safely" clean up the core_session table without causing lock wait timeouts.

Not thoroughly vetted but met the client's needs.

Requires disabling Mage_Core_Model_Resource_Session:gc(), e.g.

--- a/app/code/core/Mage/Core/Model/Resource/Session.php
+++ b/app/code/core/Mage/Core/Model/Resource/Session.php
@@ -74,7 +74,7 @@ class Mage_Core_Model_Resource_Session implements Zend_Session_SaveHandler_Inter
      *
      * @var int
      */
-    protected $_automaticCleaningFactor    = 50;
+    protected $_automaticCleaningFactor    = 0;
 
     /**
      * Constructor

local codepool override, etc.

Presented as a standalone script for usability but you get bonus points if wrapped in a shell (e.g. php shell/sessionGc.php).

<?php
include 'app/Mage.php';
umask(0);
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';
Mage::app($mageRunCode, $mageRunType);
$resource = Mage::getSingleton('core/resource');
$db = $resource->getConnection('core_write');
$sessionTable = $resource->getTableName('core/session');
$timestamp = Varien_Date::toTimestamp(true);
$batchSize = 100;
$stmt = $db->prepare(sprintf('SELECT COUNT(*) FROM %s WHERE session_expires < ?', $sessionTable));
$stmt->execute([$timestamp]);
$result = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
$count = isset($result[0]) ? ceil($result[0] / 10) * 10 : false;
if (!$count) {
echo 'No sessions need clearing.';
exit;
}
printf('Clearing ~%d expired sessions... ', $count);
for ($i = 1; $i * $batchSize <= $count; $i++) {
$stmt = $db->prepare(sprintf(
'SELECT session_id FROM %s WHERE session_expires < ? LIMIT %d',
$sessionTable,
$batchSize
));
$stmt->execute([$timestamp]);
// session_id's are indexed/faster
$ids = $stmt->fetchAll(Zend_Db::FETCH_COLUMN);
$stmt = $db->prepare(sprintf(
'DELETE FROM %s WHERE session_id in ("%s")',
$sessionTable,
implode('","', $ids)
));
$stmt->execute();
}
echo 'Done';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment