83 lines
2.7 KiB
PHP
83 lines
2.7 KiB
PHP
<?php
|
|
|
|
if (!function_exists('get_redis_connection')) {
|
|
/**
|
|
* Redis 연결을 반환하는 헬퍼 함수
|
|
* .env 환경변수에서 설정을 읽어 Redis에 연결합니다.
|
|
*
|
|
* @param string $type 'worker' 또는 'session' (기본값: 'worker')
|
|
* @return Redis|false Redis 객체 또는 연결 실패 시 false
|
|
* @throws RedisException
|
|
*/
|
|
function get_redis_connection(string $type = 'worker')
|
|
{
|
|
$redis = new \Redis();
|
|
|
|
try {
|
|
// 타입에 따라 다른 환경변수 사용
|
|
if ($type === 'session') {
|
|
$host = env('SESSION_REDIS_HOST', '127.0.0.1');
|
|
$port = env('SESSION_REDIS_PORT', '6379');
|
|
$database = env('SESSION_REDIS_DATABASE', '0');
|
|
$password = env('SESSION_REDIS_PASSWORD', '');
|
|
} else {
|
|
// worker, default
|
|
$host = env('REDIS_HOST', '127.0.0.1');
|
|
$port = env('REDIS_PORT', '6379');
|
|
$database = env('REDIS_DATABASE', '9');
|
|
$password = env('REDIS_PASSWORD', '');
|
|
}
|
|
|
|
// Redis 연결 (타임아웃: 0.5초)
|
|
$timeout = 0.5;
|
|
$success = $redis->connect($host, (int)$port, $timeout);
|
|
|
|
if (!$success) {
|
|
log_message('error', "Redis connection failed: {$host}:{$port}");
|
|
return false;
|
|
}
|
|
|
|
// 비밀번호 인증 (있는 경우)
|
|
if (!empty($password)) {
|
|
$redis->auth($password);
|
|
}
|
|
|
|
// 데이터베이스 선택
|
|
$redis->select((int)$database);
|
|
|
|
return $redis;
|
|
|
|
} catch (\RedisException $e) {
|
|
log_message('error', "Redis connection error: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('get_redis_config')) {
|
|
/**
|
|
* Redis 설정 정보를 배열로 반환
|
|
*
|
|
* @param string $type 'worker' 또는 'session'
|
|
* @return array Redis 설정 배열
|
|
*/
|
|
function get_redis_config(string $type = 'worker'): array
|
|
{
|
|
if ($type === 'session') {
|
|
return [
|
|
'host' => env('SESSION_REDIS_HOST', '127.0.0.1'),
|
|
'port' => env('SESSION_REDIS_PORT', '6379'),
|
|
'database' => env('SESSION_REDIS_DATABASE', '0'),
|
|
'password' => env('SESSION_REDIS_PASSWORD', ''),
|
|
];
|
|
}
|
|
|
|
return [
|
|
'host' => env('REDIS_HOST', '127.0.0.1'),
|
|
'port' => env('REDIS_PORT', '6379'),
|
|
'database' => env('REDIS_DATABASE', '9'),
|
|
'password' => env('REDIS_PASSWORD', ''),
|
|
];
|
|
}
|
|
}
|