Lets say I have a set of cities in the world like so:
EUKLOND
EUKMANC
EUKEDIN
EITROME
EITMILA
EITNAPE
EFRPARI
EFRAVIG
EFRBRES
Where the first letter is continent, next two are country and the trailing 4 are an abbreviated city name.
I would like to be able to search this set by passing in "E" which would return all the entries or EIT and retrieve all the entries for Italy or EFRPARI and get just the Paris entry.
Is this something I can do with Redis?
Due to the 'flat' nature of the Redis storage, the prefix is used as a 'namespace' for grouping the key/values inside a cache (and to avoid collision with other caches or keys inside Redis).
A NoSQL database, Redis doesn't use structured query language, otherwise known as SQL. Redis instead comes with its own set of commands for managing and accessing data.
The first command you can use to get the total number of keys in a Redis database is the DBSIZE command. This simple command should return the total number of keys in a selected database as an integer value. The above example command shows that there are 203 keys in the database at index 10.
Generally, it's an Auto-Complete scenario.
Salvatore Sanfilippo (@Antirez), Redis's author, wrote a thorough blog post about how to accomplish this.
UPDATE: I just saw another great blog post, that first takes Salvatore's solution and explains it in a clear way, and second offers another solution that is good also for multiple-word phrases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With