Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instruct predis to continue on error

Tags:

php

redis

predis

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]
like image 300
AFRC Avatar asked Mar 03 '23 06:03

AFRC


1 Answers

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.

like image 165
Leif Högberg Avatar answered Mar 06 '23 23:03

Leif Högberg