How do you get a list of streams?

Viewed 6103

I need to get a list of all streams (keys) in a database but I can't find a command for it.

I've already tried going over all keys and checking their typebut it is too slow/expensive.

I'd like to do something like XSCAN and get a list of keys like: ["stream1", "stream2"]

3 Answers

As of version 6.0 you can use the TYPE option to ask SCAN to only return objects that match a given type.

SCAN 0 TYPE stream

https://redis.io/commands/scan

There's no such command. Same as there's no way to get a list of other data structures, e.g. LIST, SET.

Instead, you can create an extra SET to record the keys of the streams you created. So that you can scan the SET to get the list of streams.

If you can have a prefix in the stream names ex: 'MyStream:1', 'MyStream:2' Then you can use regular scan command with patterns matching MyStream:*

EDIT: To address OPs concern to not have to use prefix and use SCAN command as is, adding from comments :

You can avoid using a prefix by using namespacing capability provided by redis. You can assign a 'database' (0-15 by default) for streams names. Say you use database 5 for streams, then scan command in database 5 should return the keys in it only. redis.io/commands/select

Related