Sha256: a316e03a6b95ad2339479110a8030282ada043c13a07b7a394c4d8222aac387c

Contents?: true

Size: 1.05 KB

Versions: 1

Compression:

Stored size: 1.05 KB

Contents

require 'redis_dedupe/version'

module RedisDedupe
  class<<self
    attr_accessor :client
  end

  class Set
    SEVEN_DAYS = 7 * 24 * 60 * 60

    attr_reader :key, :expires_in

    def initialize(redis, key, expires_in = SEVEN_DAYS)
      @redis      = redis
      @key        = key
      @expires_in = expires_in
    end

    def check(member)
      results = redis.pipelined do |pipeline|
        pipeline.sadd(key, member)
        pipeline.expire(key, expires_in)
      end

      if results[0]
        yield
      end
    end

    def finish
      redis.unlink(key)
    end

    private

    def redis
      @redis
    end
  end

  module Helpers
    private

    def dedupe
      @dedupe ||= RedisDedupe::Set.new(RedisDedupe.client, [dedupe_namespace, dedupe_id].join(':'))
    end

    # Implement in class, should return an integer or string:
    #
    # Ex.
    #
    #   def dedupe_id
    #     @announcement.id # => 42
    #   end
    #
    def dedupe_id
      raise NotImplementedError
    end

    def dedupe_namespace
      self.class.name
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
redis_dedupe-0.0.5 lib/redis_dedupe.rb