tomekfranek
tomekfranek

Reputation: 7099

How to sum up Redis keys from entire week?

I have following redis keys:

REDIS.del "weekly:activity"
REDIS.del "2013-02-27:activity"
REDIS.del "2013-02-28:activity"

REDIS.sadd "2013-02-27:activity", 1
REDIS.sadd "2013-02-27:activity", 2
REDIS.sadd "2013-02-27:activity", 3
REDIS.sadd "2013-02-28:activity", 4
REDIS.sadd "2013-02-28:activity", 1
REDIS.sadd "2013-02-28:activity", 1
REDIS.sadd "2013-02-28:activity", 6

REDIS.sunionstore "weekly:activity", "2013-02-27:activity", "2013-02-28:activity"
REDIS.scard "weekly:activity"

How will be the best way to recognise first day in current week and sum stats from current week.

Can I do that using Redis? Or should I do it in Ruby?

Upvotes: 1

Views: 353

Answers (1)

Pascal Belloncle
Pascal Belloncle

Reputation: 11389

Instead of summing regularly, I suggest you instead collect more data and create keys for weeks and month.

So every time a user logs in, you:

REDIS.incr "{today}:activity"
REDIS.incr "{this week}:weekly:activity"
REDIS.incr "{this month}:monthly:activity"

This way, no reporting to do. I'll it up to you to compute this week and this month.

Upvotes: 1

Related Questions