|
|
|
<?php
|
|
|
|
|
|
|
|
namespace jytools;
|
|
|
|
|
|
|
|
use Swoole\Database\RedisConfig;
|
|
...
|
...
|
@@ -8,21 +9,22 @@ class RedisCli |
|
|
|
{
|
|
|
|
private $pool;
|
|
|
|
public $subRedis = null;
|
|
|
|
public function __construct($host,$port,$password,$dbIndex, $num = 20)
|
|
|
|
public function __construct($host, $port, $password, $dbIndex, $num = 20)
|
|
|
|
{
|
|
|
|
$this->pool = new RedisPool((new RedisConfig)
|
|
|
|
->withHost($host)
|
|
|
|
->withPort($port)
|
|
|
|
->withAuth($password)
|
|
|
|
->withDbIndex($dbIndex)
|
|
|
|
,$num //默认64个连接池
|
|
|
|
->withHost($host)
|
|
|
|
->withPort($port)
|
|
|
|
->withAuth($password)
|
|
|
|
->withDbIndex($dbIndex),
|
|
|
|
$num //默认64个连接池
|
|
|
|
);
|
|
|
|
}
|
|
|
|
//集合存数据
|
|
|
|
public function sAdd($key,$member){
|
|
|
|
public function sAdd($key, $member)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->sAdd($key,$member);
|
|
|
|
$res = $redis->sAdd($key, $member);
|
|
|
|
} catch (\RedisException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
...
|
...
|
@@ -31,7 +33,8 @@ class RedisCli |
|
|
|
}
|
|
|
|
|
|
|
|
//获取集合
|
|
|
|
public function sMembers($key){
|
|
|
|
public function sMembers($key)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->sMembers($key);
|
|
...
|
...
|
@@ -43,11 +46,12 @@ class RedisCli |
|
|
|
}
|
|
|
|
|
|
|
|
//移除集合成员
|
|
|
|
public function sRem($key,$member){
|
|
|
|
public function sRem($key, $member)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->sRem($key,$member);
|
|
|
|
}catch (\RedisException $e){
|
|
|
|
$res = $redis->sRem($key, $member);
|
|
|
|
} catch (\RedisException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
...
|
...
|
@@ -56,7 +60,8 @@ class RedisCli |
|
|
|
}
|
|
|
|
|
|
|
|
//哈希 键 字段 值 设置
|
|
|
|
public function hSet($key,$field,$value){
|
|
|
|
public function hSet($key, $field, $value)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->hSet($key, $field, $value);
|
|
...
|
...
|
@@ -67,7 +72,8 @@ class RedisCli |
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
//获取 键对应字段的值
|
|
|
|
public function hGet($key,$field){
|
|
|
|
public function hGet($key, $field)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->hGet($key, $field);
|
|
...
|
...
|
@@ -78,7 +84,8 @@ class RedisCli |
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
//获取键所有字段和值
|
|
|
|
public function hGetAll($key){
|
|
|
|
public function hGetAll($key)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->hGetAll($key);
|
|
...
|
...
|
@@ -89,7 +96,8 @@ class RedisCli |
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
//删除一个值
|
|
|
|
public function hDel($key,$field){
|
|
|
|
public function hDel($key, $field)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->hDel($key, $field);
|
|
...
|
...
|
@@ -104,7 +112,8 @@ class RedisCli |
|
|
|
* 发布订阅等
|
|
|
|
*/
|
|
|
|
//将信息发送到指定的频道
|
|
|
|
public function publish($channel,$message){
|
|
|
|
public function publish($channel, $message)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
try {
|
|
|
|
$res = $redis->publish($channel, $message);
|
|
...
|
...
|
@@ -115,13 +124,13 @@ class RedisCli |
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function subscribe($channels,callable $onMessage)
|
|
|
|
public function subscribe($channels, callable $onMessage)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
$this->subRedis = $redis;
|
|
|
|
try {
|
|
|
|
$res = $redis->subscribe($channels,function($redis, $channel, $message)use($onMessage){
|
|
|
|
call_user_func($onMessage,$redis, $channel, $message);
|
|
|
|
$res = $redis->subscribe($channels, function ($redis, $channel, $message) use ($onMessage) {
|
|
|
|
call_user_func($onMessage, $redis, $channel, $message);
|
|
|
|
});
|
|
|
|
} catch (\RedisException $e) {
|
|
|
|
return false;
|
|
...
|
...
|
@@ -131,20 +140,19 @@ class RedisCli |
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function psubscribe($channels,callable $onMessage)
|
|
|
|
public function psubscribe($channels, callable $onMessage)
|
|
|
|
{
|
|
|
|
$redis = $this->pool->get();
|
|
|
|
$this->subRedis = $redis;
|
|
|
|
try {
|
|
|
|
$res = $redis->psubscribe($channels,function($redis, $pattern, $channel, $message)use($onMessage){
|
|
|
|
$res = $redis->psubscribe($channels, function ($redis, $pattern, $channel, $message) use ($onMessage) {
|
|
|
|
call_user_func($onMessage, $pattern, $redis, $channel, $message);
|
|
|
|
});
|
|
|
|
}catch (\RedisException $e) {
|
|
|
|
} catch (\RedisException $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->subRedis = null;
|
|
|
|
$this->pool->put($redis);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
} |
|
|
\ No newline at end of file |
|
|
|
} |
...
|
...
|
|