| 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 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             // Altrimenti utilizzo host e port dalla configurazione della connessione standalone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $nativeRedis->connect($config['host'], $config['port']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         // Autenticazione con username e password (se configurati) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (array_key_exists('username', $config) && $config['username'] !== '' && array_key_exists('password', $config) && $config['password'] !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $nativeRedis->auth([$config['username'], $config['password']]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } elseif (array_key_exists('password', $config) && $config['password'] !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             $nativeRedis->auth($config['password']); // Per versioni Redis senza ACL | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         // Seleziono il database corretto (Per il cluster è sempre 0) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $database = (array_key_exists('database', $config) && $config['database'] !== '') ? (int) $config['database'] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $nativeRedis->select($database); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         //$nativeRedis->setOption(\Redis::OPT_READ_TIMEOUT, -1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         return ['connection' => $nativeRedis, 'database' => $database]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     // Metodo per ottimizzare le operazioni Redis con pipeline | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     public function pipeline($callback, ?string $connection_name = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         return $this->getRedisConnection(($connection_name ?? config('supercache.connection')))->pipeline($callback); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |