9951 explained code solutions for 126 technologies


predisHow can I configure a PHP application to use Redis with a specific timeout?


To configure a PHP application to use Redis with a specific timeout, the following steps should be taken:

  1. Install the Redis extension for PHP. This can be done by running pecl install redis or by downloading and compiling the source code.

  2. Create a Redis instance and set the timeout:

$redis = new Redis();
$redis->connect('127.0.0.1', 6379);
$redis->setOption(Redis::OPT_READ_TIMEOUT, 5);
  1. Use the Redis instance to interact with the data store:
$redis->set('key', 'value');
$value = $redis->get('key');
echo $value; // Outputs 'value'
  1. Close the connection when done:
$redis->close();

The above code creates a Redis instance and sets the timeout to 5 seconds. It then uses the instance to set and get a value from the data store, and finally closes the connection when done.

Helpful links

Edit this code on GitHub