首页 > 开发 > PHP > 正文

PHP实现操作redis的封装类完整实例

2024-05-04 23:40:22
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了PHP实现操作redis的封装类,以完整实例形式较为详细的分析了PHP操作redis的自定义类及其相关使用方法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP实现操作redis的封装类。分享给大家供大家参考,具体如下:

 

 
  1. <?php 
  2. /** 
  3. * Redis 操作,支持 Master/Slave 的负载集群 
  4. * 
  5. * @author jackluo 
  6. */ 
  7. class RedisCluster{ 
  8. // 是否使用 M/S 的读写集群方案 
  9. private $_isUseCluster = false; 
  10. // Slave 句柄标记 
  11. private $_sn = 0; 
  12. // 服务器连接句柄 
  13. private $_linkHandle = array( 
  14. 'master'=>null,// 只支持一台 Master 
  15. 'slave'=>array(),// 可以有多台 Slave 
  16. ); 
  17. /** 
  18. * 构造函数 
  19. * 
  20. * @param boolean $isUseCluster 是否采用 M/S 方案 
  21. */ 
  22. public function __construct($isUseCluster=false){ 
  23. $this->_isUseCluster = $isUseCluster; 
  24. } 
  25. /** 
  26. * 连接服务器,注意:这里使用长连接,提高效率,但不会自动关闭 
  27. * 
  28. * @param array $config Redis服务器配置 
  29. * @param boolean $isMaster 当前添加的服务器是否为 Master 服务器 
  30. * @return boolean 
  31. */ 
  32. public function connect($config=array('host'=>'127.0.0.1','port'=>6379), $isMaster=true){ 
  33. // default port 
  34. if(!isset($config['port'])){ 
  35. $config['port'] = 6379; 
  36. } 
  37. // 设置 Master 连接 
  38. if($isMaster){ 
  39. $this->_linkHandle['master'] = new Redis(); 
  40. $ret = $this->_linkHandle['master']->pconnect($config['host'],$config['port']); 
  41. }else{ 
  42. // 多个 Slave 连接 
  43. $this->_linkHandle['slave'][$this->_sn] = new Redis(); 
  44. $ret = $this->_linkHandle['slave'][$this->_sn]->pconnect($config['host'],$config['port']); 
  45. ++$this->_sn; 
  46. } 
  47. return $ret; 
  48. } 
  49. /** 
  50. * 关闭连接 
  51. * 
  52. * @param int $flag 关闭选择 0:关闭 Master 1:关闭 Slave 2:关闭所有 
  53. * @return boolean 
  54. */ 
  55. public function close($flag=2){ 
  56. switch($flag){ 
  57. // 关闭 Master 
  58. case 0: 
  59. $this->getRedis()->close(); 
  60. break; 
  61. // 关闭 Slave 
  62. case 1: 
  63. for($i=0; $i<$this->_sn; ++$i){ 
  64. $this->_linkHandle['slave'][$i]->close(); 
  65. } 
  66. break; 
  67. // 关闭所有 
  68. case 1: 
  69. $this->getRedis()->close(); 
  70. for($i=0; $i<$this->_sn; ++$i){ 
  71. $this->_linkHandle['slave'][$i]->close(); 
  72. } 
  73. break; 
  74. } 
  75. return true; 
  76. } 
  77. /** 
  78. * 得到 Redis 原始对象可以有更多的操作 
  79. * 
  80. * @param boolean $isMaster 返回服务器的类型 true:返回Master false:返回Slave 
  81. * @param boolean $slaveOne 返回的Slave选择 true:负载均衡随机返回一个Slave选择 false:返回所有的Slave选择 
  82. * @return redis object 
  83. */ 
  84. public function getRedis($isMaster=true,$slaveOne=true){ 
  85. // 只返回 Master 
  86. if($isMaster){ 
  87. return $this->_linkHandle['master']; 
  88. }else{ 
  89. return $slaveOne ? $this->_getSlaveRedis() : $this->_linkHandle['slave']; 
  90. } 
  91. } 
  92. /** 
  93. * 写缓存 
  94. * 
  95. * @param string $key 组存KEY 
  96. * @param string $value 缓存值 
  97. * @param int $expire 过期时间, 0:表示无过期时间 
  98. */ 
  99. public function set($key, $value, $expire=0){ 
  100. // 永不超时 
  101. if($expire == 0){ 
  102. $ret = $this->getRedis()->set($key, $value); 
  103. }else{ 
  104. $ret = $this->getRedis()->setex($key, $expire, $value); 
  105. } 
  106. return $ret; 
  107. } 
  108. /** 
  109. * 读缓存 
  110. * 
  111. * @param string $key 缓存KEY,支持一次取多个 $key = array('key1','key2') 
  112. * @return string || boolean 失败返回 false, 成功返回字符串 
  113. */ 
  114. public function get($key){ 
  115. // 是否一次取多个值 
  116. $func = is_array($key) ? 'mGet' : 'get'; 
  117. // 没有使用M/S 
  118. if(! $this->_isUseCluster){ 
  119. return $this->getRedis()->{$func}($key); 
  120. } 
  121. // 使用了 M/S 
  122. return $this->_getSlaveRedis()->{$func}($key); 
  123. } 
  124. /* 
  125. // magic function  
  126. public function __call($name,$arguments){ 
  127. return call_user_func($name,$arguments);  
  128. } 
  129. */ 
  130. /** 
  131. * 条件形式设置缓存,如果 key 不存时就设置,存在时设置失败 
  132. * 
  133. * @param string $key 缓存KEY 
  134. * @param string $value 缓存值 
  135. * @return boolean 
  136. */ 
  137. public function setnx($key, $value){ 
  138. return $this->getRedis()->setnx($key, $value); 
  139. } 
  140. /** 
  141. * 删除缓存 
  142. * 
  143. * @param string || array $key 缓存KEY,支持单个健:"key1" 或多个健:array('key1','key2') 
  144. * @return int 删除的健的数量 
  145. */ 
  146. public function remove($key){ 
  147. // $key => "key1" || array('key1','key2') 
  148. return $this->getRedis()->delete($key); 
  149. } 
  150. /** 
  151. * 值加加操作,类似 ++$i ,如果 key 不存在时自动设置为 0 后进行加加操作 
  152. * 
  153. * @param string $key 缓存KEY 
  154. * @param int $default 操作时的默认值 
  155. * @return int 操作后的值 
  156. */ 
  157. public function incr($key,$default=1){ 
  158. if($default == 1){ 
  159. return $this->getRedis()->incr($key); 
  160. }else{ 
  161. return $this->getRedis()->incrBy($key, $default); 
  162. } 
  163. } 
  164. /** 
  165. * 值减减操作,类似 --$i ,如果 key 不存在时自动设置为 0 后进行减减操作 
  166. * 
  167. * @param string $key 缓存KEY 
  168. * @param int $default 操作时的默认值 
  169. * @return int 操作后的值 
  170. */ 
  171. public function decr($key,$default=1){ 
  172. if($default == 1){ 
  173. return $this->getRedis()->decr($key); 
  174. }else{ 
  175. return $this->getRedis()->decrBy($key, $default); 
  176. } 
  177. } 
  178. /** 
  179. * 添空当前数据库 
  180. * 
  181. * @return boolean 
  182. */ 
  183. public function clear(){ 
  184. return $this->getRedis()->flushDB(); 
  185. } 
  186. /* =================== 以下私有方法 =================== */ 
  187. /** 
  188. * 随机 HASH 得到 Redis Slave 服务器句柄 
  189. * 
  190. * @return redis object 
  191. */ 
  192. private function _getSlaveRedis(){ 
  193. // 就一台 Slave 机直接返回 
  194. if($this->_sn <= 1){ 
  195. return $this->_linkHandle['slave'][0]; 
  196. } 
  197. // 随机 Hash 得到 Slave 的句柄 
  198. $hash = $this->_hashId(mt_rand(), $this->_sn); 
  199. return $this->_linkHandle['slave'][$hash]; 
  200. } 
  201. /** 
  202. * 根据ID得到 hash 后 0~m-1 之间的值 
  203. * 
  204. * @param string $id 
  205. * @param int $m 
  206. * @return int 
  207. */ 
  208. private function _hashId($id,$m=10) 
  209. { 
  210. //把字符串K转换为 0~m-1 之间的一个值作为对应记录的散列地址 
  211. $k = md5($id); 
  212. $l = strlen($k); 
  213. $b = bin2hex($k); 
  214. $h = 0; 
  215. for($i=0;$i<$l;$i++) 
  216. { 
  217. //相加模式HASH 
  218. $h += substr($b,$i*2,2); 
  219. } 
  220. $hash = ($h*1)%$m; 
  221. return $hash; 
  222. } 
  223. /** 
  224. * lpush  
  225. */ 
  226. public function lpush($key,$value){ 
  227. return $this->getRedis()->lpush($key,$value); 
  228. } 
  229. /** 
  230. * add lpop 
  231. */ 
  232. public function lpop($key){ 
  233. return $this->getRedis()->lpop($key); 
  234. } 
  235. /** 
  236. * lrange  
  237. */ 
  238. public function lrange($key,$start,$end){ 
  239. return $this->getRedis()->lrange($key,$start,$end);  
  240. } 
  241. /** 
  242. * set hash opeation 
  243. */ 
  244. public function hset($name,$key,$value){ 
  245. if(is_array($value)){ 
  246. return $this->getRedis()->hset($name,$key,serialize($value));  
  247. } 
  248. return $this->getRedis()->hset($name,$key,$value); 
  249. } 
  250. /** 
  251. * get hash opeation 
  252. */ 
  253. public function hget($name,$key = null,$serialize=true){ 
  254. if($key){ 
  255. $row = $this->getRedis()->hget($name,$key); 
  256. if($row && $serialize){ 
  257. unserialize($row); 
  258. } 
  259. return $row; 
  260. } 
  261. return $this->getRedis()->hgetAll($name); 
  262. } 
  263. /** 
  264. * delete hash opeation 
  265. */ 
  266. public function hdel($name,$key = null){ 
  267. if($key){ 
  268. return $this->getRedis()->hdel($name,$key); 
  269. } 
  270. return $this->getRedis()->hdel($name); 
  271. } 
  272. /** 
  273. * Transaction start 
  274. */ 
  275. public function multi(){ 
  276. return $this->getRedis()->multi();  
  277. } 
  278. /** 
  279. * Transaction send 
  280. */ 
  281. public function exec(){ 
  282. return $this->getRedis()->exec();  
  283. } 
  284. }// End Class 
  285. // ================= TEST DEMO ================= 
  286. // 只有一台 Redis 的应用 
  287. $redis = new RedisCluster(); 
  288. $redis->connect(array('host'=>'127.0.0.1','port'=>6379)); 
  289. //* 
  290. $cron_id = 10001; 
  291. $CRON_KEY = 'CRON_LIST'; // 
  292. $PHONE_KEY = 'PHONE_LIST:'.$cron_id;// 
  293. //cron info 
  294. $cron = $redis->hget($CRON_KEY,$cron_id); 
  295. if(empty($cron)){ 
  296. $cron = array('id'=>10,'name'=>'jackluo');//mysql data 
  297. $redis->hset($CRON_KEY,$cron_id,$cron); // set redis  
  298. } 
  299. //phone list 
  300. $phone_list = $redis->lrange($PHONE_KEY,0,-1); 
  301. print_r($phone_list); 
  302. if(empty($phone_list)){ 
  303. $phone_list =explode(',','13228191831,18608041585'); //mysql data 
  304. //join list 
  305. if($phone_list){ 
  306. $redis->multi(); 
  307. foreach ($phone_list as $phone) { 
  308. $redis->lpush($PHONE_KEY,$phone);  
  309. } 
  310. $redis->exec(); 
  311. } 
  312. } 
  313. print_r($phone_list); 
  314. /*$list = $redis->hget($cron_list,); 
  315. var_dump($list);*/ 
  316. //*/ 
  317. //$redis->set('id',35); 
  318. /* 
  319. $redis->lpush('test','1111'); 
  320. $redis->lpush('test','2222'); 
  321. $redis->lpush('test','3333'); 
  322. $list = $redis->lrange('test',0,-1); 
  323. print_r($list); 
  324. $lpop = $redis->lpop('test'); 
  325. print_r($lpop); 
  326. $lpop = $redis->lpop('test'); 
  327. print_r($lpop); 
  328. $lpop = $redis->lpop('test'); 
  329. print_r($lpop); 
  330. */ 
  331. // var_dump($redis->get('id')); 

希望本文所述对大家PHP程序设计有所帮助。


注:相关教程知识阅读请移步到PHP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表