亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

 更新時間:2018年02月08日 10:53:37   作者:傲雪星楓  
這篇文章主要介紹了PHP基于redis計數(shù)器類定義與用法,結合實例形式較為詳細的分析了php定義的redis計數(shù)器類及其相關使用技巧,需要的朋友可以參考下

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

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

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

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

RedisCounter.class.php

<?php
/**
 * PHP基于Redis計數(shù)器類
 * Date:  2017-10-28
 * Author: fdipzone
 * Version: 1.0
 *
 * Descripton:
 * php基于Redis實現(xiàn)自增計數(shù),主要使用redis的incr方法,并發(fā)執(zhí)行時保證計數(shù)自增唯一。
 *
 * Func:
 * public incr  執(zhí)行自增計數(shù)并獲取自增后的數(shù)值
 * public get   獲取當前計數(shù)
 * public reset  重置計數(shù)
 * 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í)行自增計數(shù)并獲取自增后的數(shù)值
   * @param String $key 保存計數(shù)的鍵值
   * @param Int  $incr 自增數(shù)量,默認為1
   * @return Int
   */
  public function incr($key, $incr=1){
    return intval($this->_redis->incr($key, $incr));
  }
  /**
   * 獲取當前計數(shù)
   * @param String $key 保存計數(shù)的健值
   * @return Int
   */
  public function get($key){
    return intval($this->_redis->get($key));
  }
  /**
   * 重置計數(shù)
   * @param String $key 保存計數(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連接設定
$config = array(
  'host' => 'localhost',
  'port' => 6379,
  'index' => 0,
  'auth' => '',
  'timeout' => 1,
  'reserved' => NULL,
  'retry_interval' => 100,
);
// 創(chuàng)建RedisCounter對象
$oRedisCounter = new RedisCounter($config);
// 定義保存計數(shù)的健值
$key = 'mycounter';
// 執(zhí)行自增計數(shù),獲取當前計數(shù),重置計數(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)用計數(shù)器,檢查計數(shù)唯一性

測試代碼如下:

<?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);
// 定義保存計數(shù)的健值
$key = 'mytestcounter';
// 執(zhí)行自增計數(shù)并返回自增后的計數(shù),記錄入臨時文件
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)

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

生成的總計數(shù)

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

生成的唯一計數(shù)

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

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

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

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

相關文章

  • php類自動加載失敗的處理方案及實例代碼

    php類自動加載失敗的處理方案及實例代碼

    在本篇文章里小編給大家整理了一篇關于php類自動加載失敗的處理方案及實例代碼,有興趣的朋友們可以學習參考下。
    2021-09-09
  • PHP遞歸調(diào)用數(shù)組值并用其執(zhí)行指定函數(shù)的方法

    PHP遞歸調(diào)用數(shù)組值并用其執(zhí)行指定函數(shù)的方法

    這篇文章主要介紹了PHP遞歸調(diào)用數(shù)組值并用其執(zhí)行指定函數(shù)的方法,涉及php數(shù)組調(diào)用與函數(shù)執(zhí)行的技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-04-04
  • 同臺服務器使用緩存APC效率高于Memcached的演示代碼

    同臺服務器使用緩存APC效率高于Memcached的演示代碼

    之前看到有文章說同臺服務器上APC的效率是Memcached的7倍,APC效率比Memcached高是肯定的,至于倒底快多少,我寫了個小程序測試了下。
    2010-02-02
  • PHP自定義函數(shù)實現(xiàn)數(shù)組比較功能示例

    PHP自定義函數(shù)實現(xiàn)數(shù)組比較功能示例

    這篇文章主要介紹了PHP自定義函數(shù)實現(xiàn)數(shù)組比較功能,涉及php針對數(shù)組的遍歷、比較、判斷等相關操作技巧,需要的朋友可以參考下
    2017-10-10
  • php對大文件進行讀取操作的實現(xiàn)代碼

    php對大文件進行讀取操作的實現(xiàn)代碼

    在php中,對于文件的讀取時,最快捷的方式莫過于使用一些諸如file、file_get_contents之類的函數(shù),簡簡單單的幾行代碼就能很漂亮的完成我們所需要的功能。但當所操作的文件是一個比較大的文件時,這些函數(shù)可能就顯的力不從心, 下面將從一個需求入手來說明對于讀取大文件時,常用的操作方法
    2013-01-01
  • PHP中error_reporting()函數(shù)的用法(修改PHP屏蔽錯誤)

    PHP中error_reporting()函數(shù)的用法(修改PHP屏蔽錯誤)

    一般在默認的普通PHP文件中輸出一個未定義聲明的變量是不會報錯誤的,但在codeigniter框架下卻要報錯誤,這對于想集成 添加 和 修改 頁面于一體的”懶人”很不方便,由于是初學者開始還想怎么在代碼中屏蔽這一錯誤提示呢.甚至用到了@,但聽很多人都說@會大大降低性能.
    2011-07-07
  • php實現(xiàn)mysql同步的實現(xiàn)方法

    php實現(xiàn)mysql同步的實現(xiàn)方法

    由于公司的英文網(wǎng)站放置在美國,而這些網(wǎng)站的數(shù)據(jù)要與大陸的服務器數(shù)據(jù)同步。 同步時間在一天之內(nèi)。
    2009-10-10
  • PHP設計模式之迭代器模式淺析

    PHP設計模式之迭代器模式淺析

    迭代器(Iterator)模式,它在一個很常見的過程上提供了一個抽象:位于對象圖不明部分的一組對象(或標量)集合上的迭代。迭代有幾種不同的具體執(zhí)行方法:在數(shù)組屬性,集合對象,數(shù)組,甚至一個查詢結果集之上迭代
    2023-04-04
  • PHP中“簡單工廠模式”實例代碼講解

    PHP中“簡單工廠模式”實例代碼講解

    PHP中簡單工廠模式實例代碼,學習php類的朋友可以參考下
    2012-09-09
  • php 如何設置一個嚴格控制過期時間的session

    php 如何設置一個嚴格控制過期時間的session

    本篇文章主要介紹了php設置一個嚴格控制過期時間的session的方法,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-05-05

最新評論