Dragonfly

PHP Redis: Get All Keys Matching Pattern (Detailed Guide w/ Code Examples)

Use Case(s)

Finding keys in a Redis database matching a certain pattern is useful when you want to perform a mass operation on these keys, or need to filter keys based on specific criteria.

Code Examples

Let's assume we have a few keys such as user:1, user:2, user:3 and we're looking for all keys starting with 'user'.

The Predis client library can be used to connect PHP with Redis:

CODE_BLOCK_PLACEHOLDER_0
The keys method of the Predis client object is used to fetch all keys that match a given pattern. The '*' character is a wildcard that matches any sequence of characters.

Best Practices

Common Mistakes

FAQs

Was this content helpful?

Help us improve by giving us your feedback.

Similar Code Examples

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

System Design on AWS

Switch & save up to 80% 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement. Instantly experience up to a 25X boost in performance and 80% reduction in cost