本文实例讲述了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; // 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.并发调用计数器,检查计数唯一性
测试代码如下:
<?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工具进行测试,设置执行150次,15个并发。
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相关内容感兴趣的读者可查看本站专题:《php+redis数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php基本语法入门教程》、《php数组(array)操作技巧大全》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!