Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save you-think-you-are-special/6006037 to your computer and use it in GitHub Desktop.
Save you-think-you-are-special/6006037 to your computer and use it in GitHub Desktop.
<?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