class HitList::Counter
Constants
- DATE_PARTIAL_FORMAT
- DEFAULT_DAYS_OF_INTEREST
- DEFAULT_INCR_VALUE
- DEFAULT_NAMESPACE
- SECONDS_IN_DAY
Attributes
connection[R]
days_of_interest[R]
name[R]
namespace[R]
Public Class Methods
new(connection, name, days_of_interest = DEFAULT_DAYS_OF_INTEREST)
click to toggle source
Creates new instance of counter. It requires a working Redis connection as first argument.
Examples¶ ↑
counter = HitList::Counter.new(connection, 'articles', 7) counter = HitList::Counter.new(connection, 'articles')
# File lib/hit_list/counter.rb, line 15 def initialize(connection, name, days_of_interest = DEFAULT_DAYS_OF_INTEREST) @connection, @name, @days_of_interest = connection, name, days_of_interest end
Public Instance Methods
hit!(id)
click to toggle source
Increments total hits and rank for given id
# File lib/hit_list/counter.rb, line 49 def hit!(id) increment_total_hits!(id) increment_rank!(id) end
increment_rank!(id)
click to toggle source
Increments rank only for given id
# File lib/hit_list/counter.rb, line 60 def increment_rank!(id) @connection.pipelined do date_partials.each do |date_partial| connection.zincrby("#{namespace}:#{name}:date:#{date_partial}", DEFAULT_INCR_VALUE, id) end end end
increment_total_hits!(id)
click to toggle source
Increments total hits only for given id
# File lib/hit_list/counter.rb, line 55 def increment_total_hits!(id) connection.incr("#{namespace}:#{name}:total:#{id}") end
top_records(limit, time_of_interest = nil)
click to toggle source
Returns array of ids sorted by most hits first
Examples¶ ↑
counter.top_records(3) # => ["31", "1", "44"] counter.top(1, Time.now - 4.days) # => ["5"]
# File lib/hit_list/counter.rb, line 42 def top_records(limit, time_of_interest = nil) time = time_of_interest || Time.now date = time.strftime(DATE_PARTIAL_FORMAT) connection.zrevrange("#{namespace}:#{name}:date:#{date}", 0, limit - 1) end
Private Instance Methods
date_partials()
click to toggle source
# File lib/hit_list/counter.rb, line 70 def date_partials partials = [] 0.upto(days_of_interest - 1) do |index| partials << (Time.now + (index * SECONDS_IN_DAY)).strftime(DATE_PARTIAL_FORMAT) end partials.to_enum end