Sha256: d348a7ea00140f45718f1cda3606a871981b83ab44059c3fae56135c9a2b5d57

Contents?: true

Size: 1.14 KB

Versions: 5

Compression:

Stored size: 1.14 KB

Contents

module Split
  # Simplifies the interface to Redis.
  class RedisInterface
    def initialize
      self.redis = Split.redis
    end

    def persist_list(list_name, list_values)
      max_index = list_length(list_name) - 1
      list_values.each_with_index do |value, index|
        if index > max_index
          add_to_list(list_name, value)
        else
          set_list_index(list_name, index, value)
        end
      end
      make_list_length(list_name, list_values.length)
      list_values
    end

    def add_to_list(list_name, value)
      redis.rpush(list_name, value)
    end

    def set_list_index(list_name, index, value)
      redis.lset(list_name, index, value)
    end

    def list_length(list_name)
      redis.llen(list_name)
    end

    def remove_last_item_from_list(list_name)
      redis.rpop(list_name)
    end

    def make_list_length(list_name, new_length)
      while list_length(list_name) > new_length
        remove_last_item_from_list(list_name)
      end
    end

    def add_to_set(set_name, value)
      redis.sadd(set_name, value) unless redis.sismember(set_name, value)
    end

    private

    attr_accessor :redis
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
split-3.2.0 lib/split/redis_interface.rb
split-3.1.1 lib/split/redis_interface.rb
split-3.1.0 lib/split/redis_interface.rb
split-3.0.0 lib/split/redis_interface.rb
split-2.2.0 lib/split/redis_interface.rb