monospice / laravel-redis-sentinel-drivers

Redis Sentinel integration for Laravel and Lumen.
MIT License
101 stars 48 forks source link

How to check if the client is connected? #14

Closed over-star closed 6 years ago

over-star commented 6 years ago

i want to know that the sentinel is first connected with each connection?then connect redis? I want to know the specific process of connection,please tell me

cyrossignol commented 6 years ago

According to the Predis documentation:

Connections to Redis are lazy meaning that the client connects to a server only if and when needed. While it is recommended to let the client do its own stuff under the hood, there may be times when it is still desired to have control of when the connection is opened or closed: this can easily be achieved by invoking $client->connect() and $client->disconnect()

Normally, the client won't connect to Sentinel/Redis until we execute a command, and we don't need to check each time. The client manages this for us. That said, we can check if the client is connected:

$connected = app('redis-sentinel')->connection('name')->client()->isConnected();

...or simply:

$connected = app('redis-sentinel')->isConnected();

Predis generally follows this process to establish a connection when we execute a Redis command:

  1. If not connected, connect to a Sentinel and query for the master and list of slaves
  2. Pick a master/slave and connect
  3. Send command to selected master/slave
over-star commented 6 years ago

has been star,thanks very mush !

cyrossignol commented 6 years ago

Thanks! I'm going to close this ticket now, but please let me know if you need more information.