欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

php基于redis計數(shù)器類的示例分析

小編給大家分享一下php基于redis計數(shù)器類的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司2013年成立,先為定遠等服務建站,定遠等地企業(yè),進行企業(yè)商務咨詢服務。為定遠企業(yè)網(wǎng)站制作PC+手機+微官網(wǎng)三網(wǎng)同步一站式服務解決您的所有建站問題。

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í)行結(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)

檢查計數(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基于redis計數(shù)器類的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享名稱:php基于redis計數(shù)器類的示例分析
當前鏈接:http://aaarwkj.com/article30/jeespo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、網(wǎng)頁設計公司、品牌網(wǎng)站設計網(wǎng)站改版、Google、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
懂色一区二区三区精品视频| 男同午夜视频在线观看| 国产乱国产乱老熟部视频| 亚洲区一区二区三区亚洲| 一区二区日韩视频九一蜜桃| 久久国产精品av在线观看| 久久九特黄的免费大片| 亚洲日本久久久午夜精品| 亚洲欧美日韩激情另类| 久久夜色精品亚洲国产| 亚洲精品啪啪一区二区| 国产黄色av网站在线| 精品视频中文字幕天码| 99久久精品免费国产一区| 欧美高清精品在线视频| 中文在线中文天堂黄色片| 亚洲成人免费在线播放| 国产精品午夜福利91| 粉嫩护士国产在线观看| 午夜最新福利在线视频| 亚洲成人日韩在线播放| 欧美在线免费一级黄片| 精品亚洲韩国一区二区三区| 国产91在线一区精品| 中文字幕一区二区精品人妻| 日本欧美一区二区三区高清| 区一区二区三视频日韩| 四虎海外免费永久地址| 日韩熟女av中文字幕| 国产日韩精品欧美综合区| 人妻精品中文字幕一区二区在线| 欧美激情在线精品一区二区| 亚洲三级黄色在线观看| 热热久久这里只有精品| 国产精品一区二区麻豆本子| 91午夜福利视频在线观看| 久久亚洲综合精品人妻| 日本国产一区二区三区在线| 高清免费欧美大片在线观看| 国产九色91中文在线视频| 日韩三级av在线免费观看|