Joe
Joe

Reputation: 13101

How to add condition (filter) in Redis

I have a CSV like this:

key,fname,lname,email,authorized
1,mike,gote,[email protected],1
2,joe,doe,[email protected],0
3,mark,ding,[email protected],1

I can store in redis based on Keys (1,2,3) but need to get only users that are authorized (authorized==1).

How to achieve that?

Thanks

Upvotes: 0

Views: 3612

Answers (1)

Supermacy
Supermacy

Reputation: 1489

You can fit the csv in this data structure. You can create hashmap with key like
user:{email}
for Example:
You can make the hash keys like-
user:[email protected] with keys like fname mike, lname gote, email [email protected], authorised 1
You can use REDIS COMMAND for every row in the CSV

hmset user:[email protected] fname mike lname gote email [email protected] authorised 1

Now you can make Set with the key user:authorisation:0 and user:authorisation:1 and insert the values in the set using command
sadd user:authorisation:1 user:[email protected]
You have to insert all the hash keys of user:{email} for respective authorisation set.

Now for getting the all the users which have same authorisation level you have to first fetch all the memebers of the set and then take out the value from that pointing of the hash. For example getting the value of all authorised email from the list - you have to run the query

smembers user:authorisation:1

This will return the hash of the user. For getting the information out of the hash we have to run the command.

hgetall user:[email protected]

This is the standard approach while dealing with this type of dataset in redis.

Upvotes: 3

Related Questions