<?php
include_once ('s/RedisCounter.class.php');
$oRedisCounter = new RedisCounter();
// 定义保存计数的健值
$key = 'mycounter';
// 执行自增计数,获取当前计数,重置计数
//echo $oRedisCounter->incr($key).PHP_EOL; // 1
//echo $oRedisCounter->get($key).PHP_EOL; // 0
?>
<?php
class RedisCounter
{
private $_config;
private $redis = null;
public function flush()
{
return $this->redis->flushDb();
}
/**
* 初始化
* @param Array $config redis连接设定
*/
public function __construct(){
$this->_config =array(
'host' => 'localhost',
'port' => 6379,
'index' => 0,
'auth' => '',
'timeout' => 1,
'reserved' => NULL,
'retry_interval' => 100,
);;
$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;
}
}
?>
博主真是太厉害了!!!
怎么收藏这篇文章?
想想你的文章写的特别好www.jiwenlaw.com
《神兽麻将馆》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/30188.html
黑龙江谷醴传奇酒:价格如何?:https://501h.com/lianji/6951.html
你的文章让我感受到了艺术的魅力,谢谢! http://www.55baobei.com/yOkmkRYnwc.html
《星梦缘》国产剧高清在线免费观看:https://www.jgz518.com/xingkong/30796.html
《逆天邪神 动态漫画第一季》国产动漫高清在线免费观看:https://www.jgz518.com/xingkong/48530.html
这篇文章如同一首动人的乐章,触动了读者内心深处的柔软。
正能量充沛,给予读者行动勇气。
这篇文章提供了宝贵的经验和见解,对读者有很大的启发和帮助。
这是一篇佳作,无论是从内容、语言还是结构上,都堪称完美。