Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Track Hits

Tags:

redis

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?

like image 480
Mark Avatar asked Dec 28 '22 06:12

Mark


2 Answers

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.

like image 78
Tom Clarkson Avatar answered Jan 09 '23 02:01

Tom Clarkson


Another option is to use redis >= 2.1.3. From that version on, expire works the way you want. I'm using 2.1.5 (from git) to do this same thing. I use both expire and a method similar to what Tom describes. I place the date in the key, and I set an expiration. With redis >= 2.1.3 you can set expire on an incremented key without it resetting the counter.

The reason for both is a) I store more than one day and b) if the expired keys are still there for whatever reason I'm not querying for them when getting stats for today. For example the hash: SERVER with key YEAR:MONTH:DAY:URL is incremented and an expiration set for today+3 days. Works like a charm.

like image 21
The Real Bill Avatar answered Jan 09 '23 01:01

The Real Bill