国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 后端開發(fā) php教程 php操作redies封裝的類

php操作redies封裝的類

Jul 25, 2016 am 08:43 AM

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

php, redies


本站聲明
本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請(qǐng)聯(lián)系admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣服圖片

Undresser.AI Undress

Undresser.AI Undress

人工智能驅(qū)動(dòng)的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用于從照片中去除衣服的在線人工智能工具。

Clothoff.io

Clothoff.io

AI脫衣機(jī)

Video Face Swap

Video Face Swap

使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的代碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

功能強(qiáng)大的PHP集成開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

神級(jí)代碼編輯軟件(SublimeText3)

熱門話題

Laravel 教程
1601
29
PHP教程
1502
276
PHP變量范圍解釋了 PHP變量范圍解釋了 Jul 17, 2025 am 04:16 AM

PHP變量作用域常見問題及解決方法包括:1.函數(shù)內(nèi)部無法訪問全局變量,需使用global關(guān)鍵字或參數(shù)傳入;2.靜態(tài)變量用static聲明,只初始化一次并在多次調(diào)用間保持值;3.超全局變量如$_GET、$_POST可在任何作用域直接使用,但需注意安全過濾;4.匿名函數(shù)需通過use關(guān)鍵字引入父作用域變量,修改外部變量則需傳遞引用。掌握這些規(guī)則有助于避免錯(cuò)誤并提升代碼穩(wěn)定性。

如何在PHP中牢固地處理文件上傳? 如何在PHP中牢固地處理文件上傳? Jul 08, 2025 am 02:37 AM

要安全處理PHP文件上傳需驗(yàn)證來源與類型、控制文件名與路徑、設(shè)置服務(wù)器限制并二次處理媒體文件。1.驗(yàn)證上傳來源通過token防止CSRF并通過finfo_file檢測(cè)真實(shí)MIME類型使用白名單控制;2.重命名文件為隨機(jī)字符串并根據(jù)檢測(cè)類型決定擴(kuò)展名存儲(chǔ)至非Web目錄;3.PHP配置限制上傳大小及臨時(shí)目錄Nginx/Apache禁止訪問上傳目錄;4.GD庫重新保存圖片清除潛在惡意數(shù)據(jù)。

在PHP中評(píng)論代碼 在PHP中評(píng)論代碼 Jul 18, 2025 am 04:57 AM

PHP注釋代碼常用方法有三種:1.單行注釋用//或#屏蔽一行代碼,推薦使用//;2.多行注釋用/.../包裹代碼塊,不可嵌套但可跨行;3.組合技巧注釋如用/if(){}/控制邏輯塊,或配合編輯器快捷鍵提升效率,使用時(shí)需注意閉合符號(hào)和避免嵌套。

撰寫PHP評(píng)論的提示 撰寫PHP評(píng)論的提示 Jul 18, 2025 am 04:51 AM

寫好PHP注釋的關(guān)鍵在于明確目的與規(guī)范,注釋應(yīng)解釋“為什么”而非“做了什么”,避免冗余或過于簡(jiǎn)單。1.使用統(tǒng)一格式,如docblock(/*/)用于類、方法說明,提升可讀性與工具兼容性;2.強(qiáng)調(diào)邏輯背后的原因,如說明為何需手動(dòng)輸出JS跳轉(zhuǎn);3.在復(fù)雜代碼前添加總覽性說明,分步驟描述流程,幫助理解整體思路;4.合理使用TODO和FIXME標(biāo)記待辦事項(xiàng)與問題,便于后續(xù)追蹤與協(xié)作。好的注釋能降低溝通成本,提升代碼維護(hù)效率。

發(fā)電機(jī)如何在PHP中工作? 發(fā)電機(jī)如何在PHP中工作? Jul 11, 2025 am 03:12 AM

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

學(xué)習(xí)PHP:初學(xué)者指南 學(xué)習(xí)PHP:初學(xué)者指南 Jul 18, 2025 am 04:54 AM

易于效率,啟動(dòng)啟動(dòng)tingupalocalserverenverenvirestoolslikexamppandacodeeditorlikevscode.1)installxamppforapache,mysql,andphp.2)uscodeeditorforsyntaxssupport.3)

快速PHP安裝教程 快速PHP安裝教程 Jul 18, 2025 am 04:52 AM

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre

如何通過php中的索引訪問字符串中的字符 如何通過php中的索引訪問字符串中的字符 Jul 12, 2025 am 03:15 AM

在PHP中獲取字符串特定索引字符可用方括號(hào)或花括號(hào),但推薦方括號(hào);索引從0開始,超出范圍訪問返回空值,不可賦值;處理多字節(jié)字符需用mb_substr。例如:$str="hello";echo$str[0];輸出h;而中文等字符需用mb_substr($str,1,1)獲取正確結(jié)果;實(shí)際應(yīng)用中循環(huán)訪問前應(yīng)檢查字符串長(zhǎng)度,動(dòng)態(tài)字符串需驗(yàn)證有效性,多語言項(xiàng)目建議統(tǒng)一使用多字節(jié)安全函數(shù)。

See all articles