Class: ActiveSet

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

Constant Summary collapse

VERSION =
"0.1.0"
SECONDS_PER_DAY =
86400

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ActiveSet

Initializes a new Set of active objects.

name - String name to identify this ActiveSet. options - Hash of options.

:prefix - String prefix used to namespace the Redis key.
:days   - Default Fixnum of days of items to allow in the
          set.  Default to 30.
:sec    - Fixnum of seconds of items to allow in the set.
          Defaults to using MATH to calculate seconds in
          30 days.
:redis  - An existing Redis connection.  If not set, the
          rest of this options Hash is used to initialize
          a new Redis connection.


22
23
24
25
26
27
28
29
30
# File 'lib/active_set.rb', line 22

def initialize(name, options = {})
  @name = name
  @prefix  = options.delete(:prefix) || :active
  @days    = options.delete(:days)   || 30
  @sec     = options.delete(:sec)    || (SECONDS_PER_DAY * @days)
  @redis   = options.delete(:redis)
  @key     = "#{@prefix}:#{@name}"
  @redis ||= Redis.new(options)
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/active_set.rb', line 7

def key
  @key
end

Instance Method Details

#add(entry, time = Time.now) ⇒ Object

Public: Adds a new object to the Set.

entry - The String identifier of the object. time - Optional Time specifying when the object was last active.

Returns nothing.



38
39
40
# File 'lib/active_set.rb', line 38

def add(entry, time = Time.now)
  @redis.zadd(@key, time.to_i, entry)
end

#clearObject

Public: Clears the Set.

Returns nothing.



87
88
89
# File 'lib/active_set.rb', line 87

def clear
  @redis.del(@key)
end

#count(since = nil) ⇒ Object

Public: Counts the number of objects in the set.

since - An optional Time to specify the cutoff time to

count.  If provided, any object updated since the timestamp
is counted.

Returns a Fixnum.



68
69
70
71
72
# File 'lib/active_set.rb', line 68

def count(since = nil)
  (since ?
    @redis.zcount(@key, since.to_i, "+inf") :
    @redis.zcard(@key)).to_i
end

#earliest_timeObject

Calculates the earliest time used as a cutoff point for #trim.

Returns Fixnum seconds.



94
95
96
# File 'lib/active_set.rb', line 94

def earliest_time
  Time.now.to_i - @sec
end

#include?(entry) ⇒ Boolean

Public: Checks to see if the object is in the Set.

entry - The String identifier of the object.

Returns true if the object is in the set, or false.

Returns:

  • (Boolean)


47
48
49
# File 'lib/active_set.rb', line 47

def include?(entry)
  !@redis.zscore(@key, entry).nil?
end

#timestamp_for(entry) ⇒ Object

Public: Gets the timestamp for when the given object was active.

entry - The String identifier of the object.

Returns the Time the object was last active, or nil.



56
57
58
59
# File 'lib/active_set.rb', line 56

def timestamp_for(entry)
  sec = @redis.zscore(@key, entry)
  sec ? Time.at(sec.to_i) : nil
end

#trim(time = earliest_time) ⇒ Object

Public: Trims the Set.

time - Optional Time specifying the earliest cutoff point. Any

object with a later timestamp is purged.

Returns nothing.



80
81
82
# File 'lib/active_set.rb', line 80

def trim(time = earliest_time)
  @redis.zremrangebyscore(@key, "-inf", "(#{time.to_i}")
end