How to get all keys in Redis

In Redis, we use KEYS to retrieve specific keys we’ve defined in our database. For example, if we’d created a Redis key like this:

set someKeyName someValue

Then we could get that value by using KEYS like this:

KEYS someKeyName

Intere…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson

In Redis, we use KEYS to retrieve specific keys we've defined in our database. For example, if we'd created a Redis key like this:

set someKeyName someValue

Then we could get that value by using KEYS like this:

KEYS someKeyName

Interestingly, Redis KEYS support glob style patterns - so we can match many keys using text familiar to you if you've ever worked with regex. Here are some examples:

  • someK?y - the question mark represents any one character - so someKqy, someKay, someKyy, etc, are all valid.
  • someK*y - the asterisk represents any character including spaces - so someKy is valid
  • someK[a-d]y - to match someKay, someKby, someKcy or someKdy.
  • someK[ea]y, - to match someKey or someKay
  • someK[^10]y - where [^10] means any number before 10 not including 10.
  • someK[^e]y - where [^e] means any letter before e, but not including e.

This glob style pattern support also lets us retrieve all keys at once. Since the asterisk () or wild card symbol is supported, **to retrieve all keys in redis at once*, you only have to run KEYS *:

KEYS *

If you have a lot of keys, this may not be what you want to do - but for certain use cases, it can be quite useful to retrieve all keys in Redis at once.


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Johnny Simpson


Print Share Comment Cite Upload Translate Updates
APA

Johnny Simpson | Sciencx (2022-10-01T16:56:00+00:00) How to get all keys in Redis. Retrieved from https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/

MLA
" » How to get all keys in Redis." Johnny Simpson | Sciencx - Saturday October 1, 2022, https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/
HARVARD
Johnny Simpson | Sciencx Saturday October 1, 2022 » How to get all keys in Redis., viewed ,<https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/>
VANCOUVER
Johnny Simpson | Sciencx - » How to get all keys in Redis. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/
CHICAGO
" » How to get all keys in Redis." Johnny Simpson | Sciencx - Accessed . https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/
IEEE
" » How to get all keys in Redis." Johnny Simpson | Sciencx [Online]. Available: https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/. [Accessed: ]
rf:citation
» How to get all keys in Redis | Johnny Simpson | Sciencx | https://www.scien.cx/2022/10/01/how-to-get-all-keys-in-redis/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.