/ Published in: SQL
caching session variables in Mysql using memory engine for fast client response.
note: please test before use, then use at your own risk.
note: please test before use, then use at your own risk.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** @author prgrmmr.aben [at] gmail (dot) com * http://fivesnippets.blogspot.com/2014/08/a-very-light-implementation-of-session.html * please give back a small donation if you find * this little educational snippet of code useful **/ CREATE TABLE IF NOT EXISTS `session` ( `id` INT(11) NOT NULL AUTO_INCREMENT, /*`refUser` int(11) DEFAULT NULL COMMENT 'references user number',*/ `IP` INT(39) DEFAULT NULL, `creation` datetime NOT NULL TIMESTAMP DEFAULT CURRENT_TIMESTAMP COMMENT 'creation time', `expiry` datetime DEFAULT NULL COMMENT 'expiry time', `secretToken` VARCHAR(10) NOT NULL, `type` enum('guest','client') NOT NULL DEFAULT 'guest', PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='don''t need to be a real session in memory' AUTO_INCREMENT=1 ; CREATE TABLE cachedSession ENGINE=MEMORY SELECT * FROM SESSION; CREATE DEFINER=`root`@`localhost` PROCEDURE `cacheSessions`() NO SQL BEGIN DELETE FROM cachedsession WHERE 1; INSERT INTO cachedsession SELECT * FROM SESSION WHERE `expiry`<NOW(); SELECT ROW_COUNT(); END$$ CREATE DEFINER=`root`@`localhost` PROCEDURE `cleanSession`() NO SQL COMMENT 'clean sessions expired @hours ago' BEGIN DELETE FROM `session` WHERE TIMESTAMPDIFF(MINUTE, expiry, NOW())>0; SELECT ROW_COUNT(); END$$