1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\SuperCache; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Redis; |
6
|
|
|
|
7
|
|
|
class RedisConnector |
8
|
|
|
{ |
9
|
|
|
public function getRedisConnection(?string $connection_name = null): \Illuminate\Redis\Connections\Connection |
10
|
|
|
{ |
11
|
|
|
return Redis::connection($connection_name ?? config('supercache.connection')); |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Establishes a native Redis connection based on the provided connection name and optional host and port. |
16
|
|
|
* |
17
|
|
|
* @param string|null $connection_name The name of the Redis connection configuration to use. Defaults to 'default'. |
18
|
|
|
* @param string|null $host The hostname to use for the connection. If not provided, it will be retrieved from the configuration. |
19
|
|
|
* @param string|null $port The port number to use for the connection. If not provided, it will be retrieved from the configuration. |
20
|
|
|
* @return array|null Returns an associative array with the Redis connection instance and the selected database, or null on failure. |
21
|
|
|
* The array contains: |
22
|
|
|
* - 'connection': The instance of the native Redis connection. |
23
|
|
|
* - 'database': The selected database index. |
24
|
|
|
*/ |
25
|
|
|
public function getNativeRedisConnection(?string $connection_name = null, ?string $host = null, ?string $port = null): ?array |
26
|
|
|
{ |
27
|
|
|
// Crea una nuova connessione nativa Redis |
28
|
|
|
$config = config('database.redis.clusters.' . ($connection_name ?? config('supercache.connection'))); |
29
|
|
|
if ($config !== null && ($host === null || $port === null)) { |
30
|
|
|
// Sono nel caso del cluster, host e port sono obbligatori in quanto le connessioni vanno stabilite per ogni nodo del cluster |
31
|
|
|
throw new \RuntimeException('Host e port non possono essere null per le connessioni in cluster'); |
32
|
|
|
} |
33
|
|
|
if ($config === null) { |
34
|
|
|
// Sono nel caso di una connessione standalone |
35
|
|
|
$config = config('database.redis.' . ($connection_name ?? config('supercache.connection'))); |
36
|
|
|
} |
37
|
|
|
$nativeRedis = new \Redis(); |
38
|
|
|
if ($host !== null && $port !== null) { |
39
|
|
|
// Se ho host e port (caso del cluster) uso questi |
40
|
|
|
//$nativeRedis->connect($host, $port); |
41
|
|
|
$nativeRedis->connect($host, $port, 0, null, 0, -1); |
|
|
|
|
42
|
|
|
} else { |
43
|
|
|
// Altrimenti utilizzo host e port dalla configurazione della connessione standalone |
44
|
|
|
$nativeRedis->connect($config['host'], $config['port'], 0, null, 0, -1); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// Autenticazione con username e password (se configurati) |
48
|
|
|
if (array_key_exists('username', $config) && $config['username'] !== '' && array_key_exists('password', $config) && $config['password'] !== '') { |
49
|
|
|
$nativeRedis->auth([$config['username'], $config['password']]); |
|
|
|
|
50
|
|
|
} elseif (array_key_exists('password', $config) && $config['password'] !== '') { |
51
|
|
|
$nativeRedis->auth($config['password']); // Per versioni Redis senza ACL |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Seleziono il database corretto (Per il cluster è sempre 0) |
55
|
|
|
$database = (array_key_exists('database', $config) && $config['database'] !== '') ? (int) $config['database'] : 0; |
56
|
|
|
$nativeRedis->select($database); |
57
|
|
|
//$nativeRedis->setOption(\Redis::OPT_READ_TIMEOUT, -1); |
58
|
|
|
|
59
|
|
|
return ['connection' => $nativeRedis, 'database' => $database]; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Metodo per ottimizzare le operazioni Redis con pipeline |
63
|
|
|
public function pipeline($callback, ?string $connection_name = null) |
64
|
|
|
{ |
65
|
|
|
return $this->getRedisConnection(($connection_name ?? config('supercache.connection')))->pipeline($callback); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|