Redis XINFO Command in PHP (Detailed Guide w/ Code Examples)

Use Case(s)

The XINFO command in Redis is commonly used in scenarios where details about a stream are needed. It gives useful information such as the number of items in the stream, number of consumers, last generated ID and more.

Code Examples

To use the XINFO command in PHP with Redis, you will need to have the Redis extension installed. Below is an example:

$redis = new Redis(); $redis->connect('', 6379); // Create a sample stream $redis->xAdd('mystream', '*', ['key' => 'value']); // Get information about the stream $info = $redis->rawCommand('XINFO', 'STREAM', 'mystream'); print_r($info);

In this example, we first create a connection to the Redis server. Then, we add a key-value pair to a stream named 'mystream'. We then use the rawCommand method to send the XINFO STREAM mystream command to Redis, which returns information about the 'mystream'. We print this data using print_r.

Best Practices

When using the XINFO command in PHP, remember:

  1. Always ensure that the stream exists before running XINFO. If the stream does not exist, Redis will return an error.
  2. Be mindful when running XINFO on large streams, as it may be resource-intensive.

Common Mistakes

One common mistake is assuming XINFO behaves like other commands in the PHP Redis extension, where you can pass arguments separately instead of all being passed in one array (like rawCommand). The correct usage of rawCommand is to pass all the arguments in an array after the command.


Q: Does XINFO work with the PHP Redis extension's methods like xRead, xAdd, etc.?

A: No, XINFO is not a method in the PHP Redis extension, you have to use the rawCommand method and pass 'XINFO' as an argument to this method.

Q: What information can I get from XINFO STREAM <stream>?

A: You can get various details such as the number of items in the stream, the last generated ID, the number of consumers, groups, and more.

Was this content helpful?

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.