PHP语言 基于redis计数器类
小标 2018-07-12 来源 : 阅读 1148 评论 0

摘要:本文主要向大家介绍了PHP语言 基于redis计数器类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

本文主要向大家介绍了PHP语言 基于redis计数器类,通过具体的实例向大家展示,希望对大家学习php语言有所帮助。

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

<?phpRequire '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; // 0echo $oRedisCounter->incr($key).PHP_EOL; // 1echo $oRedisCounter->incr($key, 10).PHP_EOL; // 11echo $oRedisCounter->reset($key).PHP_EOL; // 1echo $oRedisCounter->get($key).PHP_EOL; // 0 ?>

输出:

0

1

11

1

0

 

2.并发调用计数器,检查计数唯一性

测试代码如下:

<?phpRequire '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 //localhost/test.php

执行结果:

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

This is ApacheBench, Version 2.3 <$Revision: 1554214 $>

Copyright 1996 Adam Twiss, Zeus Technology Ltd, //www.zeustech.net/

Licensed to The Apache Software Foundation, //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频道!


本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程