正在显示
1 个修改的文件
包含
38 行增加
和
24 行删除
| @@ -23,6 +23,15 @@ class RedisPool { | @@ -23,6 +23,15 @@ class RedisPool { | ||
| 23 | 23 | ||
| 24 | public function __construct() | 24 | public function __construct() | 
| 25 | { | 25 | { | 
| 26 | + $this->conn(); | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + /** | ||
| 30 | + * 链接 | ||
| 31 | + * @author:dc | ||
| 32 | + * @time 2023/4/12 15:10 | ||
| 33 | + */ | ||
| 34 | + private function conn(){ | ||
| 26 | $this->client = new \Redis(); | 35 | $this->client = new \Redis(); | 
| 27 | 36 | ||
| 28 | $this->client->connect(REDIS_HOST,REDIS_PORT,1); | 37 | $this->client->connect(REDIS_HOST,REDIS_PORT,1); | 
| @@ -30,7 +39,6 @@ class RedisPool { | @@ -30,7 +39,6 @@ class RedisPool { | ||
| 30 | REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD); | 39 | REDIS_PASSWORD && $this->client->auth(REDIS_PASSWORD); | 
| 31 | // 用库4 | 40 | // 用库4 | 
| 32 | $this->client->select(REDIS_DB); | 41 | $this->client->select(REDIS_DB); | 
| 33 | - | ||
| 34 | } | 42 | } | 
| 35 | 43 | ||
| 36 | 44 | ||
| @@ -43,7 +51,7 @@ class RedisPool { | @@ -43,7 +51,7 @@ class RedisPool { | ||
| 43 | */ | 51 | */ | 
| 44 | public function has($key) | 52 | public function has($key) | 
| 45 | { | 53 | { | 
| 46 | - return $this->client->exists($key); | 54 | + return $this->getClient()->exists($key); | 
| 47 | } | 55 | } | 
| 48 | 56 | ||
| 49 | 57 | ||
| @@ -56,7 +64,7 @@ class RedisPool { | @@ -56,7 +64,7 @@ class RedisPool { | ||
| 56 | */ | 64 | */ | 
| 57 | public function get($key, $default=null) | 65 | public function get($key, $default=null) | 
| 58 | { | 66 | { | 
| 59 | - $data = $this->client->get($key); | 67 | + $data = $this->getClient()->get($key); | 
| 60 | if($data === null){ | 68 | if($data === null){ | 
| 61 | return $default; | 69 | return $default; | 
| 62 | } | 70 | } | 
| @@ -72,7 +80,7 @@ class RedisPool { | @@ -72,7 +80,7 @@ class RedisPool { | ||
| 72 | * @time 2023/2/10 18:02 | 80 | * @time 2023/2/10 18:02 | 
| 73 | */ | 81 | */ | 
| 74 | public function set($key,$val,$ttl=null) { | 82 | public function set($key,$val,$ttl=null) { | 
| 75 | - return $this->client->set($key,$this->serialize($val),$ttl); | 83 | + return $this->getClient()->set($key,$this->serialize($val),$ttl); | 
| 76 | } | 84 | } | 
| 77 | 85 | ||
| 78 | 86 | ||
| @@ -86,7 +94,7 @@ class RedisPool { | @@ -86,7 +94,7 @@ class RedisPool { | ||
| 86 | * @time 2023/2/10 17:53 | 94 | * @time 2023/2/10 17:53 | 
| 87 | */ | 95 | */ | 
| 88 | public function add($key,$val,$ttl=null):mixed { | 96 | public function add($key,$val,$ttl=null):mixed { | 
| 89 | - return $this->client->eval( | 97 | + return $this->getClient()->eval( | 
| 90 | "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", | 98 | "return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", | 
| 91 | [$key, $this->serialize($val), $ttl], | 99 | [$key, $this->serialize($val), $ttl], | 
| 92 | 1 | 100 | 1 | 
| @@ -101,7 +109,7 @@ class RedisPool { | @@ -101,7 +109,7 @@ class RedisPool { | ||
| 101 | * @time 2023/2/13 9:07 | 109 | * @time 2023/2/13 9:07 | 
| 102 | */ | 110 | */ | 
| 103 | public function lPush($key,$value){ | 111 | public function lPush($key,$value){ | 
| 104 | - return $this->client->lPush($key,$this->serialize($value)); | 112 | + return $this->getClient()->lPush($key,$this->serialize($value)); | 
| 105 | } | 113 | } | 
| 106 | 114 | ||
| 107 | /** | 115 | /** | 
| @@ -112,7 +120,7 @@ class RedisPool { | @@ -112,7 +120,7 @@ class RedisPool { | ||
| 112 | * @time 2023/2/13 9:07 | 120 | * @time 2023/2/13 9:07 | 
| 113 | */ | 121 | */ | 
| 114 | public function rPush($key,$value){ | 122 | public function rPush($key,$value){ | 
| 115 | - return $this->client->rPush($key,$this->serialize($value)); | 123 | + return $this->getClient()->rPush($key,$this->serialize($value)); | 
| 116 | } | 124 | } | 
| 117 | 125 | ||
| 118 | /** | 126 | /** | 
| @@ -122,7 +130,7 @@ class RedisPool { | @@ -122,7 +130,7 @@ class RedisPool { | ||
| 122 | * @time 2023/2/13 9:08 | 130 | * @time 2023/2/13 9:08 | 
| 123 | */ | 131 | */ | 
| 124 | public function lPop($key){ | 132 | public function lPop($key){ | 
| 125 | - return $this->unserialize($this->client->lPop($key)); | 133 | + return $this->unserialize($this->getClient()->lPop($key)); | 
| 126 | } | 134 | } | 
| 127 | 135 | ||
| 128 | /** | 136 | /** | 
| @@ -132,7 +140,7 @@ class RedisPool { | @@ -132,7 +140,7 @@ class RedisPool { | ||
| 132 | * @time 2023/2/13 9:09 | 140 | * @time 2023/2/13 9:09 | 
| 133 | */ | 141 | */ | 
| 134 | public function rPop($key){ | 142 | public function rPop($key){ | 
| 135 | - return $this->unserialize($this->client->rPop($key)); | 143 | + return $this->unserialize($this->getClient()->rPop($key)); | 
| 136 | } | 144 | } | 
| 137 | 145 | ||
| 138 | /** | 146 | /** | 
| @@ -145,13 +153,13 @@ class RedisPool { | @@ -145,13 +153,13 @@ class RedisPool { | ||
| 145 | */ | 153 | */ | 
| 146 | public function incr($key, $ttl = null){ | 154 | public function incr($key, $ttl = null){ | 
| 147 | if($ttl){ | 155 | if($ttl){ | 
| 148 | - return $this->client->eval( | 156 | + return $this->getClient()->eval( | 
| 149 | "local x = redis.call('incr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x", | 157 | "local x = redis.call('incr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x", | 
| 150 | [$key, $ttl], | 158 | [$key, $ttl], | 
| 151 | 1 | 159 | 1 | 
| 152 | ); | 160 | ); | 
| 153 | } | 161 | } | 
| 154 | - return $this->client->incr($key); | 162 | + return $this->getClient()->incr($key); | 
| 155 | } | 163 | } | 
| 156 | 164 | ||
| 157 | /** | 165 | /** | 
| @@ -164,13 +172,13 @@ class RedisPool { | @@ -164,13 +172,13 @@ class RedisPool { | ||
| 164 | */ | 172 | */ | 
| 165 | public function decr($key,$ttl = null){ | 173 | public function decr($key,$ttl = null){ | 
| 166 | if($ttl){ | 174 | if($ttl){ | 
| 167 | - return $this->client->eval( | 175 | + return $this->getClient()->eval( | 
| 168 | "local x = redis.call('decr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x", | 176 | "local x = redis.call('decr',KEYS[1]);redis.call('expire',KEYS[1],ARGV[1]);return x", | 
| 169 | [$key, $ttl], | 177 | [$key, $ttl], | 
| 170 | 1 | 178 | 1 | 
| 171 | ); | 179 | ); | 
| 172 | } | 180 | } | 
| 173 | - return $this->client->decr($key); | 181 | + return $this->getClient()->decr($key); | 
| 174 | } | 182 | } | 
| 175 | 183 | ||
| 176 | 184 | ||
| @@ -182,7 +190,7 @@ class RedisPool { | @@ -182,7 +190,7 @@ class RedisPool { | ||
| 182 | * @time 2023/2/14 14:04 | 190 | * @time 2023/2/14 14:04 | 
| 183 | */ | 191 | */ | 
| 184 | public function delete($key):int { | 192 | public function delete($key):int { | 
| 185 | - return $this->client->del($key); | 193 | + return $this->getClient()->del($key); | 
| 186 | } | 194 | } | 
| 187 | 195 | ||
| 188 | /** | 196 | /** | 
| @@ -193,7 +201,7 @@ class RedisPool { | @@ -193,7 +201,7 @@ class RedisPool { | ||
| 193 | * @time 2023/3/16 11:36 | 201 | * @time 2023/3/16 11:36 | 
| 194 | */ | 202 | */ | 
| 195 | public function getDel($key){ | 203 | public function getDel($key){ | 
| 196 | - return $this->client->eval( | 204 | + return $this->getClient()->eval( | 
| 197 | "local x = redis.call('get',KEYS[1]);if x then redis.call('del',KEYS[1]) end return x", | 205 | "local x = redis.call('get',KEYS[1]);if x then redis.call('del',KEYS[1]) end return x", | 
| 198 | [$key], | 206 | [$key], | 
| 199 | 1 | 207 | 1 | 
| @@ -221,6 +229,19 @@ class RedisPool { | @@ -221,6 +229,19 @@ class RedisPool { | ||
| 221 | return $val ? unserialize($val) : ''; | 229 | return $val ? unserialize($val) : ''; | 
| 222 | } | 230 | } | 
| 223 | 231 | ||
| 232 | + /** | ||
| 233 | + * @return \Redis | ||
| 234 | + * @author:dc | ||
| 235 | + * @time 2023/4/12 15:11 | ||
| 236 | + */ | ||
| 237 | + public function getClient(){ | ||
| 238 | + try { | ||
| 239 | + $this->client->ping(); | ||
| 240 | + }catch (\RedisException $e){ | ||
| 241 | + $this->conn(); | ||
| 242 | + } | ||
| 243 | + return $this->client; | ||
| 244 | + } | ||
| 224 | 245 | ||
| 225 | 246 | ||
| 226 | 247 | ||
| @@ -240,12 +261,6 @@ class RedisPool { | @@ -240,12 +261,6 @@ class RedisPool { | ||
| 240 | if(empty(static::$instance[$cid])){ | 261 | if(empty(static::$instance[$cid])){ | 
| 241 | static::$instance[$cid] = new \Lib\RedisPool(); | 262 | static::$instance[$cid] = new \Lib\RedisPool(); | 
| 242 | } | 263 | } | 
| 243 | - // ping失败了说明连接炸了 | ||
| 244 | - try { | ||
| 245 | - static::$instance[$cid]->client->ping(); | ||
| 246 | - }catch (\RedisException $e){ | ||
| 247 | - static::$instance[$cid] = new \Lib\RedisPool(); | ||
| 248 | - } | ||
| 249 | 264 | ||
| 250 | return static::$instance[$cid]; | 265 | return static::$instance[$cid]; | 
| 251 | } | 266 | } | 
| @@ -259,9 +274,8 @@ class RedisPool { | @@ -259,9 +274,8 @@ class RedisPool { | ||
| 259 | public function close(){ | 274 | public function close(){ | 
| 260 | // TODO: Implement __destruct() method. | 275 | // TODO: Implement __destruct() method. | 
| 261 | try { | 276 | try { | 
| 262 | - if($this->client->ping()){ | ||
| 263 | - $this->client->close(); | ||
| 264 | - } | 277 | + $this->client->ping(); | 
| 278 | + $this->client->close(); | ||
| 265 | }catch (\RedisException $e){} | 279 | }catch (\RedisException $e){} | 
| 266 | 280 | ||
| 267 | $this->client = null; | 281 | $this->client = null; | 
- 
请 注册 或 登录 后发表评论