<?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
?>
没有合适的资源?快使用搜索试试~ 我知道了~
PHP基于Redis计数器类
共2个文件
php:2个
5星 · 超过95%的资源 需积分: 50 9 下载量 40 浏览量
2017-10-28
15:32:10
上传
评论
收藏 1KB ZIP 举报
温馨提示
php基于Redis实现自增计数,主要使用redis的incr方法,并发执行时保证计数自增唯一。
资源推荐
资源详情
资源评论
收起资源包目录
RedisCounter.zip (2个子文件)
demo.php 650B
RedisCounter.class.php 2KB
共 2 条
- 1
资源评论
- 九月Index2018-09-10还可以!!!!!!!!!!
傲雪星枫
- 粉丝: 2304
- 资源: 74
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功