Bitmap Counter

An efficient id-based counter. Current implementation uses redis.

Usage

counter = Bitmap::Counter.new("name")

counter.add(id = 1)   # a numeric user_id for example
counter.count         # 2
counter.includes?(1)  # true
counter.includes?(2)  # false

DateCounter

Count ids for a series of dates.

counter = Bitmap::DateCounter.new("name")
counter.add(1, Date.today)
counter.add(2, Date.today)
counter.add(1, Date.today - 1)

You can inspect a specific date:

counter.count(Date.today)     # 2
counter.count(Date.today - 1) # 1

counter.includes?(1, Date.today)      # true
counter.includes?(2, Date.today)      # true
counter.includes?(1, Date.today - 1)  # true
counter.includes?(2, Date.today - 1)  # false

Or a range of dates:

counter.count((Date.today - 1)..Date.today) # 2

counter.includes?(1, (Date.today - 1)..Date.today) # true
counter.includes?(2, (Date.today - 1)..Date.today) # true
counter.includes?(3, (Date.today - 1)..Date.today) # false

Credits

Inspired by the awesome post by Chandra Patni.

Ruby Version

This was developed on ruby 1.9.2p180 and not tested on other versions...yet :)

TODO

  • Add different backends (File, Postgres, etc)