周一应该需努力
心中不该有情绪
小雨依旧淅沥沥
愿你仍然很犀利
<?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;
}
}
?>
127.0.0.1:6379> ping
PONG
127.0.0.1:6379> keys *
1) "neteasepic18962177532894457"
2) "neteaseurl258416"
3) "k2"
4) "neteasepic109951164912253150"
5) "neteaselrc165340"
6) "neteaseurl141288"
7) "neteasepic109951163083269878"
8) "neteaselrc351224"
9) "neteaselrc77103"
10) "neteaseurl77103"
11) "neteaseurl5249178"
12) "neteasepic109951164176658680"
13) "neteaselrc258416"
14) "neteaselrc141288"
15) "neteaseurl165340"
16) "neteaseplaylist95102196"
17) "neteasepic109951163167402154"
18) "neteaseurl351224"
19) "neteaseurl258542"
20) "neteasepic19000660439853455"
21) "k1"
22) "neteasepic109951163368468453"
23) "neteaselrc258542"
24) "neteaselrc5249178"
127.0.0.1:6379>