1 <?php 2 // +---------------------------------------------------------------------- 3 // | © 2015 DBRedis.class.php 4 // +---------------------------------------------------------------------- 5 // | Author: banpingshui 6 // +---------------------------------------------------------------------- 7 8 defined(‘THINK_PATH‘) or exit(); 9 class DbRedis extends Db{ 10 protected $_redis = null; // Redis Object 11 protected $_keyname = null; // Redis Key 12 protected $_dbName = ‘‘; // dbName 13 protected $_cursor = null; // Reids Cursor Object 14 /** 15 * 架构函数 读取数据库配置信息 16 * @access public 17 * @param array $config 数据库配置数组 18 */ 19 public function __construct($config=‘‘){ 21 if ( !class_exists(‘redis‘) ) { 22 throw_exception(L(‘_NOT_SUPPERT_‘).‘:redis‘); 23 } 24 if(!empty($config)) { 25 $this->config = $config; 26 if(empty($this->config[‘params‘])) { 27 $this->config[‘params‘] = array(); 28 } 29 30 } 31 } 32 /** 33 * 连接数据库方法 34 * @access public 35 */ 36 public function connect($config=‘‘,$linkNum=0) { 37 if ( !isset($this->linkID[$linkNum]) ) { 38 if(empty($config)) $config = $this->config; 39 40 $redis = new Redis(); 41 $redis->connect($config["hostname"]?$config["hostname"]:"localhost",$config["hostport"]?$config["hostport"]:6379); 42 //$redis->auth($config["username"]?$config["username"]:""); 43 //$info=$redis->info(); //如果连接的是twemproxy,这里必须要注释~~~ 44 // 标记连接成功 45 if (true){ 46 //if (!empty($info["redis_version"])){ 47 $this->linkID[$linkNum] = $redis; 48 $this->connected = true; 49 } 50 // 注销数据库连接配置信息 51 if(1 != C(‘DB_DEPLOY_TYPE‘)) unset($this->config); 52 } 53 return $this->linkID[$linkNum]; 54 } 55 /** 56 * 切换当前操作的Db和redis key 57 * @access public 58 * @param string $keyname redis key 59 * @param string $db db 60 * @param boolean $master 是否主服务器 61 * @return void 62 */ 63 public function switchKey($keyname,$db=‘‘,$master=true){ 64 // 当前没有连接 则首先进行数据库连接 65 if ( !$this->_linkID ) $this->initConnect($master); 66 try{ 67 if(!empty($db)) { // 传入Db则切换数据库 68 // 当前MongoDb对象 69 $this->_dbName = $db; 70 $this->_redis = $this->_linkID->select($db); 71 } 72 // 当前MongoCollection对象 73 if(C(‘DB_SQL_LOG‘)) { 74 $this->queryStr = $this->_dbName.‘.getKey(‘.$keyname.‘)‘; 75 } 76 if($this->_keyname != $keyname) { 77 N(‘db_read‘,1); 78 // 记录开始执行时间 79 G(‘queryStartTime‘); 80 $this->debug(); 81 $this->_keyname = $keyname; 82 } 83 }catch (Exception $e){ 84 throw_exception($e->getMessage()); 85 } 86 } 87 /** 88 * 释放查询结果 89 * @access public 90 */ 91 public function free() { 92 $this->_cursor = null; 93 } 94 /** 95 * 关闭数据库 96 * @access public 97 */ 98 public function close() { 99 if($this->_linkID) { 100 $this->_linkID->close(); 101 $this->_linkID = null; 102 $this->_redis = null; 103 $this->_keyname = null; 104 $this->_cursor = null; 105 } 106 } 107 /** 108 * 查找记录 109 * @access public 110 * @param array $options 表达式 111 * @return iterator 112 */ 113 public function select($options=array()) { 115 if(isset($options[‘table‘])) { 116 $this->switchKey($options[‘table‘],‘‘,false); 117 } 118 $cache = isset($options[‘cache‘])?$options[‘cache‘]:false; 119 if($cache) { // 查询缓存检测 120 $key = is_string($cache[‘key‘])?$cache[‘key‘]:md5(serialize($options)); 121 $value = S($key,‘‘,‘‘,$cache[‘type‘]); 122 if(false !== $value) { 123 return $value; 124 } 125 } 126 $this->model = $options[‘model‘]; 127 N(‘db_query‘,1); 128 //$query = $this->parseWhere($options[‘where‘]); 129 $field = $this->parseField($options[‘field‘]); 130 try{ 131 if(C(‘DB_SQL_LOG‘)) { 132 // $this->queryStr = $this->_dbName.‘查询出错:‘.$field; 133 } 134 // 记录开始执行时间 135 G(‘queryStartTime‘); 136 137 if ($options[‘limit‘]){ 138 $limit=$this->parseLimit($options[‘limit‘]); 139 }else{ 140 $limit=array("0"=>0,"1"=>19); 141 } 142 if($options[‘type‘]) { 143 if ($options["type"]==strtolower("list")){ 144 //列表 145 $_cursor = $this->_linkID->lRange($this->_keyname, $limit[0],$limit[1]); 146 }elseif ($options["type"]==strtolower("sets")){ 147 //集合 148 149 switch (strtolower($options["where"])) { 150 case "sinterstore": 151 //求交集 152 $_cursor = $this->_linkID->sInter($field); 153 break; 154 case "sunion": 155 //求并集 156 $_cursor = $this->_linkID->sUnion($field); 157 break; 158 case "sdiff": 159 //求差值 160 $_cursor = $this->_linkID->sDiff($field); 161 break; 162 default: 163 $_cursor = $this->_linkID->sMembers($this->_keyname); 164 } 165 }elseif ($options["type"]==strtolower("zset")){ 166 //有序集合 167 if ($options["zscore"]){ 168 $_cursor = $this->_linkID->zScore($this->_keyname, $options["zscore"]); 169 } else { 170 $zsets=$options["order"][0]; 171 $page=$options["page"]?$options["page"]:1; 172 $limit[0]+=($page-1)*$options["limit"]; 173 $limit[1]+=($page-1)*$options["limit"]; 174 switch (strtolower($zsets)) { 175 case strtolower("zRevRange"): 176 $_cursor = $this->_linkID->zRevRange($this->_keyname, $limit[0],$limit[1],$options["order"][1]); 177 break; 178 179 default: 180 $_cursor = $this->_linkID->zRange($this->_keyname, $limit[0],$limit[1],$options["order"][1]); 181 break; 182 } 183 } 184 }elseif ($options["type"]==strtolower("string")){ 185 //字符串 186 $_cursor = $this->_linkID->mget($field); 187 }elseif ($options["type"]==strtolower("hash")){ 188 //HASH 189 if (empty($field)){ 190 $_cursor = $this->_linkID->hGetAll($this->_keyname); 191 }else{ 192 $_cursor = $this->_linkID->hmGet($this->_keyname,$field); 193 } 194 } 195 }else{ 196 $_cursor = $this->_linkID->lRange($this->_keyname, $limit[0],$limit[1]); 197 } 198 $this->debug(); 199 $this->_cursor = $_cursor; 200 $resultSets = $_cursor; 201 if($cache && $resultSet ) { // 查询缓存写入 202 S($key,$resultSet,$cache[‘expire‘],$cache[‘type‘]); 203 } 204 return $resultSets; 205 } catch (Exception $e) { 206 throw_exception($e->getMessage()); 207 } 208 209 } 210 /** 211 * 统计记录数 212 * @access public 213 * @param array $options 表达式 214 * @return iterator 215 */ 216 public function count($options=array()){ 218 $count=0; 219 if(isset($options[‘table‘])) { 220 $this->switchKey($options[‘table‘],‘‘,false); 221 } 222 $this->model = $options[‘model‘]; 223 N(‘db_query‘,1); 224 //$query = $this->parseWhere($options[‘where‘]); 225 $field = $this->parseField($options[‘field‘]); 226 try{ 227 if(C(‘DB_SQL_LOG‘)) { 228 $this->queryStr = $this->_dbName.‘查询出错:‘.$field; 229 } 230 // 记录开始执行时间 231 G(‘queryStartTime‘); 232 233 if($options[‘type‘]) { 234 if ($options["type"]==strtolower("list")){ 235 //列表 236 $count = $this->_linkID->lSize($this->_keyname); 237 }elseif ($options["type"]==strtolower("sets")){ 238 //集合 239 $count = $this->_linkID->sCard($this->_keyname); 240 }elseif ($options["type"]==strtolower("zset")){ 241 //有序集合 242 $count = $this->_linkID->zCard($this->_keyname); 243 }elseif ($options["type"]==strtolower("string")){ 244 //字符串 245 }elseif ($options["type"]==strtolower("hash")){ 246 //HASH 247 $count = $this->_linkID->hLen($this->_keyname); 248 }elseif ($options["type"]==strtolower("hllc")){ 249 //HASH 250 $count = $this->_linkID->pfCount($this->_keyname); 251 } 252 }else{ 253 $count = $this->_linkID->lSize($this->_keyname); 254 } 255 $this->debug(); 256 return $count; 257 } catch (Exception $e) { 259 throw_exception($e->getMessage()); 260 } 261 262 263 264 } 265 /** 266 * 添加数据 267 * Enter description here ... 268 * @param unknown_type $options 269 * @param unknown_type $data 270 */ 271 public function add($options=array(),$data){ 272 if(isset($options[‘table‘])) { 273 $this->switchKey($options[‘table‘],‘‘,false); 274 } 275 $this->model = $options[‘model‘]; 276 N(‘db_query‘,1); 277 //$query = $this->parseWhere($options[‘where‘]); 278 $field = $this->parseField($options[‘field‘]); 279 try{ 280 if(C(‘DB_SQL_LOG‘)) { 281 $this->queryStr = $this->_dbName.‘查询出错:‘.$field; 282 } 283 // 记录开始执行时间 284 G(‘queryStartTime‘); 285 if($options[‘type‘]) { 286 if ($options["type"]==strtolower("list")){ 287 //列表 288 $add = $this->_linkID->lPush($this->_keyname,$data); 289 }elseif ($options["type"]==strtolower("sets")){ 290 //集合 291 $add = $this->_linkID->sAdd($this->_keyname,$data); 292 }elseif ($options["type"]==strtolower("zset")){ 293 //有序集合 294 foreach ($data as $key=>$value) { 295 $add = $this->_linkID->zAdd($this->_keyname,$key,$value); 296 } 297 298 }elseif ($options["type"]==strtolower("string")){ 299 //字符串 300 $add = $this->_linkID->mset($data); 301 }elseif ($options["type"]==strtolower("hash")){ 302 //HASH 303 $add = $this->_linkID->hmSet($this->_keyname,$data); 304 } 305 }else{ 306 $add = $this->_linkID->lPush($this->_keyname,$data); 307 } 308 $this->debug(); 309 return $add; 310 } catch (Exception $e) { 311 throw_exception($e->getMessage()); 312 } 313 } 314 /** 315 * 删除数据 316 * Enter description here ... 317 * @param unknown_type $options 318 * @param unknown_type $data 319 */ 320 public function delete($options=array(),$way=""){ 321 if(isset($options[‘table‘])) { 322 $this->switchKey($options[‘table‘],‘‘,false); 323 } 324 $this->model = $options[‘model‘]; 325 N(‘db_query‘,1); 326 //$query = $this->parseWhere($options[‘where‘]); 327 $field = $this->parseField($options[‘field‘]); 328 try{ 329 if(C(‘DB_SQL_LOG‘)) { 330 $this->queryStr = $this->_dbName.‘查询出错:‘.$field; 331 } 332 // 记录开始执行时间 333 G(‘queryStartTime‘); 334 if ($options["type"]==strtolower("list") || empty($options["type"])){ 335 //列表 336 switch (strtolower($way)) { 337 case "lpop": 338 $delete=$this->_linkID->lPop($this->_keyname); 339 break; 340 case "ltrim": 341 $delete=$this->_linkID->lTrim(‘key‘, $options["where"][0], $options["where"][1]); 342 break; 343 default: 344 if ($this->_linkID->lSet($this->_keyname,intval($options["where"]),"_deleted_")){ 345 $delete=$this->_linkID->lRem($this->_keyname,"_deleted_",0); 346 } 347 break; 348 } 349 }elseif ($options["type"]==strtolower("sets")){ 350 //集合 351 $delete = $this->_linkID->sRem($this->_keyname,$options["where"]); 352 }elseif ($options["type"]==strtolower("zset")){ 353 //有序集合 354 switch (strtolower($way)) { 355 case strtolower("zremrangebyscore"): 356 $delete = $this->_linkID->zRemRangeByScore($this->_keyname,$options["where"][0],$options["where"][1]); 357 break; 358 case strtolower("zRemRangeByRank"): 359 $delete = $this->_linkID->zRemRangeByRank($this->_keyname,$options["where"][0],$options["where"][1]); 360 break; 361 default: 362 $delete = $this->_linkID->zDelete($this->_keyname,$options["where"]); 363 break; 364 } 365 366 }elseif ($options["type"]==strtolower("string")){ 367 //字符串 368 $delete = $this->_linkID->delete($field); 369 }elseif ($options["type"]==strtolower("hash")){ 370 //HASH 371 $delete = $this->_linkID->hDel($this->_keyname, $options["where"]); 372 } 373 $this->debug(); 374 return $delete; 375 } catch (Exception $e) { 376 throw_exception($e->getMessage()); 377 } 378 } 379 380 /** 381 * limit分析 382 * @access protected 383 * @param mixed $limit 384 * @return array 385 */ 386 protected function parseLimit($limit) { 387 $limit = $limit - 1; 388 if(strpos($limit,‘,‘)) { 389 $array = explode(‘,‘,$limit); 390 }else{ 391 $array = array(0,$limit); 392 } 393 return $array; 394 } 395 396 /** 397 * field分析 398 * @access protected 399 * @param mixed $fields 400 * @return array 401 */ 402 public function parseField($fields){ 403 if (is_array($fields)){ 404 return $fields; 405 } 406 } 407 /** 408 * 取得数据表的字段信息 409 * @access public 410 * @return array 411 */ 412 public function getFields($keyname=‘‘){ 413 if(!empty($keyname) && $keyname != $this->_keyname) { 414 $this->switchKey($keyname,‘‘,false); 415 } 416 N(‘db_query‘,1); 417 if(C(‘DB_SQL_LOG‘)) { 418 //$this->queryStr = $this->_dbName.‘.‘.$this->_collectionName.‘.findOne()‘; 419 } 420 try{ 421 // 记录开始执行时间 422 G(‘queryStartTime‘); 423 $result = $this->_linkID->hkeys($this->_keyname); 424 $this->debug(); 425 } catch (Exception $e) { 426 throw_exception($e->getMessage()); 427 } 428 if($result) { // 存在数据则分析字段 429 $info = array(); 430 foreach ($result as $key=>$val){ 431 $info[$key] = array( 432 ‘name‘=>$key, 433 ‘type‘=>getType($val), 434 ); 435 } 436 return $info; 437 } 438 // 暂时没有数据 返回false 439 return false; 440 } 441 } 442 ?> 443
1 <?php 2 // +---------------------------------------------------------------------- 3 // | © 2015 RedisModel.class.php 4 // +---------------------------------------------------------------------- 5 // | Author: banpingshui 6 // +---------------------------------------------------------------------- 7 8 class RedisModel extends Model{ 9 /** 10 * 利用__call方法实现一些特殊的Model方法 11 * @access public 12 * @param string $method 方法名称 13 * @param array $args 调用参数 14 * @return mixed 15 */ 16 public function __call($method,$args) { 17 if(in_array(strtolower($method),array(‘type‘,‘where‘,‘order‘,‘limit‘,‘page‘,‘zscore‘),true)) { 18 // 连贯操作的实现 19 $this->options[strtolower($method)] = $args[0]; 20 return $this; 21 }else if(in_array(strtolower($method),array(‘key‘),true)){ 22 $this->options[‘table‘] = $args[0]; 23 return $this; 24 }else{ 25 throw_exception(__CLASS__.‘:‘.$method.L(‘_METHOD_NOT_EXIST_‘)); 26 return; 27 } 28 } 29 /** 30 * count统计 配合where连贯操作 31 * @access public 32 * @return integer 33 */ 34 35 public function count($key){ 36 // 分析表达式 37 $options = $this->_parseOptions(); 38 return $this->db->count($options); 39 } 40 public function add($data){ 41 // 分析表达式 42 $options = $this->_parseOptions(); 43 return $this->db->add($options,$data); 44 } 45 public function delete($way=""){ 46 // 分析表达式 47 $options = $this->_parseOptions(); 48 return $this->db->delete($options,$way); 49 } 50 } 51 52 ?>
目前还没有实现expire
时间: 2024-10-07 05:06:51