php 基于redis计数器类
本文引自网络
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
<?php
Require '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; //
echo $oRedisCounter->incr($key).PHP_EOL; //
echo $oRedisCounter->incr($key, 10).PHP_EOL; //
echo $oRedisCounter->reset($key).PHP_EOL; //
echo $oRedisCounter->get($key).PHP_EOL; // 0
?>
输出:
0
1
11
1
0
2.并发调用计数器,检查计数唯一性
测试代码如下:
<?php
Require '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工具进行测试,设置执行次,个并发。
ab -c 15 -n 150 http://localhost/test.php
执行结果:
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)
检查计数是否唯一
生成的总计数
wc -l /tmp/mytest_result.log
150 /tmp/mytest_result.log 生成的唯一计数
sort -u /tmp/mytest_result.log | wc -l
150
可以看到在并发调用的情况下,生成的计数也保证唯一。
源码下载地址:点击查看
php 基于redis计数器类的更多相关文章
- 项目分布式部署那些事(1):ONS消息队列、基于Redis的Session共享,开源共享
因业务发展需要现在的系统不足以支撑现在的用户量,于是我们在一周之前着手项目的性能优化与分布式部署的相关动作. 概况 现在的系统是基于RabbitHub(一套开源的开发时框架)和Rabbit.WeiXi ...
- 分布式锁 基于Redis
分布式锁的实现(基于Redis) 参考:http://www.jb51.net/article/75439.htm http://www.linuxidc.com/Linux/2015-01/1118 ...
- 基于Redis的CAS服务端集群
为了保证生产环境CAS(Central Authentication Service)认证服务的高可用,防止出现单点故障,我们需要对CAS Server进行集群部署. CAS的Ticket默认是以Ma ...
- 设计模式之PHP项目应用——单例模式设计Memcache和Redis操作类
1 单例模式简单介绍 单例模式是一种经常使用的软件设计模式. 在它的核心结构中仅仅包括一个被称为单例类的特殊类. 通过单例模式能够保证系统中一个类仅仅有一个实例并且该实例易于外界訪问.从而方便对实例个 ...
- 基于Redis缓存的Session共享(附源码)
基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...
- 基于Redis的在线用户列表解决方案
前言: 由于项目需求,需要在集群环境下实现在线用户列表的功能,并依靠在线列表实现用户单一登陆(同一账户只能一处登陆)功能: 在单机环境下,在线列表的实现方案可以采用SessionListener来完成 ...
- [项目回顾]基于Redis的在线用户列表解决方案
迁移:基于Redis的在线用户列表解决方案 前言: 由于项目需求,需要在集群环境下实现在线用户列表的功能,并依靠在线列表实现用户单一登陆(同一账户只能一处登陆)功能: 在单机环境下,在线列表的实现方案 ...
- 记一次企业级爬虫系统升级改造(六):基于Redis实现免费的IP代理池
前言: 首先表示抱歉,春节后一直较忙,未及时更新该系列文章. 近期,由于监控的站源越来越多,就偶有站源做了反爬机制,造成我们的SupportYun系统小爬虫服务时常被封IP,不能进行数据采集. 这时候 ...
- [转载] 基于Redis实现分布式消息队列
转载自http://www.linuxidc.com/Linux/2015-05/117661.htm 1.为什么需要消息队列?当系统中出现“生产“和“消费“的速度或稳定性等因素不一致的时候,就需要消 ...
随机推荐
- vb.net 使用MD5密碼加密
Function MD5(ByVal strSource As String, ByVal Code As Int16) As String'使用MD5加密 Dim dataToHash As Byt ...
- [日常] PHP与Mysql测试kill慢查询并检验PDO的错误模式
<?php try{ //1. pdo的错误模式,抛出异常,不记录到php的error日志,不影响代码继续运行, $opts=array( PDO::ATTR_ERRMODE => PDO ...
- 诡异的楼梯(bfs)hdu1180
诡异的楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submiss ...
- 【C#数据结构系列】线性表
一:线性表 1.1:定义:零个或多个数据元素的有限序列 1.2: 线性表元素个数n定义为线性表的长度,n = 0称为空表,i 为数据元素ai在线性表中的位序. 1.3:满足线性表的条件:(1):有序, ...
- Fragment的坑
http://www.jianshu.com/p/d9143a92ad94 使用add()加入fragment时将触发onAttach(),使用attach()不会触发onAttach() 使用rep ...
- MapReduce运行原理和过程
原文 一.Map的原理和运行流程 Map的输入数据源是多种多样的,我们使用hdfs作为数据源.文件在hdfs上是以block(块,Hdfs上的存储单元)为单位进行存储的. 1.分片 我们将这一个个bl ...
- JS 回调函数、立即执行、for块作用域、try/catch、let、垃圾收集 p3
限于时间关系,加上有些倦意,简单的记录下一些要点: 1.回调函数:就你把函数当成参数传给另一个函数,这个函数在某个时间段会执行这个函数.
- cf232E. Quick Tortoise(分治 bitset dp)
题意 题目链接 Sol 感觉这个思路还是不错的 #include<bits/stdc++.h> using namespace std; const int MAXN = 501, SS ...
- 从零开始学习html(十二)CSS布局模型——上
一.css布局模型 清楚了CSS 盒模型的基本概念. 盒模型类型, 我们就可以深入探讨网页布局的基本模型了. 布局模型与盒模型一样都是 CSS 最基本. 最核心的概念. 但布局模型是建立在盒模型基础之 ...
- svn本地连接服务器失败,但是浏览器可以
tortoise svn无法连接到服务器,清空“Autherticate data”后,再进行更新,提交,log查看等操作,svn还是不提示输入用户名和密码,而是报: error: Unable to ...