本文實(shí)例講述了PHP基于redis計數(shù)器類定義與用法。分享給大家供大家參考,具體如下:
Redis是一個開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫,并提供多種語言的API。
這里使用其incr(自增),get(獲取),delete(清除)方法來實(shí)現(xiàn)計數(shù)器類。
1.Redis計數(shù)器類代碼及演示實(shí)例
RedisCounter.class.php
<?php/** * PHP基于Redis計數(shù)器類 * Date: 2017-10-28 * Author: fdipzone * Version: 1.0 * * Descripton: * php基于Redis實(shí)現(xiàn)自增計數(shù),主要使用redis的incr方法,并發(fā)執(zhí)行時保證計數(shù)自增唯一。 * * Func: * public incr 執(zhí)行自增計數(shù)并獲取自增后的數(shù)值 * public get 獲取當(dāng)前計數(shù) * public reset 重置計數(shù) * private connect 創(chuàng)建redis連接 */class RedisCounter{ // class start private $_config; private $_redis; /** * 初始化 * @param Array $config redis連接設(shè)定 */ public function __construct($config){ $this->_config = $config; $this->_redis = $this->connect(); } /** * 執(zhí)行自增計數(shù)并獲取自增后的數(shù)值 * @param String $key 保存計數(shù)的鍵值 * @param Int $incr 自增數(shù)量,默認(rèn)為1 * @return Int */ public function incr($key, $incr=1){ return intval($this->_redis->incr($key, $incr)); } /** * 獲取當(dāng)前計數(shù) * @param String $key 保存計數(shù)的健值 * @return Int */ public function get($key){ return intval($this->_redis->get($key)); } /** * 重置計數(shù) * @param String $key 保存計數(shù)的健值 * @return Int */ public function reset($key){ return $this->_redis->delete($key); } /** * 創(chuàng)建redis連接 * @return Link */ private function connect(){ try{ $redis = new Redis(); $redis->connect($this->_config['host'],$this->_config['port'],$this->_config['timeout'],$this->_config['reserved'],$this->_config['retry_interval']); if(empty($this->_config['auth'])){ $redis->auth($this->_config['auth']); } $redis->select($this->_config['index']); }catch(RedisException $e){ throw new Exception($e->getMessage()); return false; } return $redis; }} // class end?>demo.php
<?phpRequire 'RedisCounter.class.php';// redis連接設(shè)定$config = array( 'host' => 'localhost', 'port' => 6379, 'index' => 0, 'auth' => '', 'timeout' => 1, 'reserved' => NULL, 'retry_interval' => 100,);// 創(chuàng)建RedisCounter對象$oRedisCounter = new RedisCounter($config);// 定義保存計數(shù)的健值$key = 'mycounter';// 執(zhí)行自增計數(shù),獲取當(dāng)前計數(shù),重置計數(shù)echo $oRedisCounter->get($key).PHP_EOL; // 0echo $oRedisCounter->incr($key).PHP_EOL; // 1echo $oRedisCounter->incr($key, 10).PHP_EOL; // 11echo $oRedisCounter->reset($key).PHP_EOL; // 1echo $oRedisCounter->get($key).PHP_EOL; // 0?>
輸出:
011110
2.并發(fā)調(diào)用計數(shù)器,檢查計數(shù)唯一性
測試代碼如下:
<?phpRequire 'RedisCounter.class.php';// redis連接設(shè)定$config = array( 'host' => 'localhost', 'port' => 6379, 'index' => 0, 'auth' => '', 'timeout' => 1, 'reserved' => NULL, 'retry_interval' => 100,);// 創(chuàng)建RedisCounter對象$oRedisCounter = new RedisCounter($config);// 定義保存計數(shù)的健值$key = 'mytestcounter';// 執(zhí)行自增計數(shù)并返回自增后的計數(shù),記錄入臨時文件file_put_contents('/tmp/mytest_result.log', $oRedisCounter->incr($key).PHP_EOL, FILE_APPEND);?>測試并發(fā)執(zhí)行,我們使用ab工具進(jìn)行測試,設(shè)置執(zhí)行150次,15個并發(fā)。
ab -c 15 -n 150 http://localhost/test.php
執(zhí)行結(jié)果:
ab -c 15 -n 150 http://localhost/test.phpThis is ApacheBench, Version 2.3 <$Revision: 1554214 $>Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/Licensed to The Apache Software Foundation, http://www.apache.org/Benchmarking home.rabbit.km.com (be patient).....doneServer Software: nginx/1.6.3Server Hostname: localhostServer Port: 80Document Path: /test.phpDocument Length: 0 bytesConcurrency Level: 15Time taken for tests: 0.173 secondsComplete requests: 150Failed requests: 0Total transferred: 24150 bytesHTML transferred: 0 bytesRequests per second: 864.86 [#/sec] (mean)Time per request: 17.344 [ms] (mean)Time per request: 1.156 [ms] (mean, across all concurrent requests)Transfer rate: 135.98 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 0 0.2 0 1Processing: 3 16 3.2 16 23Waiting: 3 16 3.2 16 23Total: 4 16 3.1 17 23Percentage of the requests served within a certain time (ms) 50% 17 66% 18 75% 18 80% 19 90% 20 95% 21 98% 22 99% 22 100% 23 (longest request)
檢查計數(shù)是否唯一
生成的總計數(shù)wc -l /tmp/mytest_result.log 150 /tmp/mytest_result.log生成的唯一計數(shù)sort -u /tmp/mytest_result.log | wc -l 150
可以看到在并發(fā)調(diào)用的情況下,生成的計數(shù)也保證唯一。
希望本文所述對大家PHP程序設(shè)計有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選