国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP基于redis計數器類定義與用法示例

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

2019-10-22 10:59傲雪星楓 PHP教程

這篇文章主要介紹了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)

檢查計數是否唯一

生成的總計數

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

生成的唯一計數

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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产精品国产成人国产三级 | 91色乱码一区二区三区 | 国产日韩精品一区 | 久久久久久国产精品 | 国产精品欧美一区二区三区不卡 | 四虎久久精品 | 欧美日韩中文国产一区发布 | 国产偷亚洲偷欧美偷精品 | 中文av字幕 | 美女久久久久 | 亚洲午夜成激人情在线影院 | 久久久中文| 久久精品国产一区二区三 | 狠狠干美女 | 91亚洲国产精品 | 国内自拍视频网 | 在线播放国产精品 | 91久久综合亚洲鲁鲁五月天 | 成人久久久精品国产乱码一区二区 | 激情五月婷婷在线 | 一区二区乱码 | 一区二区三区在线观看视频 | 曰韩中文字幕 | 久久久久久久国产视频 | 国产精品毛片久久久久久久 | 亚洲黄色特级片 | 国产色秀视频在线观看 | 粉嫩欧美一区二区三区高清影视 | 亚洲精品成人 | 日韩精品久久 | 亚洲精品一区二区三区在线观看 | 国产精品久久久久久久久久久久久 | 日韩欧美国产一区二区 | 性做久久久久久 | a一级免费视频 | 一区在线观看 | 成人久久久久久久久 | 国产精品久久久久久久岛一牛影视 | 青青久草在线 | 91精品国产乱码久久久久久久久 | 91久久精品国产 |