作者 邓超

1

... ... @@ -23,6 +23,15 @@ class RedisPool {
public function __construct()
{
$this->conn();
}
/**
* 链接
* @author:dc
* @time 2023/4/12 15:10
*/
private function conn(){
$this->client = new \Redis();
$this->client->connect(REDIS_HOST,REDIS_PORT,1);
... ... @@ -30,7 +39,6 @@ class RedisPool {
REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD);
// 用库4
$this->client->select(REDIS_DB);
}
... ... @@ -43,7 +51,7 @@ class RedisPool {
*/
public function has($key)
{
return $this->client->exists($key);
return $this->getClient()->exists($key);
}
... ... @@ -56,7 +64,7 @@ class RedisPool {
*/
public function get($key, $default=null)
{
$data = $this->client->get($key);
$data = $this->getClient()->get($key);
if($data === null){
return $default;
}
... ... @@ -72,7 +80,7 @@ class RedisPool {
* @time 2023/2/10 18:02
*/
public function set($key,$val,$ttl=null) {
return $this->client->set($key,$this->serialize($val),$ttl);
return $this->getClient()->set($key,$this->serialize($val),$ttl);
}
... ... @@ -86,7 +94,7 @@ class RedisPool {
* @time 2023/2/10 17:53
*/
public function add($key,$val,$ttl=null):mixed {
return $this->client->eval(
return $this->getClient()->eval(
"return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])",
[$key, $this->serialize($val), $ttl],
1
... ... @@ -101,7 +109,7 @@ class RedisPool {
* @time 2023/2/13 9:07
*/
public function lPush($key,$value){
return $this->client->lPush($key,$this->serialize($value));
return $this->getClient()->lPush($key,$this->serialize($value));
}
/**
... ... @@ -112,7 +120,7 @@ class RedisPool {
* @time 2023/2/13 9:07
*/
public function rPush($key,$value){
return $this->client->rPush($key,$this->serialize($value));
return $this->getClient()->rPush($key,$this->serialize($value));
}
/**
... ... @@ -122,7 +130,7 @@ class RedisPool {
* @time 2023/2/13 9:08
*/
public function lPop($key){
return $this->unserialize($this->client->lPop($key));
return $this->unserialize($this->getClient()->lPop($key));
}
/**
... ... @@ -132,7 +140,7 @@ class RedisPool {
* @time 2023/2/13 9:09
*/
public function rPop($key){
return $this->unserialize($this->client->rPop($key));
return $this->unserialize($this->getClient()->rPop($key));
}
/**
... ... @@ -145,13 +153,13 @@ class RedisPool {
*/
public function incr($key, $ttl = null){
if($ttl){
return $this->client->eval(
return $this->getClient()->eval(
"local x = redis.call('incr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x",
[$key, $ttl],
1
);
}
return $this->client->incr($key);
return $this->getClient()->incr($key);
}
/**
... ... @@ -164,13 +172,13 @@ class RedisPool {
*/
public function decr($key,$ttl = null){
if($ttl){
return $this->client->eval(
return $this->getClient()->eval(
"local x = redis.call('decr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x",
[$key, $ttl],
1
);
}
return $this->client->decr($key);
return $this->getClient()->decr($key);
}
... ... @@ -182,7 +190,7 @@ class RedisPool {
* @time 2023/2/14 14:04
*/
public function delete($key):int {
return $this->client->del($key);
return $this->getClient()->del($key);
}
/**
... ... @@ -193,7 +201,7 @@ class RedisPool {
* @time 2023/3/16 11:36
*/
public function getDel($key){
return $this->client->eval(
return $this->getClient()->eval(
"local x = redis.call('get',KEYS[1]);if x then redis.call('del',KEYS[1]) end return x",
[$key],
1
... ... @@ -221,6 +229,19 @@ class RedisPool {
return $val ? unserialize($val) : '';
}
/**
* @return \Redis
* @author:dc
* @time 2023/4/12 15:11
*/
public function getClient(){
try {
$this->client->ping();
}catch (\RedisException $e){
$this->conn();
}
return $this->client;
}
... ... @@ -240,12 +261,6 @@ class RedisPool {
if(empty(static::$instance[$cid])){
static::$instance[$cid] = new \Lib\RedisPool();
}
// ping失败了说明连接炸了
try {
static::$instance[$cid]->client->ping();
}catch (\RedisException $e){
static::$instance[$cid] = new \Lib\RedisPool();
}
return static::$instance[$cid];
}
... ... @@ -259,9 +274,8 @@ class RedisPool {
public function close(){
// TODO: Implement __destruct() method.
try {
if($this->client->ping()){
$this->client->ping();
$this->client->close();
}
}catch (\RedisException $e){}
$this->client = null;
... ...