Created
July 16, 2013 05:34
-
-
Save you-think-you-are-special/6006037 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class RedisSessionHandler implements \SessionHandlerInterface{ | |
public $ttl = 1800; // 30 minutes default | |
protected $db; | |
protected $prefix; | |
public function __construct(Predis\Client $db, $prefix = 'PHPSESSID:') { | |
$this->db = $db; | |
$this->prefix = $prefix; | |
} | |
public function open($savePath, $sessionName) { | |
// No action necessary because connection is injected | |
// in constructor and arguments are not applicable. | |
} | |
public function close() { | |
$this->db = null; | |
unset($this->db); | |
} | |
public function read($id) { | |
$id = $this->prefix . $id; | |
$sessData = $this->db->get($id); | |
$this->db->expire($id, $this->ttl); | |
return $sessData; | |
} | |
public function write($id, $data) { | |
$id = $this->prefix . $id; | |
$this->db->set($id, $data); | |
$this->db->expire($id, $this->ttl); | |
} | |
public function destroy($id) { | |
$this->db->del($this->prefix . $id); | |
} | |
public function gc($maxLifetime) { | |
// no action necessary because using EXPIRE | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment