Class: UniqueContentSet
- Inherits:
-
Object
- Object
- UniqueContentSet
- Defined in:
- lib/unique_content_set.rb
Overview
Stores a set of unique content. This is used to check if new content being added has been seen already.
Constant Summary collapse
- VERSION =
'0.1.0'
Class Attribute Summary collapse
-
.redis ⇒ Object
Sets are stored in Redis.
-
.redis_prefix ⇒ Object
Sets are stored in Redis.
Instance Attribute Summary collapse
-
#key ⇒ Object
readonly
Returns the value of attribute key.
Instance Method Summary collapse
-
#add(content, time = Time.now) ⇒ Object
Public: Adds the given content to the current set, scored by the given time.
-
#delete_before(time) ⇒ Object
Public: Removes content posted before the given time.
-
#exist?(content) ⇒ Boolean
Public: Looks for the given content in the current set.
-
#initialize(*args) ⇒ UniqueContentSet
constructor
A new instance of UniqueContentSet.
Constructor Details
#initialize(*args) ⇒ UniqueContentSet
Returns a new instance of UniqueContentSet.
20 21 22 23 24 25 26 27 28 |
# File 'lib/unique_content_set.rb', line 20 def initialize(*args) @redis = self.class.redis # Turn the given args into a redis key, with pieces separated by ':'. args.unshift self.class.redis_prefix args.compact! args.map! { |a| a.to_s } @key = args * ":" end |
Class Attribute Details
.redis ⇒ Object
Sets are stored in Redis.
10 11 12 |
# File 'lib/unique_content_set.rb', line 10 def redis @redis end |
.redis_prefix ⇒ Object
Sets are stored in Redis.
10 11 12 |
# File 'lib/unique_content_set.rb', line 10 def redis_prefix @redis_prefix end |
Instance Attribute Details
#key ⇒ Object (readonly)
Returns the value of attribute key.
18 19 20 |
# File 'lib/unique_content_set.rb', line 18 def key @key end |
Instance Method Details
#add(content, time = Time.now) ⇒ Object
Public: Adds the given content to the current set, scored by the given time.
content - String content to add to the set. time - The current Time the content was created.
Returns true if this is the first occurence of the content, or false.
37 38 39 |
# File 'lib/unique_content_set.rb', line 37 def add(content, time = Time.now) @redis.zadd(@key, time.to_i, member_from(content)) end |
#delete_before(time) ⇒ Object
Public: Removes content posted before the given time.
time - The latest Time that should be purged from the set.
Returns a Fixnum of the number of removed entries.
55 56 57 |
# File 'lib/unique_content_set.rb', line 55 def delete_before(time) @redis.zremrangebyscore(@key, 0, time.to_i+1) end |
#exist?(content) ⇒ Boolean
Public: Looks for the given content in the current set.
content - String content that is being checked.
Returns true if the content is a member of the set, or false.
46 47 48 |
# File 'lib/unique_content_set.rb', line 46 def exist?(content) !!@redis.zscore(@key, member_from(content)) end |