为了账号安全,请及时绑定邮箱和手机立即绑定

php把session存储在数据库性能如何啊?

php把session存储在数据库性能如何啊?

PHP
慕无忌1623718 2019-03-18 09:00:19
SessionHandler.php文件内容如下 <?php namespace Dry\Base; class SessionHandler{ private $pdo = null; private $lifeTime = 1440; public function __construct($pdo) { $this->pdo = $pdo; $this->lifeTime = get_cfg_var('session.gc_maxlifetime'); session_set_save_handler( array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc') ); register_shutdown_function('session_write_close'); } public function open($save_path, $session_name) { return true; } public function close() { return true; } public function read($session_id) { $time = date('Y-m-d H:i:s'); $sql = "select session_data from session_handler where session_id='{$session_id}' and session_expires>'{$time}' limit 1"; $sm = $this->pdo->prepare($sql); $sm->execute(); if($sm->rowCount()){ $result = $sm->fetch(); return $result['session_data']; } else{ return ''; } } public function write($session_id, $session_data) { $time = date('Y-m-d H:i:s', time()+$this->lifeTime); $sql = "select count(1) as total from session_handler where session_id='{$session_id}' limit 1"; $sm = $this->pdo->prepare($sql); $sm->execute(); if($sm->fetchColumn()){ $sql="update session_handler set session_data='{$session_data}',session_expires='{$time}' where session_id='{$session_id}'"; } else{ $sql="insert into session_handler(session_id,session_data,session_expires) values('{$session_id}','{$session_data}','{$time}')"; } $sm = $this->pdo->prepare($sql); $sm->execute(); return true; } public function destroy($session_id) { $sql = "delete from session_handler where session_id='{$session_id}'"; $sm = $this->pdo->prepare($sql); $sm->execute(); return true; } public function gc($maxlifetime) { $time = date('Y-m-d H:i:s', time()-$maxlifetime); $sql = "delete from session_handler where session_expires<'{$time}'"; $sm = $this->pdo->prepare($sql); $sm->execute(); return true; } } ?> test.php文件内容如下 <?php require_once('SessionHandler.php'); use Dry\Base\SessionHandler; $pdo = new PDO('mysql:host=192.168.1.20;dbname=test;port=3306', 'root', '1234', array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_PERSISTENT => false, PDO::MYSQL_ATTR_INIT_COMMAND => 'set names utf8', PDO::ATTR_ORACLE_NULLS => PDO::NULL_TO_STRING, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC )); new SessionHandler($pdo); session_start(); $_SESSION['name'] = 'test'; echo $_SESSION['name']; ?> session_handler表结构如下 CREATE TABLE `session_handler` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `session_id` varchar(64) NOT NULL, `session_data` mediumtext, `session_expires` datetime NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `session_id` (`session_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
查看完整描述

5 回答

?
暮色呼如

TA贡献1853条经验 获得超9个赞

不推荐存数据库中,假如站点访问量大的时候,对数据库会频繁写入,频繁读取,从而影响数据库性能。一般用redis memcache存更好些。而且维护过期时间等也方便。设置key过期时间就好。

查看完整回答
反对 回复 2019-03-18
?
一只名叫tom的猫

TA贡献1906条经验 获得超3个赞

存redis这些缓存应用就可以了,读取高效,自动过期

查看完整回答
反对 回复 2019-03-18
  • 5 回答
  • 0 关注
  • 446 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信