tags:

views:

31

answers:

1
+1  Q: 

Redis Track Hits

Hi,

I have a simple daily hit counter on my site that I want to use Redis as the datastore for.

Simply because redis has an expire, I dont have to set up a cron to clear the data down. Plus I want to try it out.

I store daily hits on a URL basis.

How can I store the daily hits for a url then have them expire at the end of the day.

for example:


incr today:www.google.com           >> 1
incr today:www.google.com           >> 2
incr today:www.google.com           >> 3
incr today:www.yahoo.com            >> 1
incr today:www.yahoo.com            >> 2
How do I have these counters expire at the end of the day.
If I do an expire, it resets the counters.

I feel like my thought process is off. Am i doing things backwards?

+1  A: 

You need to be using the current date as key rather than "today".

Set up a hash for the current date, with each url being a key within that hash. Your update would then be

HINCRBY 101021 www.google.com 1

and you can use the DEL command to remove the entire hash for a day once you no longer want to keep the data - maybe set up a manually triggered script that calls DEL for everything between 1 and 2 months old.

Setting expiry on the hash would probably also work though I haven't tried it - using a different key for each day means you aren't relying on expiry happening at a precise time like you would be with a "today" key.

Tom Clarkson
I think you are correct about the current date. That means I can store all the data for each day, I can also do it for month and year.
Mark
Also, the EXPIRE command is not as great as I first thought, I could either just delete keys when they become obsolete as you suggest.
Mark