Class: RedisBackend

Inherits:
Object
  • Object
show all
Defined in:
lib/counter_server.rb

Instance Method Summary collapse

Constructor Details

#initialize(redis_client) ⇒ RedisBackend

Returns a new instance of RedisBackend.



90
91
92
# File 'lib/counter_server.rb', line 90

def initialize(redis_client)
  @redis_client = redis_client
end

Instance Method Details

#flushObject



106
107
108
109
110
# File 'lib/counter_server.rb', line 106

def flush
  @redis_client.pipelined do
    yield self
  end
end

#increment_by(group_name, key, increment_by) ⇒ Object



112
113
114
115
116
117
# File 'lib/counter_server.rb', line 112

def increment_by(group_name, key, increment_by)
  @redis_client.zincrby(group_name, increment_by, key)

  # Expire all keys in one month.
  @redis_client.expire(group_name, 60 * 60 * 24 * 30)
end

#retrieve_counts(group_name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/counter_server.rb', line 94

def retrieve_counts(group_name)
  ret = ActiveSupport::OrderedHash.new

  # The return value of zrangebyscore looks like this:
  # ["key 1", "1", "key 2", "4", "key 3", "100"]
  counts_array = @redis_client.zrangebyscore(group_name, '-inf', '+inf', :with_scores => true)
  counts_array.reverse.each_slice(2) do |pageviews, key|
    ret[key] = pageviews.to_i
  end
  ret
end