How to count userhits with redis? - redis

mayby I am doin something completely useless to create my datasets at all. I incr keys to a user-tracking with
incr('userhit-by-day:20131118') //for the day
incr('userhit-by-day-and-userid:20131118:foobar123') //for the day and userid
How can I get a hit counter like top 10 users by from today, this week and a special date?

make use of "Sorted set" as per above comment.
here is documentation
Create a set for every day,
In that set, key should be UserId and its value should be count of hits.
Use ZINCRBY key increment member to update user's hit count.
Using ZREVRANGE key start stop [WITHSCORES] you can get list of all players who hit that day.
Using ZSCORE key member you can get particular users hit count.

Related

Using RedisTimeSeries downsampling to aggregate data

I want to use redis timeseries to store item count. i.e
Monitoring many companies and Company A has an account where they have added employees... When an employee adds an item I want to update their count of today, thisweek and thismonth and also increment the company A's account items today, this week and this month.
Is RedisTimeSeries the best technology and how can it be done to monitor multiple companies and their employees ?
If you don't need the history of items over time, One option is to just use counters (integers, Redis Strings) in either a multi exec block or a Lua script.
I tagged the company name (A) so that all keys reside on the same shard. 123 denotes the employee id
MULTI
SADD {A}:123:items newItem
INCR {A}:123:total
INCR {A}:123:2021
INCR {A}:123:2021:month:07
INCR {A}:123:2021:week:32
EXEC
Alternatively, you could keep all the counts per user in a single hash and use HINCRBY
Either solution might need some clean up for longer running periods.

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.

Scaling-window ratings in Redis

I use awesome Redis sorted sets to score users and, then, quickly get user rating by score. Also, my score has "weight", so that one score can give 5 points to user, and another vote can give 2 points, etc. Now if somebody votes for user, I call
ZINCRBY user:votes <vote_weight> <userId>
but now I need to calculate users ratings for the last week, month, year from the current timestamp (like 'moving window')
What is the best way to do it in Redis?
Your current approach would only work if you're interested in counting all votes from the beginning of time until this instant..
Lets focus on the problem of doing this for today - this could be easily addressed by adding a new sorted, e.g. votes:today, and doing the ZINCR on its elements.
What happens when today becomes tomorrow? Simple - either RENAME the key, e.g. to votes:yesterday, or just use the timestamp to begin with so you'll always be updating today's vote key, i.e. votes:<timestamp day value>
If you use the timestamp approach, after a week you'll end up with 7 keys - on for each day - with scores-per-user. Getting the results for the last week is a simple matter of getting these 7 keys' members and summing up their scores. You could even do it on the fly. The same goes for 1m0, 3mo, 6mo, 12mo and so forth... but.
But, if you want to do it for 12mo (~= 365 keys), you'll need more RAM (for storing these keys in Redis) and it will take longer to complete the aggregation, naturally. You can combat this by combining Redis' key expiry capabilities (e.g. set the TTL of a day's key to 12mo to keep only one year of history) and keeping running aggregates that are updated either on the fly (i.e. with every vote) or periodically (daily, weekly, monthly, etc...). Note that the same scripts can do housekeeping and delete/archive old data, potentially solving the need to expire it explicitly.

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?

incrementing a score in redis sorted sets

I want to track daily usage statistics using redis's sorted sets in which the score would be the time stamp and the member would be the value of that specific statistic (since I want to use zrange to fetch the statistics)
so if I have a visits key
zadd "visits", "20131211", 1 # which would add a single visit to the date of today dec 11, 2013
but what happens in the other visits?
How do I increment the score of a member?
I feel may be confusing the purpose of member and score
There is ZINCRBY.
Example from the docs:
ZINCRBY visits 1 "20131211"
Also, if the member doesn't exist yet, it get's added with the score you passed along. ZINCRBY in the Redis docs