麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > 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. * @param array $config Redis服務器配置 
  28. * @param boolean $isMaster 當前添加的服務器是否為 Master 服務器 
  29. * @return boolean 
  30. */ 
  31. public function connect($config=array('host'=>'127.0.0.1','port'=>6379), $isMaster=true){ 
  32. // default port 
  33. if(!isset($config['port'])){ 
  34. $config['port'] = 6379; 
  35. // 設置 Master 連接 
  36. if($isMaster){ 
  37. $this->_linkHandle['master'] = new Redis(); 
  38. $ret = $this->_linkHandle['master']->pconnect($config['host'],$config['port']); 
  39. }else
  40. // 多個 Slave 連接 
  41. $this->_linkHandle['slave'][$this->_sn] = new Redis(); 
  42. $ret = $this->_linkHandle['slave'][$this->_sn]->pconnect($config['host'],$config['port']); 
  43. ++$this->_sn; 
  44. return $ret; 
  45. /** 
  46. * 關閉連接 
  47. * 
  48. * @param int $flag 關閉選擇 0:關閉 Master 1:關閉 Slave 2:關閉所有 
  49. * @return boolean 
  50. */ 
  51. public function close($flag=2){ 
  52. switch($flag){ 
  53. // 關閉 Master 
  54. case 0: 
  55. $this->getRedis()->close(); 
  56. break
  57. // 關閉 Slave 
  58. case 1: 
  59. for($i=0; $i<$this->_sn; ++$i){ 
  60. $this->_linkHandle['slave'][$i]->close(); 
  61. break
  62. // 關閉所有 
  63. case 1: 
  64. $this->getRedis()->close(); 
  65. for($i=0; $i<$this->_sn; ++$i){ 
  66. $this->_linkHandle['slave'][$i]->close(); 
  67. break
  68. return true
  69. /** 
  70. * 得到 Redis 原始對象可以有更多的操作 
  71. * 
  72. * @param boolean $isMaster 返回服務器的類型 true:返回Master false:返回Slave 
  73. * @param boolean $slaveOne 返回的Slave選擇 true:負載均衡隨機返回一個Slave選擇 false:返回所有的Slave選擇 
  74. * @return redis object 
  75. */ 
  76. public function getRedis($isMaster=true,$slaveOne=true){ 
  77. // 只返回 Master 
  78. if($isMaster){ 
  79. return $this->_linkHandle['master']; 
  80. }else
  81. return $slaveOne ? $this->_getSlaveRedis() : $this->_linkHandle['slave']; 
  82. /** 
  83. * 寫緩存 
  84. * 
  85. * @param string $key 組存KEY 
  86. * @param string $value 緩存值 
  87. * @param int $expire 過期時間, 0:表示無過期時間 
  88. */ 
  89. public function set($key, $value, $expire=0){ 
  90. // 永不超時 
  91. if($expire == 0){ 
  92. $ret = $this->getRedis()->set($key, $value); 
  93. }else
  94. $ret = $this->getRedis()->setex($key, $expire, $value); 
  95. return $ret; 
  96. /** 
  97. * 讀緩存 
  98. * 
  99. * @param string $key 緩存KEY,支持一次取多個 $key = array('key1','key2') 
  100. * @return string || boolean 失敗返回 false, 成功返回字符串 
  101. */ 
  102. public function get($key){ 
  103. // 是否一次取多個值 
  104. $func = is_array($key) ? 'mGet' : 'get'
  105. // 沒有使用M/S 
  106. if(! $this->_isUseCluster){ 
  107. return $this->getRedis()->{$func}($key); 
  108. // 使用了 M/S 
  109. return $this->_getSlaveRedis()->{$func}($key); 
  110. /* 
  111. // magic function  
  112. public function __call($name,$arguments){ 
  113. return call_user_func($name,$arguments);  
  114. } 
  115. */ 
  116. /** 
  117. * 條件形式設置緩存,如果 key 不存時就設置,存在時設置失敗 
  118. * 
  119. * @param string $key 緩存KEY 
  120. * @param string $value 緩存值 
  121. * @return boolean 
  122. */ 
  123. public function setnx($key, $value){ 
  124. return $this->getRedis()->setnx($key, $value); 
  125. /** 
  126. * 刪除緩存 
  127. * 
  128. * @param string || array $key 緩存KEY,支持單個健:"key1" 或多個健:array('key1','key2') 
  129. * @return int 刪除的健的數量 
  130. */ 
  131. public function remove($key){ 
  132. // $key => "key1" || array('key1','key2') 
  133. return $this->getRedis()->delete($key); 
  134. /** 
  135. * 值加加操作,類似 ++$i ,如果 key 不存在時自動設置為 0 后進行加加操作 
  136. * 
  137. * @param string $key 緩存KEY 
  138. * @param int $default 操作時的默認值 
  139. * @return int 操作后的值 
  140. */ 
  141. public function incr($key,$default=1){ 
  142. if($default == 1){ 
  143. return $this->getRedis()->incr($key); 
  144. }else
  145. return $this->getRedis()->incrBy($key, $default); 
  146. /** 
  147. * 值減減操作,類似 --$i ,如果 key 不存在時自動設置為 0 后進行減減操作 
  148. * 
  149. * @param string $key 緩存KEY 
  150. * @param int $default 操作時的默認值 
  151. * @return int 操作后的值 
  152. */ 
  153. public function decr($key,$default=1){ 
  154. if($default == 1){ 
  155. return $this->getRedis()->decr($key); 
  156. }else
  157. return $this->getRedis()->decrBy($key, $default); 
  158. /** 
  159. * 添空當前數據庫 
  160. * 
  161. * @return boolean 
  162. */ 
  163. public function clear(){ 
  164. return $this->getRedis()->flushDB(); 
  165. /* =================== 以下私有方法 =================== */ 
  166. /** 
  167. * 隨機 HASH 得到 Redis Slave 服務器句柄 
  168. * 
  169. * @return redis object 
  170. */ 
  171. private function _getSlaveRedis(){ 
  172. // 就一臺 Slave 機直接返回 
  173. if($this->_sn <= 1){ 
  174. return $this->_linkHandle['slave'][0]; 
  175. // 隨機 Hash 得到 Slave 的句柄 
  176. $hash = $this->_hashId(mt_rand(), $this->_sn); 
  177. return $this->_linkHandle['slave'][$hash]; 
  178. /** 
  179. * 根據ID得到 hash 后 0~m-1 之間的值 
  180. * 
  181. * @param string $id 
  182. * @param int $m 
  183. * @return int 
  184. */ 
  185. private function _hashId($id,$m=10) 
  186. //把字符串K轉換為 0~m-1 之間的一個值作為對應記錄的散列地址 
  187. $k = md5($id); 
  188. $l = strlen($k); 
  189. $b = bin2hex($k); 
  190. $h = 0; 
  191. for($i=0;$i<$l;$i++) 
  192. //相加模式HASH 
  193. $h += substr($b,$i*2,2); 
  194. $hash = ($h*1)%$m; 
  195. return $hash; 
  196. /** 
  197. * lpush  
  198. */ 
  199. public function lpush($key,$value){ 
  200. return $this->getRedis()->lpush($key,$value); 
  201. /** 
  202. * add lpop 
  203. */ 
  204. public function lpop($key){ 
  205. return $this->getRedis()->lpop($key); 
  206. /** 
  207. * lrange  
  208. */ 
  209. public function lrange($key,$start,$end){ 
  210. return $this->getRedis()->lrange($key,$start,$end);  
  211. /** 
  212. * set hash opeation 
  213. */ 
  214. public function hset($name,$key,$value){ 
  215. if(is_array($value)){ 
  216. return $this->getRedis()->hset($name,$key,serialize($value));  
  217. return $this->getRedis()->hset($name,$key,$value); 
  218. /** 
  219. * get hash opeation 
  220. */ 
  221. public function hget($name,$key = null,$serialize=true){ 
  222. if($key){ 
  223. $row = $this->getRedis()->hget($name,$key); 
  224. if($row && $serialize){ 
  225. unserialize($row); 
  226. return $row; 
  227. return $this->getRedis()->hgetAll($name); 
  228. /** 
  229. * delete hash opeation 
  230. */ 
  231. public function hdel($name,$key = null){ 
  232. if($key){ 
  233. return $this->getRedis()->hdel($name,$key); 
  234. return $this->getRedis()->hdel($name); 
  235. /** 
  236. * Transaction start 
  237. */ 
  238. public function multi(){ 
  239. return $this->getRedis()->multi();  
  240. /** 
  241. * Transaction send 
  242. */ 
  243. public function exec(){ 
  244. return $this->getRedis()->exec();  
  245. }// End Class 
  246. // ================= TEST DEMO ================= 
  247. // 只有一臺 Redis 的應用 
  248. $redis = new RedisCluster(); 
  249. $redis->connect(array('host'=>'127.0.0.1','port'=>6379)); 
  250. //* 
  251. $cron_id = 10001; 
  252. $CRON_KEY = 'CRON_LIST'// 
  253. $PHONE_KEY = 'PHONE_LIST:'.$cron_id;// 
  254. //cron info 
  255. $cron = $redis->hget($CRON_KEY,$cron_id); 
  256. if(empty($cron)){ 
  257. $cron = array('id'=>10,'name'=>'jackluo');//mysql data 
  258. $redis->hset($CRON_KEY,$cron_id,$cron); // set redis  
  259. //phone list 
  260. $phone_list = $redis->lrange($PHONE_KEY,0,-1); 
  261. print_r($phone_list); 
  262. if(empty($phone_list)){ 
  263. $phone_list =explode(',','13228191831,18608041585'); //mysql data 
  264. //join list 
  265. if($phone_list){ 
  266. $redis->multi(); 
  267. foreach ($phone_list as $phone) { 
  268. $redis->lpush($PHONE_KEY,$phone);  
  269. $redis->exec(); 
  270. print_r($phone_list); 
  271. /*$list = $redis->hget($cron_list,); 
  272. var_dump($list);*/ 
  273. //*/ 
  274. //$redis->set('id',35); 
  275. /* 
  276. $redis->lpush('test','1111'); 
  277. $redis->lpush('test','2222'); 
  278. $redis->lpush('test','3333'); 
  279. $list = $redis->lrange('test',0,-1); 
  280. print_r($list); 
  281. $lpop = $redis->lpop('test'); 
  282. print_r($lpop); 
  283. $lpop = $redis->lpop('test'); 
  284. print_r($lpop); 
  285. $lpop = $redis->lpop('test'); 
  286. print_r($lpop); 
  287. */ 
  288. // var_dump($redis->get('id')); 

希望本文所述對大家PHP程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 免费人成在线观看网站 | 久久久经典视频 | 香蕉国产片 | 久综合| 99在线精品视频免费观看20 | 九九热这里只有精品8 | 欧美精品免费一区二区三区 | 国产一区视频免费观看 | 亚洲小视频在线 | 久久精品com| 视频在线色| 在线播放中文 | 精品一区二区三区四区在线 | 日韩视频区 | 爱视频福利 | 久久成人免费网 | 国产成人自拍视频在线 | 欧美18xxxx| www视频免费在线观看 | 黄色毛片视频在线观看 | 久久精品中文字幕 | 欧美成人精品一区二区 | 叶子楣成人爽a毛片免费啪啪 | 久久精品中文字幕一区二区 | 香蕉黄色网| 国产成人av一区 | 欧美一级片一区 | 亚洲欧美日韩一区二区三区在线观看 | 狠狠色噜噜狠狠狠米奇9999 | 久久久久久久久久性 | 羞羞色院91精品网站 | 午夜精品影院 | 狠狠操人人干 | 国产日韩a | 最新一区二区三区 | 色悠悠久久久久 | 91久久久久久久久久久久久 | 亚州综合一区 | 亚洲精品久久久久久 | 99热久草| 欧美亚洲一区二区三区四区 |