In case of Redis failure, is that possible to instruct php predis (https://github.com/nrk/predis) to continue and to not die?
I have Redis to handle application cache, but the application can run without cache, it just hits the database heavier. I prefer to fallback to database then have the application dying. I cant find a way to instruct predis to continue on fail.
I thought in set the connection limit to about 5 seconds, if it cant connect to Redis the application should go on.
Is this possible?
Fatal error: Uncaught Predis\Connection\ConnectionException: Operation timed out [tcp://128.0.0.1:6379]
You can catch the connection exception and fallback to your database. Example:
try {
return $predis->get('foobar');
} catch(\Predis\Connection\ConnectionException $ex) {
// fallback to database call
}
For cleaner code wrap the database/redis call in a new class that abstracts away the actual connection, that way your calling code doesn't need to care which datasource was used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With