<commit_before>require 'redis'

class RedisRateLimiter
  attr_accessor :key, :redis, :limit, :interval

  # options:
  # :interval - time span to track in seconds
  # :limit    - max count allowed in interval
  def initialize key, redis, options = {}
    @key      = key
    @redis    = redis
    @limit    = options[:limit] || 50
    @interval = options[:interval] || 60
  end

  def add subject, time = Time.now.to_i
    subject = "#{@key}:#{subject}"
    @redis.multi do
      @redis.lpush(subject, time)
      @redis.ltrim(subject, 0, @limit - 1)
      @redis.expire(subject, @interval)
    end
  end

  def exceeded? subject
    subject = "#{@key}:#{subject}"
    return false if @redis.llen(subject) < @limit
    last = @redis.lrange(subject, @limit - 1, @limit - 1).last
    Time.now.to_i - last.to_i < @interval
  end
end<commit_msg>use negative index<commit_after>require 'redis'

class RedisRateLimiter
  attr_accessor :key, :redis, :limit, :interval

  # options:
  # :interval - time span to track in seconds
  # :limit    - max count allowed in interval
  def initialize key, redis, options = {}
    @key      = key
    @redis    = redis
    @limit    = options[:limit] || 50
    @interval = options[:interval] || 60
  end

  def add subject, time = Time.now.to_i
    subject = "#{@key}:#{subject}"
    @redis.multi do
      @redis.lpush(subject, time)
      @redis.ltrim(subject, 0, @limit - 1)
      @redis.expire(subject, @interval)
    end
  end

  def exceeded? subject
    subject = "#{@key}:#{subject}"
    return false if @redis.llen(subject) < @limit
    last = @redis.lrange(subject, -1, -1).last
    Time.now.to_i - last.to_i < @interval
  end
end
{'fraction_non_alphanumeric': 0.13644524236983843, 'fraction_numerical': 0.009575104727707959, 'mean_word_length': 2.8260869565217392, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2327973', 'n_tokens_mistral': 601, 'n_tokens_neox': 582, 'n_words': 157}