I am currently looking into using redis to manage a black and whitelist for asterisks to manage spam calls. Since redis is a key value store it is great to check if a phone number is in the db. I can store some additional info in my value part of store and use the phone number as a key. 2 question I have is A) a phone number could be multiple times in the key as it would be for different users. So since the key has to be unique in db I assume best way would to use a key like user:phone number. Is there a way I could get all records for a given user: in key or can I only get data if I have full key
B) when I store phone number as key, can I use something like user:+2135551212 or will the + in key cause problems ?
-
Is getting all the records for a given user is the only query you need?Guy Korland– Guy Korland2020年12月28日 06:30:03 +00:00Commented Dec 28, 2020 at 6:30
-
In my case I will store a blacklist of nbrs to block. The block can be for either the system or for individual users or both. So a phone nbr can be in the the db more then once. In a traditional db I would have a either a join or multiple records which hold like the id of owner . But since redis is key based I am not sure how to make this work to not lose the benefit of speed. If I store it in hash I can’t query the owner.NoSoup4you– NoSoup4you2020年12月28日 17:54:28 +00:00Commented Dec 28, 2020 at 17:54
-
You can use Redis SET to store unique phone numbers if that works for you. If you need a mapping of a phone number to user then try to use the Hash map.sonus21– sonus212020年12月29日 14:17:43 +00:00Commented Dec 29, 2020 at 14:17
1 Answer 1
To answer your 1st question: No, you don't need full key to fetch all records for a given user. Redis provides wildcard matches which you can use in your case. As example:
127.0.0.1:6379> set john:1234567890 johnOne
OK
127.0.0.1:6379> set john:0987654321 johnTwo
OK
127.0.0.1:6379> keys john*
1) "john:1234567890"
2) "john:0987654321"
127.0.0.1:6379>
I have set two keys both with user 'john' followed by different phone numbers, so when I need all the keys which starts with john, we can use 'keys john*' to get all the keys which starts with 'john'.
To answer your second part, yes you can use '+' in the keys without any problem. Again, as an example:
127.0.0.1:6379> set user:+1234567890 helloagain
OK