@@ -59,8 +59,6 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
5959 const LUA_CLEAN_SH1 = '42ab2fe548aee5ff540123687a2c39a38b54e4a2 ' ;
6060 const LUA_GC_SH1 = 'c00416b970f1aa6363b44965d4cf60ee99a6f065 ' ;
6161
62- const SENTINEL_CACHE_KEY = 'cm_cache_redis:sentinel ' ;
63-
6462 /** @var Credis_Client */
6563 protected $ _redis ;
6664
@@ -159,21 +157,8 @@ public function __construct($options = array())
159157
160158 $ this ->_clientOptions = $ this ->getClientOptions ($ options );
161159
162- // Try to load selected master from cache
163- if ($ sentinelMaster && ! empty ($ options ['sentinel_master_cache ' ]) && function_exists ('apcu_fetch ' )) {
164- $ cacheData = apcu_fetch (self ::SENTINEL_CACHE_KEY );
165- if ($ cacheData ) {
166- $ this ->_redis = new Credis_Client ($ cacheData ['master ' ]['host ' ], $ cacheData ['master ' ]['port ' ], $ timeout , $ persistent );
167- $ this ->_applyClientOptions ($ this ->_redis );
168- if ( ! empty ($ cacheData ['slave ' ])) {
169- $ this ->_slave = new Credis_Client ($ cacheData ['slave ' ]['host ' ], $ cacheData ['slave ' ]['port ' ], $ timeout , $ persistent );
170- $ this ->_applyClientOptions ($ this ->_slave , TRUE );
171- }
172- }
173- }
174-
175160 // If 'sentinel_master' is specified then server is actually sentinel and master address should be fetched from server.
176- if ($ sentinelMaster && ! $ this -> _redis ) {
161+ if ($ sentinelMaster ) {
177162 $ sentinelClientOptions = isset ($ options ['sentinel ' ]) && is_array ($ options ['sentinel ' ])
178163 ? $ this ->getClientOptions ($ options ['sentinel ' ] + $ options )
179164 : $ this ->_clientOptions ;
@@ -198,6 +183,21 @@ public function __construct($options = array())
198183 ->setClientPersistent ($ this ->_clientOptions ->persistent );
199184 $ redisMaster = $ sentinel ->getMasterClient ($ sentinelMaster );
200185 $ this ->_applyClientOptions ($ redisMaster );
186+
187+ // Verify connected server is actually master as per Sentinel client spec
188+ if ( ! empty ($ options ['sentinel_master_verify ' ])) {
189+ $ roleData = $ redisMaster ->role ();
190+ if ( ! $ roleData || $ roleData [0 ] != 'master ' ) {
191+ usleep (100000 ); // Sleep 100ms and try again
192+ $ redisMaster = $ sentinel ->getMasterClient ($ sentinelMaster );
193+ $ this ->_applyClientOptions ($ redisMaster );
194+ $ roleData = $ redisMaster ->role ();
195+ if ( ! $ roleData || $ roleData [0 ] != 'master ' ) {
196+ Zend_Cache::throwException ('Unable to determine master redis server. ' );
197+ }
198+ }
199+ }
200+
201201 $ this ->_redis = $ redisMaster ;
202202 break ;
203203 } catch (Exception $ e ) {
@@ -229,20 +229,10 @@ public function __construct($options = array())
229229 }
230230 }
231231 unset($ sentinel );
232-
233- // Cache data in memory to skip sentinel
234- if ( ! empty ($ options ['sentinel_master_cache ' ]) && function_exists ('apcu_fetch ' )) {
235- $ cacheData = ['master ' => ['host ' => $ this ->_redis ->getHost (), 'port ' => $ this ->_redis ->getPort ()]];
236- if ($ this ->_slave ) {
237- $ cacheData ['slave ' ] = ['host ' => $ this ->_slave ->getHost (), 'port ' => $ this ->_slave ->getPort ()];
238- }
239- // Save master/slave data from 1 to 15 seconds
240- apcu_store (self ::SENTINEL_CACHE_KEY , $ cacheData , min (15 ,max (1 ,(int )$ options ['sentinel_master_cache ' ])));
241- }
242232 }
243233
244234 // Direct connection to single Redis server
245- else if ( ! $ sentinelMaster ) {
235+ else {
246236 $ this ->_redis = new Credis_Client ($ options ['server ' ], $ port , $ this ->_clientOptions ->timeout , $ this ->_clientOptions ->persistent );
247237 $ this ->_applyClientOptions ($ this ->_redis );
248238
0 commit comments