2024年4月2日发(作者:)
$auth = $this->objRedis->auth($this->redisPassword);
}catch (Exception $e) {
echo $e->getMessage();exit;
}
//选择数据库
$this->objRedis->select($this->redisDbName);
}
/**
* 单例
* @param $config
* @return JJWXCRedisDemo
*/
public static function getInstance($config){
if(self::$instance == NULL){
self::$instance = new self($config);
}
return self::$instance;
}
/**
* 添加hash数据
* @param $id
* @param $info
*/
public function setHash($id,array $info)
{
if (!is_numeric($id) || !is_array($info) || empty($info)){
return false;
}
$redisHashKey = $this->redisPrefix.'_'.$id;
$this->objRedis->hMSet($redisHashKey,$info);
}
/**
* 获取hash数据
* @param $keys
* @return array
*/
public function getHash($keys)
{
$res = array();
if (is_string($keys)){
$res = $this->objRedis->hGetAll($this->redisPrefix.'_'.$keys);
}
if (is_array($keys)){
foreach ($keys as $v){
$res[$v] = $this->objRedis->hGetAll($this->redisPrefix.'_'.$v);
}
}
return $res;
}
/**
* 向某个有序集合添加数据
* @param $score
* @param $value
* @param $key
* @return bool
*/
public function setZSet($score,$value,$key)
{
if (empty($score) || empty($value)){
return false;
}
if (!empty($key)){
$this->key = $key;
}
$this->objRedis->zAdd($this->redisPrefix.$this->key,array(),$score,$score);
}
/**
* 获取有序集合长度
* @param $key
* @return int
*/
public function getZSetTotal($key)
{
if (!empty($key)){
$this->key = $key;
}
$name = $this->redisPrefix.$this->key;
$total = $this->objRedis->zCard($name);
return $total;
发布评论