1

I'm creating a redis connection using phpredis client

$redis = new Redis();
$redis->pconnect(loclahost, 6336, 2) ;
$redis->select(15);

Now I used the $redis object inside an infinite loop.

while(true){
   ///using redis connection object.
}

Around 54 such individual processes were running but once or twice in a day I get an error like "read error on connection".

Please help me to fix it.

John Robertson
  • 1,486
  • 13
  • 16

1 Answers1

0

I would think something like this would work. NOTE I have not tested this, and I have not written PHP in a pretty long time.

function redisConnection() {
    try {
        $redis = new Redis()
        $redis->pconnect(localhost, 6336, 2);
        $redis->select(15);
        $redis->ping();
        return $redis;
    } catch (Exception $e) {
        throw new Exception("Can not connect: " . $e->getMessage());
    }
}

$redis = redisConnection();
while (true) {
    try {
        $redis->ping();
    } catch {
        $redis = redisConnection();
    }
    // Rest of code
}
sberry
  • 128,281
  • 18
  • 138
  • 165
  • Thanks I will test this. is this okay to use ping command every time inside infinite loop? will it affect performance? – user2050435 Aug 11 '14 at 06:14
  • It's about the simplest command you can execute. How many cycles per second is this thing running? – sberry Aug 11 '14 at 06:22
  • Actually each processes used for sending SMS for different areas, I have to check few policy like same number can not more than one SMS per day and similar other policies that is I am managing through Redis. each pick data from DB and send SMS after policy check. – user2050435 Aug 11 '14 at 06:37