1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
| <?php
/** * +---------------------------------------------------------------------- * | ThinkPHP [ WE CAN DO IT JUST THINK IT ] * +---------------------------------------------------------------------- * | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved. * +---------------------------------------------------------------------- * | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) * +---------------------------------------------------------------------- */
namespace Think\Session\Driver;
/** * Redis Session驱动 */ class Redis { /** * Redis句柄 */ private $handler; private $get_result;
public function __construct(){ if ( !extension_loaded('redis') ) { E(L('_NOT_SUPPERT_').':redis'); } if(empty($options)) { $options = array ( 'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1', 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : 6379, 'timeout' => C('REDIS_TIMEOUT') ? C('REDIS_TIMEOUT') : false, 'persistent' => C('REDIS_PERSISTENT') ? C('REDIS_PERSISTENT') : false, 'auth' => C('REDIS_AUTH') ? C('REDIS_AUTH') : false, ); } $options['host'] = explode(',', $options['host']); $options['port'] = explode(',', $options['port']); $options['auth'] = explode(',', $options['auth']); foreach ($options['host'] as $key=>$value) { if (!isset($options['port'][$key])) { $options['port'][$key] = $options['port'][0]; } if (!isset($options['auth'][$key])) { $options['auth'][$key] = $options['auth'][0]; } } $this->options = $options; $expire = C('SESSION_EXPIRE'); $this->options['expire'] = isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');; $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('SESSION_PREFIX'); $this->handler = new \Redis; }
/** * 连接Redis服务端 * @access public * @param bool $is_master : 是否连接主服务器 */ public function connect($is_master = true) { if ($is_master) { $i = 0; } else { $count = count($this->options['host']); if ($count == 1) { $i = 0; } else { $i = rand(1, $count - 1); //多个从服务器随机选择 } } $func = $this->options['persistent'] ? 'pconnect' : 'connect'; try { if ($this->options['timeout'] === false) { $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i]); if (!$result) throw new \Think\Exception('Redis Error', 100); } else { $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i], $this->options['timeout']); if (!$result) throw new \Think\Exception('Redis Error', 101); } if ($this->options['auth'][$i]) { $result = $this->handler->auth($this->options['auth'][$i]); if (!$result) { throw new \Think\Exception('Redis Error', 102); } } } catch ( \Exception $e ) { exit('Error Message:'.$e->getMessage().'<br>Error Code:'.$e->getCode().''); } } /** +---------------------------------------------------------- * 打开Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $savePath * @param mixed $sessName +---------------------------------------------------------- */ public function open($savePath, $sessName) { return true; } /** +---------------------------------------------------------- * 关闭Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- */ public function close() { if ($this->options['persistent'] == 'pconnect') { $this->handler->close(); } return true; }
/** +---------------------------------------------------------- * 读取Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID +---------------------------------------------------------- */ public function read($sessID) { $this->connect(0); $this->get_result = $this->handler->get($this->options['prefix'].$sessID); //延长有效期 $this->handler->expire($this->options['prefix'].$sessID,C('SESSION_EXPIRE')); return $this->get_result; }
/** +---------------------------------------------------------- * 写入Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID * @param String $sessData +---------------------------------------------------------- */ public function write($sessID, $sessData) { if (!$sessData || $sessData == $this->get_result) { return true; } $this->connect(1); $expire = $this->options['expire']; $sessID = $this->options['prefix'].$sessID; if(is_int($expire) && $expire > 0) { $result = $this->handler->setex($sessID, $expire, $sessData); $re = $result ? 'true' : 'false'; }else{ $result = $this->handler->set($sessID, $sessData); $re = $result ? 'true' : 'false'; } return $result; }
/** +---------------------------------------------------------- * 删除Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessID +---------------------------------------------------------- */ public function destroy($sessID) { $this->connect(1); return $this->handler->delete($this->options['prefix'].$sessID); }
/** +---------------------------------------------------------- * Session 垃圾回收 +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $sessMaxLifeTime +---------------------------------------------------------- */ public function gc($sessMaxLifeTime) { return true; }
/** +---------------------------------------------------------- * 打开Session +---------------------------------------------------------- * @access public +---------------------------------------------------------- * @param string $savePath * @param mixed $sessName +---------------------------------------------------------- */ public function execute() { session_set_save_handler( array(&$this, "open"), array(&$this, "close"), array(&$this, "read"), array(&$this, "write"), array(&$this, "destroy"), array(&$this, "gc") ); } public function __destruct() { if ($this->options['persistent'] == 'pconnect') { $this->handler->close(); } session_write_close(); }
}
|