class RedisUtil {
// redis实例对象
private $redis;
// redis服务器列表
private static $serverList = array();
public function __construct($config=array()) {
$this->initRedis($config);
}
/**
* 初始化redis连接实例,每个redis服务器只连接一次
* @param array $config 参数示例:array('host' => '127.0.0.1','port' => 10001,'timeOut' => 10,'password' => '123456')
* @return mixed
*/
private function initRedis($config=array()) {
if (array_key_exists($config['host'],self::$serverList)) {
return $this->redis;
}
$this->redis = new Redis();
// 连接redis服务端
$this->redis->connect($config['host'], $config['port'],$config['timeOut']);
// 密码认证
if (isset($config['password']) && $config['password']) {
$this->redis->auth($config['password']);
}
// 连接失败时,重置$redis实例为null
if (strcasecmp($this->redis->ping(),'+PONG') != 0) {
$this->redis = null;
}
if ($this->redis) {
self::$serverList[$config['host']] = true;
}
if ($this->redis == null) {
die('Redis 连接异常!');
}
}
/**
* 设置字符串缓存
* @param $key
* @param $value
*/
public function set($key,$value) {
$this->redis->set($key, $value);
}
/**
* 获取字符串缓存
* @param $key
* @return bool|string
*/
public function get($key) {
return $this->redis->get($key);
}
/**
* 设置队列缓存(数据左进右出)
* 应用场景:简单的消息系统、最新列表、秒杀/抢购等
* @param $key 队列key名称
* @param $items 队列数据,支持单个数据和一维数组格式
* @return mixed $items为单个数据时,返回被添加元素在队列的第几位(从1开始)
*/
public function setLeftList($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->lPush($key, $item);
}
} else {
return $this->redis->lPush($key, $items);
}
}
/**
* 设置队列缓存(数据右进左出)
* 应用场景:简单的消息系统、最新列表、秒杀/抢购等
* @param $key 队列key名称
* @param $items 队列数据,支持单个数据和一维数组格式
* @return mixed $items为单个数据时,返回被添加元素在队列的第几位(从1开始)
*/
public function setRightList($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->rPush($key, $item);
}
} else {
return $this->redis->rPush($key, $items);
}
}
/**
* 从队列左侧弹出一个
* @param $key
* @return mixed
*/
public function popLeft($key) {
return $this->redis->lPop($key);
}
/**
* 从队列右侧弹出一个
* @param $key
* @return mixed
*/
public function popRight($key) {
return $this->redis->rPop($key);
}
/**
* 获取队列缓存
* @param $key
* @param int $start 开始位置,默认为队列第一个元素
* @param int $end 结束位置,默认为队列最后一个元素
* @return mixed
*/
public function getList($key,$start=0,$end=-1) {
return $this->redis->lRange($key, $start, $end);
}
/**
* 获取队列中的元素个数
* @param $key
* @return mixed
*/
public function getListSize($key) {
return $this->redis->lLen($key);
}
/**
* 删除队列中指定个数的元素值
* @param $key
* @param $value 元素值
* @param int $count 删除的个数 0 全部 | 正数 从左侧删除指定个数 | 负数 从右侧删除指定个数
*/
public function removeList($key,$value,$count=0) {
$this->redis->lRem($key,$value,$count);
}
/**
* 设置hash表缓存
* 应用场景:对象存储
* @param $key hash表key名称
* @param $items hash表数据,一维数组,键值对格式,如:array('key1' => 'item1','key2' => 'item2')
*/
public function setHash($key,$items) {
$this->redis->hmset($key, $items);
}
/**
* 获取hash表缓存
* @param $key hash表key名称
* @param null $hashKey hash表中的key,省略该参数时,获取所有数据
* @return mixed
*/
public function getHash($key,$hashKey=null) {
if ($hashKey) {
return is_string($hashKey) ? $this->redis->hGet($key,$hashKey) : $this->redis->hmget($key, $hashKey);
}
return $this->redis->hGetAll($key);
}
/**
* 获取hash表中的所有key
* @param $key hash表key名称
* @return mixed
*/
public function getHashKeys($key) {
return $this->redis->hKeys($key);
}
/**
* 删除hash
* @param $key hash表key名称
* @param $hashKey hash表中的key,支持单个数据和一维数组
*/
public function removeHash($key,$hashKey) {
if (is_array($hashKey)) {
foreach ($hashKey as $item) {
$this->redis->hDel($key,$item);
}
} else {
$this->redis->hDel($key,$hashKey);
}
}
/**
* 检查指定的hash表中是否存在某个key
* @param $key hash表key名称
* @param $hashKey hash表中的key
* @return mixed true 存在 | false 不存在
*/
public function hashExists($key,$hashKey) {
return $this->redis->hExists($key,$hashKey);
}
/**
* 设置set无序集合
* @param $key 集合key名称
* @param $items 集合数据,支持单个数据和一维数组
*/
public function setCollection($key,$items) {
is_array($items) ? $this->redis->sAddArray($key,$items) : $this->redis->sAdd($key,$items);
}
/**
* 获取set无序集合
* @param $key
* @return mixed
*/
public function getCollection($key) {
return $this->redis->sMembers($key);
}
/**
* 删除set无序集合中的元素
* @param $key 集合key名称
* @param $items 待删除的元素,支持单个数据和一维数组
*/
public function removeCollection($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->sRem($key,$item);
}
} else {
$this->redis->sRem($key,$items);
}
}
/**
* 检查set无序集合中是否包含某个元素
* @param $key 集合key名称
* @param $item 元素
* @return mixed true 包含 | false 不包含
*/
public function collectionExists($key,$item) {
return $this->redis->sIsMember($key, $item);
}
/**
* 获取set无序集合中元素的个数
* @param $key 集合key名称
* @return mixed
*/
public function getCollectionCount($key) {
return $this->redis->sCard($key);
}
/**
* 获取两个set无序集合的交集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionInter($key1,$key2) {
return $this->redis->sInter($key1, $key2);
}
/**
* 获取两个set无序集合的并集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionUnion($key1,$key2) {
return $this->redis->sUnion($key1, $key2);
}
/**
* 获取两个set无序集合的差集
* @param $key1
* @param $key2
* @return mixed
*/
public function getCollectionDiff($key1,$key2) {
return $this->redis->sDiff($key1, $key2);
}
/**
* 设置set有序集合
* @param $key 集合key名称
* @param $items 集合数据,格式:array('score1' => 'item1','score2' => 'item2')
*/
public function setZCollection($key,$items) {
foreach ($items as $score => $item) {
$this->redis->zAdd($key, $score, $item);
}
}
/**
* 获取set有序集合
* 应用场景:排行榜
* @param $key 集合key名称
* @param string $sort 排序方式,asc|desc,默认为增序
* @param int $start 开始位置,默认为集合第一个元素
* @param int $end 结束位置,默认为集合最后一个元素
* @return mixed
*/
public function getZCollection($key,$sort='asc',$start=0,$end=-1) {
if ($sort == 'asc') {
// 成员按分数值递增排序,分数值相同的则按元素字典序来排序
return $this->redis->zRange($key, $start, $end);
}
// 成员按分数值递减排序,分数值相同的则按元素字典序的逆序来排序
return $this->redis->zRevRange($key, $start, $end);
}
/**
* 删除set有序集合中的元素
* @param $key 集合key名称
* @param $items 待删除的元素,支持单个数据和一维数组
*/
public function removeZCollection($key,$items) {
if (is_array($items)) {
foreach ($items as $item) {
$this->redis->zRem($key,$item);
}
} else {
$this->redis->zRem($key,$items);
}
}
/**
* 增加set有序集合中指定元素的score
* 应用场景:计数器、统计点击数等
* @param $key 集合key名称
* @param $item 元素
* @param int $num 增量,默认为1
*/
public function zCollectionIncr($key,$item,$num=1) {
$this->redis->zIncrBy($key, $num, $item);
}
/**
* 检查set有序集合中是否包含某个元素
* @param $key 集合key名称
* @param $item 元素
* @return bool true 包含 | false 不包含
*/
public function zCollectionExists($key,$item) {
if ($this->redis->zScore($key, $item) !== false) {
return true;
}
return false;
}
/**
* 获取set有序集合中元素的个数
* @param $key 集合key名称
* @return mixed
*/
public function getZCollectionCount($key) {
return $this->redis->zCard($key);
}
}
使用方式:
$config = array(
'host' => '127.0.0.1', // redis 服务器地址
'port' => 6379, // redis 服务器端口号
'timeOut' => 10, // redis 客户端连接超时时间
'password' => '123456' // redis 客户端连接密码
);
$redisUtil = new RedisUtil($config);