KensoDev/visit-counter — pull request #3 by gzohari (2012-10-08 08:59:20+00:00)
Title: Feature - override threshold and update counters in time-range

This feature will allow the application to pull all hits from the Redis, preventing a sticky situation where an object never makes it's threshold and never gets persisted in the db (notwithstanding #read_counter which shows the count on Redis)

Possible use cases for this is:
- sweeping through all counters hourly to keep main db in sync with real numbers
- sweep daily to prevent data-loss (say Redis crashes and loses data)
- gem has a bug, user wants to stop using it and retrieve data until the bug's fixed.

Implementation:
Before counter is created/incremented, it's added to a Redis sorted set with current timestamp set as the score. This will allow for an efficient redis lookup for keys within time-periods (without key duplications).

The method #update_counters finds all relevant counters, filters counters of objects that have been deleted, and updates them in a single transaction with recently implemented locks.

Problems with implementation:
#update_counters is using multiple #update_all, to prevent validation _and_ callbacks. didn't use update_attribute because it invokes callbacks and is apparently being deprecated. Didn't use #update_column for backwards compatibility.
Didn't use Redis "transactions" (watch/multi) because the existing lock mechanism which does the job.