Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * @see Zend_Session
- */
- require_once 'Zend/Session.php';
- /**
- * @see Zend_Config
- */
- require_once 'Zend/Config.php';
- /**
- * @see Zend_Session_SaveHandler_Interface
- */
- require_once 'Zend/Session/SaveHandler/Interface.php';
- /**
- * @see Zend_Session_SaveHandler_Exception
- */
- require_once 'Zend/Session/SaveHandler/Exception.php';
- /**
- * PHPRedis save handler for Zend_Session
- *
- * @author Tigran Tokmajyan
- * @package Redis
- * @subpackage ZendFrameworkIntegration
- * @version @package_version@
- */
- class Toktik_Session_SaveHandler_Phpredis implements Zend_Session_SaveHandler_Interface
- {
- /**
- * Redis instance
- *
- * @var Redis
- */
- protected $_redis;
- /**
- * Sessions set
- *
- * @var Rediska_Zend_Session_Set
- */
- protected $_set;
- /**
- * Configuration
- *
- * @var array
- */
- protected $_options = array(
- 'keyPrefix' => 'PHPSESSIONS_',
- 'lifetime' => 5000,
- );
- /**
- * Exception class name for options
- *
- * @var string
- */
- protected $_optionsException = 'Zend_Session_SaveHandler_Exception';
- /**
- * Construct save handler
- *
- * @param Zend_Config|array $options
- */
- public function __construct($options = array())
- {
- if ($options instanceof Zend_Config) {
- $options = $options->toArray();
- }
- // set the instance of phpredis
- if (!isset($options['instance'])) {
- trigger_error(
- 'Please set the instance of Redis.',
- E_USER_WARNING
- );
- } else {
- $this->_redis = $options['instance'];
- }
- // Set default lifetime
- if (!isset($options['lifetime'])) {
- $lifetime = (int)ini_get('session.gc_maxlifetime');
- if ($lifetime != 0) {
- $options['lifetime'] = $lifetime;
- } else {
- trigger_error(
- "Please set session.gc_maxlifetime to enable garbage collection.",
- E_USER_WARNING
- );
- }
- }
- Toktik_Session_Set::setSaveHandler($this);
- $this->_set = new Toktik_Session_Set();
- }
- /**
- * Open Session
- *
- * @param string $save_path
- * @param string $name
- * @return boolean
- */
- public function open($save_path, $name)
- {
- return true;
- }
- /**
- * Close session
- *
- * @return boolean
- */
- public function close()
- {
- return true;
- }
- /**
- * Read session data
- *
- * @param string $id
- * @return string
- */
- public function read($id)
- {
- return $this->_redis->get($this->_getKeyName($id));
- }
- /**
- * Write session data
- *
- * @param string $id
- * @param string $data
- * @return boolean
- */
- public function write($id, $data)
- {
- try {
- $timestamp = time();
- $this->_set->add($id);
- } catch (Redis_Exception $e) {
- $this->_deleteSetOrThrowException($e);
- }
- return $this->_redis->set(
- $this->_getKeyName($id),
- $data,
- $this->getOption('lifetime')
- );
- }
- /**
- * Destroy session
- *
- * @param string $id
- * @return boolean
- */
- public function destroy($id)
- {
- try {
- $this->_set->remove($id);
- } catch(Redis_Exception $e) {
- $this->_deleteSetOrThrowException($e);
- }
- return $this->_redis->del($this->_getKeyName($id));
- }
- /**
- * Garbage Collection
- *
- * @param int $maxlifetime
- * @return true
- */
- public function gc($maxlifetime)
- {
- try {
- return $this->_set->remByScore(0, time() - $this->getOption('lifetime'));
- } catch(Redis_Exception $e) {
- $this->_deleteSetOrThrowException($e);
- }
- }
- /**
- * Add prefix to session name
- * @param string $id
- * @return string
- */
- protected function _getKeyName($id)
- {
- return $this->getOption('keyPrefix') . $id;
- }
- /**
- * Delete old set or throw exception
- *
- * @throws Rediska_Connection_Exec_Exception
- * @param Rediska_Connection_Exec_Exception $e
- * @return void
- */
- protected function _deleteSetOrThrowException(Redis_Exception $e)
- {
- if ($e->getMessage() == 'Operation against a key holding the wrong kind of value') {
- $this->_set->delete();
- } else {
- throw $e;
- }
- }
- /**
- * Destructor
- *
- * @return void
- */
- public function __destruct()
- {
- Zend_Session::writeClose();
- }
- public function getRedis() {
- return $this->_redis;
- }
- public function getOption($key) {
- return $this->_options[$key];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement