Skip to content

Instantly share code, notes, and snippets.

@marcelaraujo
Last active December 15, 2015 16:09
Show Gist options
  • Save marcelaraujo/5287579 to your computer and use it in GitHub Desktop.
Save marcelaraujo/5287579 to your computer and use it in GitHub Desktop.
Zend Session on Redis
<?php
/**
* @see Zend_Session
*/
require_once 'Zend/Session.php';
/**
* @see Zend_Config
*/
require_once 'Zend/Config.php';
/**
* Zend_Session_SaveHandler_DbTable
*
* @category Zend
* @package Zend_Session
* @subpackage SaveHandler
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Common_Session_SaveHandler_Redis implements Zend_Session_SaveHandler_Interface {
/**
* @var Redis
*/
protected $_redis;
/**
* Session Key Prefix
*
* @var string
*/
protected $_keyPrefix = 'php:session';
/**
* Session lifetime
* By default this time is 1440 seconds (24 minutes)
* @var int
*/
protected $_lifetime = 1440;
/**
* Session name
*
* @var string
*/
protected $_sessionName = '';
/**
* Session save path
*
* @var string
*/
protected $_savePath = '';
/**
* Construct save handler
*
* @param Zend_Config|array $options
*/
public function __construct($options = array()) {
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
/**
* Setting default lifetime
*/
if (isset($options['lifetime'])) {
$lifetime = (int) $options['lifetime'];
unset($options['lifetime']);
} else {
$lifetime = (int) ini_get('session.gc_maxlifetime');
}
/**
* Checking if lifetime is valid
*/
if( $lifetime != 0 ) {
$this->_lifetime = $lifetime;
} else {
trigger_error( "Please set session.gc_maxlifetime to enable garbage collection.", E_USER_WARNING );
}
/**
* Checking extension avaliable
*/
if (!extension_loaded('redis')) {
Zend_Session_SaveHandler_Exception::throwException('The redis extension must be loaded for using this backend !');
}
}
/**
* Close session
*
* @return boolean
*/
public function close() {
return TRUE;
}
/**
* Destroy session
*
* @param string $id
* @return boolean
*/
public function destroy($id) {
$key = $this->_getKeyName($id);
if(!$this->_redis->exists($key)) {
return FALSE;
}
$this->_redis->del($key);
return TRUE;
}
/**
* Garbage Collection
*
* @param int $maxlifetime
* @return true
*/
public function gc($maxlifetime) {
return TRUE;
}
/**
* Open Session
*
* @param string $save_path
* @param string $name
* @return boolean
*/
public function open($save_path, $name) {
/**
* Setting session name
*/
$this->_sessionName = $name;
/**
* Setting save path
*/
$this->_savePath = $save_path;
try {
$this->_redis = new Redis();
$this->_redis->connect( empty($save_path) ? '127.0.0.1' : $save_path );
} catch(Exception $e) {
return FALSE;
}
return TRUE;
}
/**
* Read session data
*
* @param string $id
* @return mixed
*/
public function read($id) {
$key = $this->_getKeyName($id);
if($this->_redis->exists($key)) {
return $this->_redis->get($key);
}
return '';
}
/**
* Write session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data) {
$key = $this->_getKeyName($id);
$this->_redis->setex($key, $this->_lifetime, $data);
return TRUE;
}
/**
* Destructor
*
* @return void
*/
public function __destruct() {
Zend_Session::writeClose();
if(isset($this->_redis) && $this->_redis instanceof Redis ){
$this->_redis->close();
}
}
/**
* Add prefix to session name
* @param string $id
* @return string
*/
protected function _getKeyName($id) {
return "{$this->_keyPrefix}:{$this->_sessionName}:{$id}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment