Daily, Weekly and Monthly Page View Counter
- by Jens Fahnenbruck
I'm building a website with user generated content. On the home page I want to show a list of all created items, and I want to be able to sort them by a view counter. That's sound easy, but I want multiple counters. I want to know which was the most visited item in the last day, last week or last months or overall.
My first Idea was to create 4 counter columns in the item's DB-Table. One for each of daily, weekly, monthly and overall, and the create a cron job, that clears the daily counter every 24 hours, the weekly counter every 7 days and so on.
But my problem with this is, what happens if I want to know which was the most viewed item of the week, just after the weekly counter got cleared?
What I need is an efficient way to create a continous counter, which got reduced for every page view that is too old, and increased for every new page view.
Right now I'm thinking of a solution with the redis server, but I have no solution yet.
I'm just looking for a general idea here, but FYI I'm developing this application in Ruby on Rails.