久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網
      最全最豐富的資訊網站

      一分鐘解讀php基于redis計數器類

      本篇文章給大家介紹一分鐘解讀php基于redis計數器類?有一定的參考價值,有需要的朋友可以參考一下,希望對大家有所幫助。

      一分鐘解讀php基于redis計數器類

      Redis是一個開源的使用ANSI C語言編寫、支持網絡、可基于內存亦可持久化的日志型、Key-Value數據庫,并提供多種語言的API。

      本文將使用其incr(自增),get(獲取),delete(清除)方法來實現計數器類。

      1.Redis計數器類代碼及演示實例

      RedisCounter.class.php

      <?php/**  * PHP基于Redis計數器類  * Date:    2017-10-28  * Author:  fdipzone  * Version: 1.0  *  * Descripton:  * php基于Redis實現自增計數,主要使用redis的incr方法,并發(fā)執(zhí)行時保證計數自增唯一。  *  * Func:  * public  incr    執(zhí)行自增計數并獲取自增后的數值  * public  get     獲取當前計數  * public  reset   重置計數  * private connect 創(chuàng)建redis連接  */class RedisCounter{ // class start      private $_config;    private $_redis;    /**      * 初始化      * @param Array $config redis連接設定      */     public function __construct($config){         $this->_config = $config;         $this->_redis = $this->connect();     }    /**      * 執(zhí)行自增計數并獲取自增后的數值      * @param  String $key  保存計數的鍵值      * @param  Int    $incr 自增數量,默認為1      * @return Int      */     public function incr($key, $incr=1){        return intval($this->_redis->incr($key, $incr));     }    /**      * 獲取當前計數      * @param  String $key 保存計數的健值      * @return Int      */     public function get($key){        return intval($this->_redis->get($key));     }    /**      * 重置計數      * @param  String  $key 保存計數的健值      * @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

      <?php Require 'RedisCounter.class.php';  // redis連接設定 $config = array(     'host' => 'localhost',     'port' => 6379,     'index' => 0,     'auth' => '',     'timeout' => 1,     'reserved' => NULL,     'retry_interval' => 100, );  // 創(chuàng)建RedisCounter對象 $oRedisCounter = new RedisCounter($config);  // 定義保存計數的健值 $key = 'mycounter';  // 執(zhí)行自增計數,獲取當前計數,重置計數 echo $oRedisCounter->get($key).PHP_EOL; // 0 echo $oRedisCounter->incr($key).PHP_EOL; // 1 echo $oRedisCounter->incr($key, 10).PHP_EOL; // 11 echo $oRedisCounter->reset($key).PHP_EOL; // 1 echo $oRedisCounter->get($key).PHP_EOL; // 0  ?>

      輸出:

      0 1 11 1 0

      2.并發(fā)調用計數器,檢查計數唯一性

      測試代碼如下:

      <?php Require 'RedisCounter.class.php';  // redis連接設定 $config = array(     'host' => 'localhost',     'port' => 6379,     'index' => 0,     'auth' => '',     'timeout' => 1,     'reserved' => NULL,     'retry_interval' => 100, );  // 創(chuàng)建RedisCounter對象 $oRedisCounter = new RedisCounter($config);  // 定義保存計數的健值 $key = 'mytestcounter';  // 執(zhí)行自增計數并返回自增后的計數,記錄入臨時文件 file_put_contents('/tmp/mytest_result.log', $oRedisCounter->incr($key).PHP_EOL, FILE_APPEND); ?>

      測試并發(fā)執(zhí)行,我們使用ab工具進行測試,設置執(zhí)行150次,15個并發(fā)。

      ab -c 15 -n 150 http://localhost/test.php

      執(zhí)行結果:

      ab -c 15 -n 150 http://localhost/test.php This 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).....done Server Software:        nginx/1.6.3 Server Hostname:        localhost Server Port:            80 Document Path:          /test.php Document Length:        0 bytes Concurrency Level:      15 Time taken for tests:   0.173 seconds Complete requests:      150 Failed requests:        0 Total transferred:      24150 bytes HTML transferred:       0 bytes Requests 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] received Connection Times (ms)               min  mean[+/-sd] median   max Connect:        0    0   0.2      0       1 Processing:     3   16   3.2     16      23 Waiting:        3   16   3.2     16      23 Total:          4   16   3.1     17      23 Percentage 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)

      檢查計數是否唯一

      生成的總計數 wc -l /tmp/mytest_result.log       150 /tmp/mytest_result.log生成的唯一計數 sort -u /tmp/mytest_result.log | wc -l      150

      可以看到在并發(fā)調用的情況下,生成的計數也保證唯一。
      推薦學習:php視頻教程

      贊(0)
      分享到: 更多 (0)
      網站地圖   滬ICP備18035694號-2    滬公網安備31011702889846號