-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify Redis conn util class structure.
- Loading branch information
steven.chiu
committed
Jun 5, 2019
1 parent
63f6d73
commit c86fc02
Showing
3 changed files
with
73 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,91 @@ | ||
<?php | ||
|
||
class CacheException extends Exception | ||
{ | ||
const REDIS_CONFIG_ERROR = "redis connection error..."; | ||
} | ||
namespace TimeWorker; | ||
|
||
function getRedisConn($redisConf) | ||
class CacheException extends \Exception | ||
{ | ||
return _initRedisPconnect($redisConf); | ||
const REDIS_CONFIG_ERROR = "redis connection error..."; | ||
} | ||
|
||
function _initRedisPconnect($redisConf) | ||
class RedisFactory | ||
{ | ||
// config socket time out 24 hours | ||
ini_set('default_socket_timeout', 86400); | ||
|
||
if (empty($redisConf['host']) || empty($redisConf['port'])) { | ||
throw new CacheException(CacheException::REDIS_CONFIG_ERROR); | ||
public static function getRedisConn($redisConf) | ||
{ | ||
return self::_initRedisPconnect($redisConf); | ||
} | ||
|
||
$host = $redisConf['host']; | ||
$port = $redisConf['port']; | ||
$init = false; | ||
private static function _initRedisPconnect($redisConf) | ||
{ | ||
// config socket time out 24 hours | ||
ini_set('default_socket_timeout', 86400); | ||
|
||
//check redis connect use ping not PONG reconnect | ||
if (!isset($redis)) { | ||
$init = true; | ||
} else { | ||
try { | ||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 1); | ||
$redis->ping(); | ||
} catch (\Exception $exception) { | ||
$redis->close(); | ||
unset($redis); | ||
$init = true; | ||
if (empty($redisConf['host']) || empty($redisConf['port'])) { | ||
throw new CacheException(CacheException::REDIS_CONFIG_ERROR); | ||
} | ||
} | ||
|
||
// init redis | ||
if ($init) { | ||
$index = 1; | ||
$retry = 3; | ||
$host = $redisConf['host']; | ||
$port = $redisConf['port']; | ||
$init = false; | ||
|
||
while ($retry-- > 0) { | ||
//check redis connect use ping not PONG reconnect | ||
if (!isset($redis)) { | ||
$init = true; | ||
} else { | ||
try { | ||
$redis = new \Redis(); | ||
$result = $redis->pconnect($host, $port, 2); //设置pconnect超时时间为1秒 | ||
if ($result == false) { | ||
throw new \Exception('pconnect fail'); | ||
} | ||
if (isset($redisConf['passwd'])) { | ||
$ret = $redis->auth($redisConf['passwd']); | ||
if (!$ret) { | ||
throw new CacheException(CacheException::REDIS_CONTENT_FAIL); | ||
} | ||
} | ||
|
||
if (isset($redisConf['db'])) { | ||
$redis->select($redisConf['db']); | ||
} | ||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 1); | ||
$redis->ping(); | ||
break; | ||
} catch (\Exception $exception) { | ||
print_r("redis info", "redis $host:$port pconnect or ping fail $index times:" . $exception->getMessage()); | ||
$redis->close(); | ||
unset($redis); | ||
$init = true; | ||
} | ||
} | ||
|
||
// init redis | ||
if ($init) { | ||
$index = 1; | ||
$retry = 3; | ||
|
||
$index++; | ||
while ($retry-- > 0) { | ||
try { | ||
$redis = new \Redis(); | ||
$result = $redis->pconnect($host, $port, 2); //设置pconnect超时时间为1秒 | ||
if ($result == false) { | ||
throw new \Exception('pconnect fail'); | ||
} | ||
if (isset($redisConf['passwd'])) { | ||
$ret = $redis->auth($redisConf['passwd']); | ||
if (!$ret) { | ||
throw new CacheException(CacheException::REDIS_CONTENT_FAIL); | ||
} | ||
} | ||
|
||
if (isset($redisConf['db'])) { | ||
$redis->select($redisConf['db']); | ||
} | ||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 1); | ||
$redis->ping(); | ||
break; | ||
} catch (\Exception $exception) { | ||
print_r("redis info", "redis $host:$port pconnect or ping fail $index times:" . $exception->getMessage()); | ||
$redis->close(); | ||
unset($redis); | ||
} | ||
|
||
$index++; | ||
} | ||
if ($retry <= 0) { | ||
print_r("redis fail", "redis $host:$port pconnect and ping fail after try 3 times"); | ||
throw new CacheException(CacheException::REDIS_CONTENT_FAIL); | ||
} | ||
} | ||
if ($retry <= 0) { | ||
print_r("redis fail", "redis $host:$port pconnect and ping fail after try 3 times"); | ||
throw new CacheException(CacheException::REDIS_CONTENT_FAIL); | ||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 3); | ||
if (isset($redisConf['db'])) { | ||
$redis->select($redisConf['db']); | ||
} else { | ||
$redis->select(0); // 默认 DB | ||
} | ||
} | ||
$redis->setOption(\Redis::OPT_READ_TIMEOUT, 3); | ||
if (isset($redisConf['db'])) { | ||
$redis->select($redisConf['db']); | ||
} else { | ||
$redis->select(0); // 默认 DB | ||
} | ||
|
||
return $redis; | ||
return $redis; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters