好湿?好紧?好多水好爽自慰,久久久噜久噜久久综合,成人做爰A片免费看黄冈,机机对机机30分钟无遮挡

主頁 > 知識庫 > PHP基于redis計數器類定義與用法示例

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

熱門標簽:電話機器人危險嗎 專業電話機器人批發商 江蘇外呼電銷機器人報價 深圳外呼系統收費 400電話申請方法收費 離石地圖標注 南寧高頻外呼回撥系統哪家好 400電話辦理福州市 長沙crm外呼系統業務

本文實例講述了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)

檢查計數是否唯一

生成的總計數

wc -l /tmp/mytest_result.log
   150 /tmp/mytest_result.log

生成的唯一計數

sort -u /tmp/mytest_result.log | wc -l
   150

可以看到在并發調用的情況下,生成的計數也保證唯一。

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+redis數據庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

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

您可能感興趣的文章:
  • Redis的使用模式之計數器模式實例
  • Redis實現唯一計數的3種方法分享
  • redis實現計數器-防止刷單方法介紹
  • Redis實現高并發計數器
  • Spring之借助Redis設計一個簡單訪問計數器的示例
  • Docker 部署 SpringBoot 項目整合 Redis 鏡像做訪問計數示例代碼
  • redis通過位圖法記錄在線用戶的狀態詳解
  • Redis精確去重計數方法(咆哮位圖)

標簽:太原 株洲 濱州 興安盟 南昌 南京 白酒營銷 曲靖

巨人網絡通訊聲明:本文標題《PHP基于redis計數器類定義與用法示例》,本文關鍵詞  PHP,基于,redis,計數器,類,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《PHP基于redis計數器類定義與用法示例》相關的同類信息!
  • 本頁收集關于PHP基于redis計數器類定義與用法示例的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 将军小娇乳H边走边欢1v1古代| yjizz国产在线视频网| 欧洲vodafonewifi巨大403| 在线看亚洲| 蜜桃秘?做爰免费网站| 偷窥美女洗澡一区二区三区| 双性浪货嗯啊趴下粗口hnp| 亂伦小说合集小说| 搡老熟女一区二区三区在线视频| 香港特级婬片A片免费看| 胸部无遮挡| 男男调教视频| 扒了内裤让男人爽一夜| 久久久91人妻无码Av蜜桃电影| 青青草原国产AV一区欧美| 日本三人交xxx69视频| 艳妇雪白细嫩的艳妇| 久久久夜色精品亚洲AV李宗瑞 | 六月丁香在线视频| 日韩精品免费在线视频| 人体美媚馆| free夫交换videos| 亚洲精品国偷拍自产在线观看蜜臀| 亚洲日韩色少妇无码播放明星| 欧美大肚性孕妇变态孕交| 波多野中文字幕s| 三亚私人高清影院品牌加盟费多少| 老女人hd| 18女下面流水视频图片欣赏| 国产V日韩V欧美综合| 免费观看成年人网站| 亚洲94vvv男人的天堂五月| 穿成对照组后我暴富了| 无人售货成人用品店有什么| 色屁屁屁www在线观看免费影院| 少妇高潮一区二区三区88影院| 女人被添视频| 国产成人精品免费视频动漫| 激烈大尺度叫床的床戏长时间| 国产一级婬片A片免费手机版宅男| 在线AV免费无码一区|