Some minor refactoring on session handler

This commit is contained in:
Diogo Cordeiro 2019-04-27 00:28:05 +01:00
parent 7845a09b34
commit 6cf674f8f8
2 changed files with 59 additions and 56 deletions

View File

@ -19,9 +19,11 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
class Session extends Managed_DataObject class Session extends Managed_DataObject
{ {
@ -39,25 +41,18 @@ class Session extends Managed_DataObject
public static function schemaDef() public static function schemaDef()
{ {
return array( return [
'fields' => array( 'fields' => [
'id' => array('type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'session ID'), 'id' => ['type' => 'varchar', 'length' => 32, 'not null' => true, 'description' => 'session ID'],
'session_data' => array('type' => 'text', 'description' => 'session data'), 'session_data' => ['type' => 'text', 'description' => 'session data'],
'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), 'created' => ['type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'],
'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), 'modified' => ['type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'],
), ],
'primary key' => array('id'), 'primary key' => ['id'],
'indexes' => array( 'indexes' => [
'session_modified_idx' => array('modified'), 'session_modified_idx' => ['modified'],
), ],
); ];
}
static function logdeb($msg)
{
if (common_config('sessions', 'debug')) {
common_debug("Session: " . $msg);
}
} }
static function open($save_path, $session_name) static function open($save_path, $session_name)
@ -87,6 +82,13 @@ class Session extends Managed_DataObject
} }
} }
static function logdeb($msg)
{
if (common_config('sessions', 'debug')) {
common_debug("Session: " . $msg);
}
}
static function write($id, $session_data) static function write($id, $session_data)
{ {
self::logdeb("Writing session '$id'"); self::logdeb("Writing session '$id'");
@ -136,36 +138,16 @@ class Session extends Managed_DataObject
} }
} }
static function destroy($id)
{
self::logdeb("Deleting session $id");
$session = Session::getKV('id', $id);
if (empty($session)) {
self::logdeb("Can't find '$id' to delete.");
} else {
$result = $session->delete();
if (!$result) {
common_log_db_error($session, 'DELETE', __FILE__);
self::logdeb("Failed to delete '$id'.");
} else {
self::logdeb("Successfully deleted '$id' (result = $result).");
}
return $result;
}
}
static function gc($maxlifetime) static function gc($maxlifetime)
{ {
self::logdeb("garbage collection (maxlifetime = $maxlifetime)"); self::logdeb("garbage collection (maxlifetime = $maxlifetime)");
$epoch = common_sql_date(time() - $maxlifetime); $epoch = common_sql_date(time() - $maxlifetime);
$ids = array(); $ids = [];
$session = new Session(); $session = new Session();
$session->whereAdd('modified < "'.$epoch.'"'); $session->whereAdd('modified < "' . $epoch . '"');
$session->selectAdd(); $session->selectAdd();
$session->selectAdd('id'); $session->selectAdd('id');
@ -192,6 +174,27 @@ class Session extends Managed_DataObject
} }
} }
static function destroy($id)
{
self::logdeb("Deleting session $id");
$session = Session::getKV('id', $id);
if (empty($session)) {
self::logdeb("Can't find '$id' to delete.");
return false;
} else {
$result = $session->delete();
if (!$result) {
common_log_db_error($session, 'DELETE', __FILE__);
self::logdeb("Failed to delete '$id'.");
} else {
self::logdeb("Successfully deleted '$id' (result = $result).");
}
return $result;
}
}
static function setSaveHandler() static function setSaveHandler()
{ {
self::logdeb("setting save handlers"); self::logdeb("setting save handlers");