Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.00 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @see Zend_Session
  4.  */
  5. require_once 'Zend/Session.php';
  6.  
  7. /**
  8.  * @see Zend_Config
  9.  */
  10. require_once 'Zend/Config.php';
  11.  
  12. /**
  13.  * @see Zend_Session_SaveHandler_Interface
  14.  */
  15. require_once 'Zend/Session/SaveHandler/Interface.php';
  16.  
  17. /**
  18.  * @see Zend_Session_SaveHandler_Exception
  19.  */
  20. require_once 'Zend/Session/SaveHandler/Exception.php';
  21.  
  22. /**
  23.  * PHPRedis save handler for Zend_Session
  24.  *
  25.  * @author Tigran Tokmajyan
  26.  * @package Redis
  27.  * @subpackage ZendFrameworkIntegration
  28.  * @version @package_version@
  29.  */
  30. class Toktik_Session_SaveHandler_Phpredis implements Zend_Session_SaveHandler_Interface
  31. {
  32.     /**
  33.      * Redis instance
  34.      *
  35.      * @var Redis
  36.      */
  37.     protected $_redis;
  38.  
  39.     /**
  40.      * Sessions set
  41.      *
  42.      * @var Rediska_Zend_Session_Set
  43.      */
  44.     protected $_set;
  45.  
  46.     /**
  47.      * Configuration
  48.      *
  49.      * @var array
  50.      */
  51.     protected $_options = array(
  52.         'keyPrefix' => 'PHPSESSIONS_',
  53.         'lifetime'  => 5000,
  54.     );
  55.  
  56.     /**
  57.      * Exception class name for options
  58.      *
  59.      * @var string
  60.      */
  61.     protected $_optionsException = 'Zend_Session_SaveHandler_Exception';
  62.  
  63.     /**
  64.      * Construct save handler
  65.      *
  66.      * @param Zend_Config|array $options
  67.      */
  68.     public function __construct($options = array())
  69.     {
  70.         if ($options instanceof Zend_Config) {
  71.             $options = $options->toArray();
  72.         }
  73.  
  74.         // set the instance of phpredis
  75.         if (!isset($options['instance'])) {
  76.             trigger_error(
  77.                 'Please set the instance of Redis.',
  78.                 E_USER_WARNING
  79.             );
  80.         } else {
  81.             $this->_redis = $options['instance'];
  82.         }
  83.  
  84.         // Set default lifetime
  85.         if (!isset($options['lifetime'])) {
  86.             $lifetime = (int)ini_get('session.gc_maxlifetime');
  87.  
  88.             if ($lifetime != 0) {
  89.                 $options['lifetime'] = $lifetime;
  90.             } else {
  91.                 trigger_error(
  92.                     "Please set session.gc_maxlifetime to enable garbage collection.",
  93.                     E_USER_WARNING
  94.                 );
  95.             }
  96.         }
  97.  
  98.         Toktik_Session_Set::setSaveHandler($this);
  99.         $this->_set = new Toktik_Session_Set();
  100.     }
  101.  
  102.     /**
  103.      * Open Session
  104.      *
  105.      * @param string $save_path
  106.      * @param string $name
  107.      * @return boolean
  108.      */
  109.     public function open($save_path, $name)
  110.     {
  111.         return true;
  112.     }
  113.  
  114.     /**
  115.      * Close session
  116.      *
  117.      * @return boolean
  118.      */
  119.     public function close()
  120.     {
  121.         return true;
  122.     }
  123.  
  124.     /**
  125.      * Read session data
  126.      *
  127.      * @param string $id
  128.      * @return string
  129.      */
  130.     public function read($id)
  131.     {
  132.         return $this->_redis->get($this->_getKeyName($id));
  133.     }
  134.  
  135.     /**
  136.      * Write session data
  137.      *
  138.      * @param string $id
  139.      * @param string $data
  140.      * @return boolean
  141.      */
  142.     public function write($id, $data)
  143.     {
  144.         try {
  145.             $timestamp = time();
  146.             $this->_set->add($id);
  147.         } catch (Redis_Exception $e) {
  148.             $this->_deleteSetOrThrowException($e);
  149.         }
  150.  
  151.         return $this->_redis->set(
  152.             $this->_getKeyName($id),
  153.             $data,
  154.             $this->getOption('lifetime')
  155.         );
  156.     }
  157.  
  158.     /**
  159.      * Destroy session
  160.      *
  161.      * @param string $id
  162.      * @return boolean
  163.      */
  164.     public function destroy($id)
  165.     {
  166.         try {
  167.             $this->_set->remove($id);
  168.         } catch(Redis_Exception $e) {
  169.             $this->_deleteSetOrThrowException($e);
  170.         }
  171.  
  172.         return $this->_redis->del($this->_getKeyName($id));
  173.     }
  174.  
  175.     /**
  176.      * Garbage Collection
  177.      *
  178.      * @param int $maxlifetime
  179.      * @return true
  180.      */
  181.     public function gc($maxlifetime)
  182.     {
  183.         try {
  184.             return $this->_set->remByScore(0, time() - $this->getOption('lifetime'));
  185.         } catch(Redis_Exception $e) {
  186.             $this->_deleteSetOrThrowException($e);
  187.         }
  188.     }
  189.  
  190.     /**
  191.      * Add prefix to session name
  192.      * @param string $id
  193.      * @return string
  194.      */
  195.     protected function _getKeyName($id)
  196.     {
  197.         return $this->getOption('keyPrefix') . $id;
  198.     }
  199.  
  200.     /**
  201.      * Delete old set or throw exception
  202.      *
  203.      * @throws Rediska_Connection_Exec_Exception
  204.      * @param Rediska_Connection_Exec_Exception $e
  205.      * @return void
  206.      */
  207.     protected function _deleteSetOrThrowException(Redis_Exception $e)
  208.     {
  209.         if ($e->getMessage() == 'Operation against a key holding the wrong kind of value') {
  210.             $this->_set->delete();
  211.         } else {
  212.             throw $e;
  213.         }
  214.     }
  215.  
  216.     /**
  217.      * Destructor
  218.      *
  219.      * @return void
  220.      */
  221.     public function __destruct()
  222.     {
  223.         Zend_Session::writeClose();
  224.     }
  225.  
  226.     public function getRedis() {
  227.         return $this->_redis;
  228.     }
  229.  
  230.     public function getOption($key) {
  231.         return $this->_options[$key];
  232.     }
  233. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement