Class: RedisExpiringCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/redis-expiring_counter/base.rb,
lib/redis-expiring_counter/version.rb

Overview

Persistance:

The counters will live on across sessions unless explicitly deleted.
WARNING: This class will wipe one Redis database per counter

Note:

A note on speed: A key search based method is about ten times slower than using dbsize.

Redis configuration:
:host       remote ip, default is 127.0.0.1
:port       default is 6379
:password   if you require a password on connection
:db         default is 0
:timeoeut   default is 5 seconds
:logger     to log activity as database it works

Defined Under Namespace

Classes: Configuration

Constant Summary collapse

VERSION =

NON_FUNCTIONAL 0.0.0.x level changes for changes to documentation and formatting or functionally equivalent sturctural changes PATCH 0.0.x level changes for implementation level detail changes, such as small bug fixes MINOR 0.x.0 level changes for any backwards compatible API changes, such as new functionality/features MAJOR x.0.0 level changes for backwards incompatible

"1.0.0.0"
@@id_key_base =
"expiring:counter:id"
@@db_0 =
Redis.new({:db => 0, :host => Configuration::HOST, :port => Configuration::DB_PORT})
@@db_range =
(Configuration::FIRST_COUTER_DB_ID..Configuration::LAST_COUNTER_DB_ID)
@@id_key_list =
(0..Configuration::LAST_COUNTER_DB_ID).each.map{|i| "expiring:counter:#{i}"}
@@max_count_exceeded =
<<-MARK_TEXT
  Exceeded maximum number of expiring counters (#{Configuration::MAX_COUNTER_COUNT}) in #{self.class.to_s}.
  Don't forget to call delete when a counter is no longer in use.
MARK_TEXT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, expiration_in_seconds, max_count) ⇒ RedisExpiringCounter

Returns a new instance of RedisExpiringCounter.



38
39
40
41
42
43
44
45
46
47
# File 'lib/redis-expiring_counter/base.rb', line 38

def initialize(name, expiration_in_seconds, max_count)
  @name = name
  @existing_counter_search = "#{@@id_key_base}:#{name}:*"
  @expiration_in_seconds = expiration_in_seconds
  @max_count = max_count

  @id_key, @db_index = get_key_and_id(name)
  @@db_0.incr(@id_key)
  @db = Redis.new({:db => @db_index, :host => Configuration::HOST, :port => Configuration::DB_PORT})
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/redis-expiring_counter/base.rb', line 36

def name
  @name
end

Class Method Details

.delete(db_id) ⇒ Object



128
129
130
# File 'lib/redis-expiring_counter/base.rb', line 128

def delete(db_id)
  @@db_0.del(@@db_0.keys("#{@@id_key_base}:#{name}:*").first)
end

.delete_all_countersObject



131
132
133
134
135
136
137
# File 'lib/redis-expiring_counter/base.rb', line 131

def delete_all_counters
  @@db_range.each do |i|
    if @@db_0.exists(@@id_key_list[i])
      @@db_0.del(@@id_key_list[i])
    end
  end
end

.exists(name) ⇒ Object



125
126
127
# File 'lib/redis-expiring_counter/base.rb', line 125

def exists(name)
  (@@db_0.keys("#{@@id_key_base}:#{name}:*").count > 0)
end

Instance Method Details

#countObject



59
60
61
# File 'lib/redis-expiring_counter/base.rb', line 59

def count
  @db.dbsize
end

#deleteObject



63
64
65
66
# File 'lib/redis-expiring_counter/base.rb', line 63

def delete
  @@db_0.del(@id_key)
  @db.flushdb
end

#dumpObject



68
69
70
71
72
73
74
75
76
# File 'lib/redis-expiring_counter/base.rb', line 68

def dump
  puts "database: '#{@id_key}'"
  keys = @db.keys
  if keys.size > 0
    values = @db.mget(*keys)
    list = Hash[keys.zip(values)]
    list.each{|k, v| puts "%s = %s" % [k, v]}
  end
end

#incrementObject



49
50
51
52
53
54
55
56
57
# File 'lib/redis-expiring_counter/base.rb', line 49

def increment
  if @db.dbsize < @max_count
    key = @@db_0.incr(@id_key)
    @db.setex(key, @expiration_in_seconds, '1')
    true
  else
    false
  end
end