激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - PHP教程 - PHP基于redis計(jì)數(shù)器類定義與用法示例

PHP基于redis計(jì)數(shù)器類定義與用法示例

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

這篇文章主要介紹了PHP基于redis計(jì)數(shù)器類定義與用法,結(jié)合實(shí)例形式較為詳細(xì)的分析了php定義的redis計(jì)數(shù)器類及其相關(guān)使用技巧,需要的朋友可以參考下

本文實(shí)例講述了PHP基于redis計(jì)數(shù)器類定義與用法。分享給大家供大家參考,具體如下:

Redis是一個(gè)開源的使用ANSI C語言編寫、支持網(wǎng)絡(luò)、可基于內(nèi)存亦可持久化的日志型、Key-Value數(shù)據(jù)庫(kù),并提供多種語言的API。

這里使用其incr(自增)get(獲取)delete(清除)方法來實(shí)現(xiàn)計(jì)數(shù)器類。

1.Redis計(jì)數(shù)器類代碼及演示實(shí)例

RedisCounter.class.php

<?php
/**
 * PHP基于Redis計(jì)數(shù)器類
 * Date:  2017-10-28
 * Author: fdipzone
 * Version: 1.0
 *
 * Descripton:
 * php基于Redis實(shí)現(xiàn)自增計(jì)數(shù),主要使用redis的incr方法,并發(fā)執(zhí)行時(shí)保證計(jì)數(shù)自增唯一。
 *
 * Func:
 * public incr  執(zhí)行自增計(jì)數(shù)并獲取自增后的數(shù)值
 * public get   獲取當(dāng)前計(jì)數(shù)
 * public reset  重置計(jì)數(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í)行自增計(jì)數(shù)并獲取自增后的數(shù)值
   * @param String $key 保存計(jì)數(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)前計(jì)數(shù)
   * @param String $key 保存計(jì)數(shù)的健值
   * @return Int
   */
  public function get($key){
    return intval($this->_redis->get($key));
  }
  /**
   * 重置計(jì)數(shù)
   * @param String $key 保存計(jì)數(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

<?php
Require 'RedisCounter.class.php';
// redis連接設(shè)定
$config = array(
  'host' => 'localhost',
  'port' => 6379,
  'index' => 0,
  'auth' => '',
  'timeout' => 1,
  'reserved' => NULL,
  'retry_interval' => 100,
);
// 創(chuàng)建RedisCounter對(duì)象
$oRedisCounter = new RedisCounter($config);
// 定義保存計(jì)數(shù)的健值
$key = 'mycounter';
// 執(zhí)行自增計(jì)數(shù),獲取當(dāng)前計(jì)數(shù),重置計(jì)數(shù)
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ā)調(diào)用計(jì)數(shù)器,檢查計(jì)數(shù)唯一性

測(cè)試代碼如下:

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

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

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

執(zhí)行結(jié)果:

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)

檢查計(jì)數(shù)是否唯一

生成的總計(jì)數(shù)

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

生成的唯一計(jì)數(shù)

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

可以看到在并發(fā)調(diào)用的情況下,生成的計(jì)數(shù)也保證唯一。

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片免费在线视频 | 香蕉久久久精品 | 日韩黄色精品视频 | 亚洲精品成人18久久久久 | 国产精品美女久久久久久网站 | 免费毛片视频 | 激情久久免费视频 | 国内精品久久久久久2021浪潮 | 日韩精品中文字幕在线观看 | 天天色综合2 | 一级爱片 | 久草影音 | 亚洲一级电影在线观看 | 欧美日韩专区国产精品 | 欧美人与牲禽动交精品一区 | 操操操日日日干干干 | 精品一区二区三区电影 | 国产欧美亚洲精品 | 日本久久久网站 | 免费国产自久久久久三四区久久 | 粉色视频污 | 国产一区二区三区影视 | 免费嗨片首页中文字幕 | 国产乱淫av片免费 | 成人爱爱电影 | 国产自在自线午夜精品视频在 | 成人黄色免费观看 | 欧美黄 片免费观看 | 国产精品片一区二区三区 | 国产免费一级淫片a级中文 99国产精品自拍 | 国产一区二区免费在线观看 | 亚洲一区二区 | 国产亚洲精品久久久久久久软件 | 欧美色视 | 亚洲精久久 | 欧美顶级毛片在线播放小说 | av在线免费电影 | 色羞羞 | 艹男人的日日夜夜 | 日韩蜜桃视频 | 国产91精品亚洲精品日韩已满 |