侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

PHP基于redis计数器类定义与用法示例

Redis  /  管理员 发布于 5年前   425

本文实例讲述了PHP基于redis计数器类定义与用法。分享给大家供大家参考,具体如下:

Redis是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。

这里使用其incr(自增),get(获取),delete(清除)方法来实现计数器类。

1.Redis计数器类代码及演示实例

RedisCounter.class.php

_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

 '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?>

输出:

011110

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

测试代码如下:

 '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.phpThis 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).....doneServer Software:    nginx/1.6.3Server Hostname:    localhostServer Port:      80Document Path:     /test.phpDocument Length:    0 bytesConcurrency Level:   15Time taken for tests:  0.173 secondsComplete requests:   150Failed requests:    0Total transferred:   24150 bytesHTML transferred:    0 bytesRequests 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] receivedConnection Times (ms)       min mean[+/-sd] median  maxConnect:    0  0  0.2   0    1Processing:   3  16  3.2   16   23Waiting:    3  16  3.2   16   23Total:     4  16  3.1   17   23Percentage 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相关内容感兴趣的读者可查看本站专题:《php+redis数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《PHP基本语法入门教程》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

  • Redis的使用模式之计数器模式实例
  • Redis实现唯一计数的3种方法分享
  • redis实现计数器-防止刷单方法介绍
  • Redis实现高并发计数器
  • Spring之借助Redis设计一个简单访问计数器的示例
  • Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数示例代码
  • redis通过位图法记录在线用户的状态详解
  • Redis精确去重计数方法(咆哮位图)


  • 上一条:
    PHP使用Redis长连接的方法详解
    下一条:
    php+redis实现商城秒杀功能
  • 昵称:

    邮箱:

    1条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在Redis中能实现的功能、常见应用介绍(0个评论)
    • 2024年Redis面试题之一(0个评论)
    • 在redis缓存常见出错及解决方案(0个评论)
    • 在redis中三种特殊数据类型:地理位置、基数(cardinality)估计、位图(Bitmap)使用场景介绍浅析(2个评论)
    • Redis 删除 key用 del 和 unlink 有啥区别?(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2017-12
    • 2020-03
    • 2020-05
    • 2021-04
    • 2022-03
    • 2022-05
    • 2022-08
    • 2023-02
    • 2023-04
    • 2023-07
    • 2024-01
    • 2024-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客