predisHow can I set a timeout for a Redis connection using PHP?
You can set a timeout for a Redis connection using PHP by using the connect method of the Redis class. This method accepts an optional timeout parameter, which is the maximum time in seconds to wait for a response from the server.
$redis = new Redis();
$redis->connect('127.0.0.1', 6379, 5); // timeout is 5 seconds
The connect method returns true if the connection is successful, otherwise false.
Code explanation
$redis: An instance of the Redis class.connect: The method used to connect to a Redis server.127.0.0.1,6379: The IP address and port of the Redis server.5: The timeout parameter, in seconds.
Helpful links
More of Predis
- How can I troubleshoot a "PHP Redis went away" error?
- How can I use PHP and Redis to retrieve data from a sorted set using ZRANGEBYSCORE?
- How do I install PHP Redis on Ubuntu 20.04?
- How can I use the zscan command in PHP with Redis?
- How can I use PHP and Redis to retrieve a range of values from a sorted set?
- How can I install and configure PHP and Redis on a Windows system?
- How can I use PHP to increment values in Redis using ZINCRBY?
- How do I use the PHP Redis zrevrange command?
- How do I install PHP, Redis, and XAMPP?
- How can I use PHP and Redis to set a time-to-live (TTL) value?
See more codes...