久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP基于redis計數器類定義與用法示例

這篇文章主要介紹了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方法,并發執行時保證計數自增唯一。
 *
 * Func:
 * public incr  執行自增計數并獲取自增后的數值
 * public get   獲取當前計數
 * public reset  重置計數
 * private connect 創建redis連接
 */
class RedisCounter{ // class start
  private $_config;
  private $_redis;
  /**
   * 初始化
   * @param Array $config redis連接設定
   */
  public function __construct($config){
    $this->_config = $config;
    $this->_redis = $this->connect();
  }
  /**
   * 執行自增計數并獲取自增后的數值
   * @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);
  }
  /**
   * 創建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,
);
// 創建RedisCounter對象
$oRedisCounter = new RedisCounter($config);
// 定義保存計數的健值
$key = 'mycounter';
// 執行自增計數,獲取當前計數,重置計數
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.并發調用計數器,檢查計數唯一性

測試代碼如下:

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

測試并發執行,我們使用ab工具進行測試,設置執行150次,15個并發。

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

執行結果:

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)

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

這篇文章主要介紹了PHP定義字符串的四種方式,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
下面小編就為大家分享一篇php 替換文章中的圖片路徑,下載圖片到本地服務器的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇PHP給源代碼加密的幾種方法匯總(推薦),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面小編就為大家分享一篇php打開本地exe程序,js打開本地exe應用程序,并傳遞相關參數方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
這篇文章主要介紹了PHP類的反射來實現依賴注入過程以及相關知識點分享,對此有興趣的朋友跟著小編學習下吧。
php遍歷一個文件夾內的所有文件和文件夾,并刪除所有文件夾和子文件夾下的所有文件的代碼,通過遞歸方式實現達到清空一個目錄的效果。本文給大家分享實例代碼,需要的朋友參考
主站蜘蛛池模板: 欧美一区二区在线看 | 国产一区视频在线 | 亚洲 中文 欧美 日韩 在线观看 | 精品综合 | 国产精品1区 | 国产视频一区二区 | 日本激情视频在线播放 | 国产精品不卡视频 | 亚洲一区免费视频 | 国产精品一区二区免费 | 亚洲国产一区二区三区 | 欧美一区二区三区一在线观看 | 香蕉久久久久久 | 一区二区在线观看免费视频 | 在线国产一区二区 | 亚洲国产一区二区三区在线观看 | 亚洲国产精品久久人人爱 | 久久精品一区二区 | 国产成人a亚洲精品 | www.日韩 | 亚洲欧美国产毛片在线 | 国产一区二区在线免费 | 2019天天干天天操 | 999视频| 久久精品中文 | 国产国产精品久久久久 | 91精品国产综合久久久密闭 | 午夜影院污 | 免费看国产精品视频 | av片在线播放 | 国产精品一区二区三区在线 | xxxcom在线观看 | 97人人超碰 | 欧美日韩国产精品一区 | 国产一区二区三区在线视频 | 日韩中文在线视频 | 成人精品一区二区三区中文字幕 | 久久亚洲天堂 | 国产三级精品视频 | chengrenzaixian| 91精品国产综合久久久久蜜臀 |