Key-value list in Redis - redis

How can I store collection with key-value pairs in Redis? For example, I want to log time when user tried to login, to some collection. Every user have id, so I want to use it as a key. But I want to store it separatly from other elements, in separate collection

For each user you can have a sorted set. You can use the user id in the name of the sorted set. Just use 1 as the value since you don't need to store something there and use the timestamp as the score.
zadd 'user:' + uid +':logins' currentTimestamp 1
With this you can run queries to grab how many times a user tried to login during certain periods with zcount etc.

Related

How to add condition (filter) in Redis

I have a CSV like this:
key,fname,lname,email,authorized
1,mike,gote,mike#gmail.com,1
2,joe,doe,joe#gmail.com,0
3,mark,ding,mark#gmail.com,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
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:mike#gmail.com with keys like fname mike, lname gote, email mike#gmail.com, authorised 1
You can use REDIS COMMAND for every row in the CSV
hmset user:mike#gmail.com fname mike lname gote email mike#gmail.com 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:mike#gmail.com
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:mike#gmail.com
This is the standard approach while dealing with this type of dataset in redis.

Redis - Sort ID's based on Names in another Set

I am using multiple redis sets, hashes and Sorted sets for my use-case.
Suppose I am having a HASH set which stores ID and its corresponding object. (Project ID and its Content)
I have sets which contain list of ID's (List of ProjectIDs)
I have sorted sets which will sortBy DateTime fields and other Integer scores.
(Sort by DeadLine, Created etc and also by Project Name).
I also created a Sorted Set as my use-case needs Sort By Name (say Project Name). I created Project Name Sorted Set (ProjectName:ID as value and 0 as score).
So my requirement is that I need to sort my set (which contain ID's) based on Project Name in DESC or ASC.
How to achieve this??
Read the documentation about SORT - it should be something like SORT nameofzset BY nosort GET nameofhash->*. Even better, learn how to write Lua scripts and execute them with EVAL.

How combine data from Redis?

I use sorted sets type in Redis. Each user adds an event to Redis with a score - current timestamp and key: User:ID_USER
How I can get 10 latest events from redis store sorted by score(timestamp)?
For this there is a function ZREVRANGEBYSCORE, but if use it, I get a problem, because not every user have events at last time score.
Edited:
For example, I want to merge some sorted keys by score(timestamp) and selecting the latest data for last 4 hours for each users.
Problem is, that if user did not post data in Redis in this interval(4 hours) then does not show nothing for this user. But I need select latest data sorted by timestamp from each user. Maybe use any function?

Redis: How to give an unique id to hashes

I want to save my users information in hashes. I want to create a hash for each user in the application.
The hashes name is like this: "user:1001"
Now, i want to users id start from 1000 and increase by one.
how can i do this?
thanks
You can have a key called user:id, which will be a number that you will increment to obtain new ids. In your case, you can set the initial value to 1000:
SET user:id 1000
Then by using INCR you will be able to get a new id for your next user:
INCR user:id
Depending on the language you use, there may be already some tools to solve this problem. I would recommend you check Ohm, or one of the ports.

Strategy to synchronize 2 sortedsets in redis?

I have 2 sorted sets:
mainSet: which contains a list of <score, id>
userSet: which contains some of items filtered from mainSet based on certain ruless. (Each user in the system will have an userSet with userId as key)
If there are some changes in the mainSet and very likely item order will be re-sorted, (like score updated, new items added), then I want to update the userSet accordingly.
For example,
mainSet<key, 1, id1>
mainSet<key, 2, id2>
mainSet<key, 3, id3>
mainSet<key, 4, id4>
userSet<userKey, 2, id2>
userSet<userKey, 3, id3>
Items in the mainSet will be added/updated in the userSet based on some filtered rules
Currently, to make sure items in the userSet up to date with the mainSet (for example, the score of items changed), I have to traverse all items in the mainSet, check filtered rules, and re-add in the userSet but it takes around 250 ms for 200 items in the mainSet. (The mainSet may have max of 1000 items).
I want to know if there are any better approaches for my case?
You should look into Redis Keyspace Notifications? Keyspace notifications is a feature available since 2.8.0. If you add a listener to your main set, and for each received event validate whether the object should be moved in or out of the subset, then the system should handle the changes automatically.