@@ -38,7 +38,9 @@ class PredisHandler extends BaseHandler
3838 * port: int,
3939 * async: bool,
4040 * persistent: bool,
41- * timeout: int
41+ * timeout: int,
42+ * sentinels: list<string>,
43+ * service: string,
4244 * }
4345 */
4446 protected $ config = [
@@ -49,6 +51,8 @@ class PredisHandler extends BaseHandler
4951 'async ' => false ,
5052 'persistent ' => false ,
5153 'timeout ' => 0 ,
54+ 'sentinels ' => [],
55+ 'service ' => '' ,
5256 ];
5357
5458 /**
@@ -68,16 +72,108 @@ public function __construct(Cache $config)
6872 $ this ->config = array_merge ($ this ->config , $ config ->redis );
6973 }
7074
75+ /**
76+ * Factory method for creating Predis Client instances.
77+ * Override in tests to mock Client.
78+ */
79+ protected function createPredisClient (array $ connection , array $ options = []): Client
80+ {
81+ return new Client ($ connection , $ options );
82+ }
83+
7184 public function initialize (): void
7285 {
7386 try {
74- $ this ->redis = new Client ($ this ->config , ['prefix ' => $ this ->prefix ]);
87+ $ config = $ this ->config ;
88+
89+ if ($ config ['sentinels ' ] !== [] && $ config ['service ' ] !== '' ) {
90+ $ this ->initializeSentinel ($ config );
91+
92+ return ;
93+ }
94+
95+ unset($ config ['sentinels ' ], $ config ['service ' ]);
96+
97+ $ this ->redis = $ this ->createPredisClient ($ config , ['prefix ' => $ this ->prefix ]);
7598 $ this ->redis ->time ();
7699 } catch (Exception $ e ) {
77100 throw new CriticalError ('Cache: Predis connection refused ( ' . $ e ->getMessage () . '). ' , $ e ->getCode (), $ e );
78101 }
79102 }
80103
104+ /**
105+ * Initializes a connection via Redis Sentinel.
106+ *
107+ * Predis v3.x does not have built-in sentinel support,
108+ * so we manually discover the master via the Sentinel protocol.
109+ *
110+ * @param array{
111+ * sentinels: list<string>,
112+ * service: string,
113+ * timeout: int,
114+ * password?: string|null,
115+ * database?: int,
116+ * prefix?: string,
117+ * } $config
118+ */
119+ protected function initializeSentinel (array $ config ): void
120+ {
121+ $ sentinels = $ config ['sentinels ' ];
122+ $ service = $ config ['service ' ];
123+ $ timeout = $ config ['timeout ' ] ?? 0 ;
124+
125+ $ masterHost = null ;
126+ $ masterPort = null ;
127+
128+ foreach ($ sentinels as $ sentinel ) {
129+ $ parts = parse_url ($ sentinel );
130+ $ sentinelHost = $ parts ['host ' ] ?? '127.0.0.1 ' ;
131+ $ sentinelPort = $ parts ['port ' ] ?? 26379 ;
132+ $ sentinelScheme = $ parts ['scheme ' ] ?? 'tcp ' ;
133+
134+ try {
135+ $ sentinelClient = $ this ->createPredisClient ([
136+ 'scheme ' => $ sentinelScheme ,
137+ 'host ' => $ sentinelHost ,
138+ 'port ' => $ sentinelPort ,
139+ 'timeout ' => $ timeout ,
140+ ]);
141+
142+ if (isset ($ config ['password ' ]) && $ config ['password ' ] !== null ) {
143+ $ sentinelClient ->auth ($ config ['password ' ]);
144+ }
145+
146+ $ addr = $ sentinelClient ->rawCommand ('SENTINEL ' , 'get-master-addr-by-name ' , $ service );
147+
148+ if (is_array ($ addr ) && count ($ addr ) >= 2 ) {
149+ $ masterHost = $ addr [0 ];
150+ $ masterPort = (int ) $ addr [1 ];
151+ }
152+
153+ $ sentinelClient ->disconnect ();
154+ unset($ sentinelClient );
155+
156+ if ($ masterHost !== null ) {
157+ break ;
158+ }
159+ } catch (Exception ) {
160+ continue ;
161+ }
162+ }
163+
164+ if ($ masterHost === null ) {
165+ throw new CriticalError ('Cache: Redis Sentinel could not find a master for service " ' . $ service . '". ' );
166+ }
167+
168+ $ config ['scheme ' ] = 'tcp ' ;
169+ $ config ['host ' ] = $ masterHost ;
170+ $ config ['port ' ] = $ masterPort ;
171+ unset($ config ['sentinels ' ], $ config ['service ' ], $ config ['async ' ], $ config ['persistent ' ]);
172+
173+ $ this ->redis = $ this ->createPredisClient ($ config , ['prefix ' => $ this ->prefix ]);
174+ $ this ->redis ->time ();
175+ }
176+
81177 public function get (string $ key ): mixed
82178 {
83179 $ key = static ::validateKey ($ key );
0 commit comments