I think a single redis DS won't suffice for your use case, considering you also need the unique users which is normally approximated using the pfcount command in redis.
I am assuming that you need the last 5 minutes at anytime, so that at 12:07 PM, you would need the data from 12:02 - 12:07 PM.
For each article, you could have the following pattern of keys.
- art:{<articleId>}:<dateinYYMMDD> eg art:{1009854}:161216 of type hash and will TTL set to 7 days(604800 seconds)
- art:{<articleId>}:unq eg. art:{1009854}:unq of type string and data added using PFADD.
- art:{<articleId>}:stats eg art:{1009854}:stats of type map, which could store the stats of the article like total count till now.
art:{1009854}:161216 can have keys of the form HHMM and values in type int, meaning the number of visits of that article during that minute. Additionally you can have another field in this key named todayTillNow which will be total article views till now for today.
art:{1009854}:unq can have the data added using PFADD and can be used for calculating the total unique users who have visited the article. Note that it is approximated, and is accurate upto 99%.
art:{1009854}:stats can be used for saving the stats of the article, like the total all time visits of the article. It is kept as a hash so that it is extensible, and can store other info/stats/metadata about the article.
Whenever a user with id user1 visits the article, we have to execute a lua script and pass it the articleId, dateinYYMMDD, timeinHHMM, and userid.
The Lua script will do the following.
1) HINCRBY art:{articleId}:dateinYYMMDD timeinHHMM 1
2) HINCRBY art:{articleId}:dateinYYMMDD todayTillNow 1
3) EXPIRE art:{articleId}:dateinYYMMDD 604800
4) PFADD art:{1009854}:unq user1
5) HINCRBY art:{1009854}:stats totalViews 1
Thereafter, all the data can be obtained from the above keys, ideally through a lua script which can be passed, the articleId, dateInYYMMDD, timeinHHMM.
1) today last 5 minute count => use HMGET on art:{articleId}:dateinYYMMDD to get the data for fields related to appropriate time for today, and add them up.
2) yesterday last 5 minute count => same as above, just pass yesterday
3) this day last week => same as above, just pass last week day
4) yesterday last week => same as above, just pass yesterday last week
5) last 5 minutes today => same as 1, just pass last 5 minutes.
6) totalViewCount => HGET art:{1009854}:stats totalViews
7) total unique view count(approximate)=> PFCOUNT art:{1009854}:unq
The reason that the article id is appended in {} is so that if you have a cluster, all your stats of an article will belong to a single node, and you can use lua script or PFCOUNT commands easily.
Also, setting expiry time of keys with date in them will make sure that for an article, a maximum of 8+1+1=10 keys are present for an article at a particular time.
Tuco