当前位置:Gxlcms > PHP教程 > 一分钟解读php基于redis计数器类

一分钟解读php基于redis计数器类

时间:2021-07-01 10:21:17 帮助过:9人阅读

本篇文章给大家介绍一分钟解读php基于redis计数器类?有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

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

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

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

RedisCounter.class.php

  1. <?php/**
  2. * PHP基于Redis计数器类
  3. * Date: 2017-10-28
  4. * Author: fdipzone
  5. * Version: 1.0
  6. *
  7. * Descripton:
  8. * php基于Redis实现自增计数,主要使用redis的incr方法,并发执行时保证计数自增唯一。
  9. *
  10. * Func:
  11. * public incr 执行自增计数并获取自增后的数值
  12. * public get 获取当前计数
  13. * public reset 重置计数
  14. * private connect 创建redis连接
  15. */class RedisCounter{ // class start
  16. private $_config; private $_redis; /**
  17. * 初始化
  18. * @param Array $config redis连接设定
  19. */
  20. public function __construct($config){
  21. $this->_config = $config;
  22. $this->_redis = $this->connect();
  23. } /**
  24. * 执行自增计数并获取自增后的数值
  25. * @param String $key 保存计数的键值
  26. * @param Int $incr 自增数量,默认为1
  27. * @return Int
  28. */
  29. public function incr($key, $incr=1){ return intval($this->_redis->incr($key, $incr));
  30. } /**
  31. * 获取当前计数
  32. * @param String $key 保存计数的健值
  33. * @return Int
  34. */
  35. public function get($key){ return intval($this->_redis->get($key));
  36. } /**
  37. * 重置计数
  38. * @param String $key 保存计数的健值
  39. * @return Int
  40. */
  41. public function reset($key){ return $this->_redis->delete($key);
  42. } /**
  43. * 创建redis连接
  44. * @return Link
  45. */
  46. private function connect(){ try{
  47. $redis = new Redis();
  48. $redis->connect($this->_config['host'],
  49. $this->_config['port'],$this->_config['timeout'],$this->_config['reserved'],
  50. $this->_config['retry_interval']);
  51. if(empty($this->_config['auth'])){
  52. $redis->auth($this->_config['auth']);
  53. }
  54. $redis->select($this->_config['index']);
  55. }catch(RedisException $e){ throw new Exception($e->getMessage()); return false;
  56. } return $redis;
  57. }
  58. } // class end?>

demo.php

  1. <?php
  2. Require 'RedisCounter.class.php';
  3. // redis连接设定
  4. $config = array(
  5. 'host' => 'localhost',
  6. 'port' => 6379,
  7. 'index' => 0,
  8. 'auth' => '',
  9. 'timeout' => 1,
  10. 'reserved' => NULL,
  11. 'retry_interval' => 100,
  12. );
  13. // 创建RedisCounter对象
  14. $oRedisCounter = new RedisCounter($config);
  15. // 定义保存计数的健值
  16. $key = 'mycounter';
  17. // 执行自增计数,获取当前计数,重置计数
  18. echo $oRedisCounter->get($key).PHP_EOL; // 0
  19. echo $oRedisCounter->incr($key).PHP_EOL; // 1
  20. echo $oRedisCounter->incr($key, 10).PHP_EOL; // 11
  21. echo $oRedisCounter->reset($key).PHP_EOL; // 1
  22. echo $oRedisCounter->get($key).PHP_EOL; // 0
  23. ?>

输出:

  1. 0
  2. 1
  3. 11
  4. 1
  5. 0

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

测试代码如下:

  1. <?php
  2. Require 'RedisCounter.class.php';
  3. // redis连接设定
  4. $config = array(
  5. 'host' => 'localhost',
  6. 'port' => 6379,
  7. 'index' => 0,
  8. 'auth' => '',
  9. 'timeout' => 1,
  10. 'reserved' => NULL,
  11. 'retry_interval' => 100,
  12. );
  13. // 创建RedisCounter对象
  14. $oRedisCounter = new RedisCounter($config);
  15. // 定义保存计数的健值
  16. $key = 'mytestcounter';
  17. // 执行自增计数并返回自增后的计数,记录入临时文件
  18. file_put_contents('/tmp/mytest_result.log', $oRedisCounter->incr($key).PHP_EOL, FILE_APPEND);
  19. ?>

测试并发执行,我们使用ab工具进行测试,设置执行150次,15个并发。

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

执行结果:

  1. ab -c 15 -n 150 http://localhost/test.php
  2. This is ApacheBench, Version 2.3 <$Revision: 1554214 $>
  3. Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
  4. Licensed to The Apache Software Foundation, http://www.apache.org/
  5. Benchmarking home.rabbit.km.com (be patient).....done
  6. Server Software: nginx/1.6.3
  7. Server Hostname: localhost
  8. Server Port: 80
  9. Document Path: /test.php
  10. Document Length: 0 bytes
  11. Concurrency Level: 15
  12. Time taken for tests: 0.173 seconds
  13. Complete requests: 150
  14. Failed requests: 0
  15. Total transferred: 24150 bytes
  16. HTML transferred: 0 bytes
  17. Requests per second: 864.86 [#/sec] (mean)
  18. Time per request: 17.344 [ms] (mean)
  19. Time per request: 1.156 [ms] (mean, across all concurrent requests)
  20. Transfer rate: 135.98 [Kbytes/sec] received
  21. Connection Times (ms)
  22. min mean[+/-sd] median max
  23. Connect: 0 0 0.2 0 1
  24. Processing: 3 16 3.2 16 23
  25. Waiting: 3 16 3.2 16 23
  26. Total: 4 16 3.1 17 23
  27. Percentage of the requests served within a certain time (ms)
  28. 50% 17
  29. 66% 18
  30. 75% 18
  31. 80% 19
  32. 90% 20
  33. 95% 21
  34. 98% 22
  35. 99% 22
  36. 100% 23 (longest request)

检查计数是否唯一

  1. 生成的总计数
  2. wc -l /tmp/mytest_result.log
  3. 150 /tmp/mytest_result.log生成的唯一计数
  4. sort -u /tmp/mytest_result.log | wc -l
  5. 150

可以看到在并发调用的情况下,生成的计数也保证唯一。
推荐学习:php视频教程

以上就是一分钟解读php基于redis计数器类的详细内容,更多请关注gxlcms其它相关文章!

人气教程排行