namespace app\driver\session;
use app\common\model\Session;
use SessionHandler;
class Db extends SessionHandler
{
protected $db = null;
protected $config = [
'expire' => 3600, // 有效期(秒)
'session_name' => '', // sessionkey前缀
];
public function __construct($config = [])
{
$this->config = array_merge($this->config, $config);
}
public function open($savePath, $sessName){
$this->db = new Session();
}
public function close()
{
$this->db = null;
return true;
}
public function read($sessID)
{
$data = $this->db->find($this->config['session_name'] . $sessID);
return $data['session_data'];
}
public function write ($sessID, $sessData)
{
$sessID = $this->config['session_name'] . $sessID;
if (!$this->db->find($sessID)) {
$data = ['id' => $sessID, 'session_data' => $sessData, 'expire' => time() + $this->config['expire']];
$this->db->data($data)->save();
}else {
$this->db->save(['session_data' => $sessData , 'expire' => time() + $this->config['expire']], ['id'=>$sessID]);
}
}
public function destroy($sessID)
{
return $this->db->delete($this->config['session_name'] . $sessID);
}
public function gc($sessMaxLifeTime)
{
$old = time() - $sessMaxLifeTime;
return $this->db->where('expire', '<', $old)->delete();
return true;
}
}
最佳答案