Redis Sets - date added - redis

I currently have a Redis Set and I'm not sure if this is possible but thought I'd ask just in case someone knows of a way around it. Is it possible to add a date of when a new element for a key is created when using 'SADD' command?
I've tried searching the web but I can't find anything, which tells me its not possible. Is there another way of doing this? Something like the below and then being able to get the date the element was created?
SADD my:key:string "1000", Time.now

Use a Sorted Set instead of a Set. Using the timestamp as a score will allow you to retrieve your values ordered by date, values for a given date, values after of before a given date, etc. Or of course just the date for the value.
ZADD myzset 12345678 "my value"

In order to set the date of an element save the element in form of json :
SADD mset "{value:1000,created:18-03-2014}"
So when you retrieve the element its creation date will be also be retrieved.

Related

How to change data in sorted set?

I use sorted set data type in Redis.
I add data with command zadd. Adding data is JSON format.
How I can change value in this sorted set by score?
I need get JSON value and change one field and after update this sorted set.
I tried to add againg data with the same score, but I get dublicates
It's simple!
ZREM key data;
ZADD key score newdata;
You simply cannot UPDATE an element in SET structure. It's not possible by definition! Like I cannot EAT a cup of water, I can only DRINK it ^_^
Reply if you have any further problems.
By the way, I don't know your application need, but I have a strong feeling that SORTED SET is unsuitable for your application senario.
One does not update a set's (sorted or not) members. You'll have to remove the old member and add the new (updated JSON) in its place with the relevant score. You could wrap this in Lua or a WATCH/MULTI/EXEC block for atomicity.
You could remove first by score if you're using the list as a key (score) => value store.
ZREMRANGEBYSCORE key score score
ZADD key score data

Key-value list in 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.

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.

Data structure for attributes changing based on datetime?

I need to write a data structure that can return a different Price for an Item, based on the day of the week and the hour of the day. Is there a clever way to store this? I can't think of where to start for something like this, other than maybe using a dictionary and using some kind of structured key format to indicating pricing for different time periods.
Item object has a number of Price objects.
When Item is asked for price it loops through it's Price object's and asks each one if they are currently valid to give a price value back.
Each Price object checks against current day/time to see if his value is valid.
Hopefully you will only get one value back but if there are multiple or no matches you need some logic to go from there.