Sha256: 13c36615e911df17045f3e04d30fe756702f74a825cfc9c9de9cf868f8c41d09

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

Contents

# frozen_string_literal: true

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

    def persist_list(list_name, list_values)
      if list_values.length > 0
        redis.multi do |multi|
          tmp_list = "#{list_name}_tmp"
          tmp_list += redis_namespace_used? ? "{#{Split.redis.namespace}:#{list_name}}" : "{#{list_name}}"
          multi.rpush(tmp_list, list_values)
          multi.rename(tmp_list, list_name)
        end
      end

      list_values
    end

    def add_to_set(set_name, value)
      return redis.sadd?(set_name, value) if redis.respond_to?(:sadd?)

      redis.sadd(set_name, value)
    end

    private
      attr_accessor :redis

      def redis_namespace_used?
        Redis.const_defined?("Namespace") && Split.redis.is_a?(Redis::Namespace)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
split-4.0.4 lib/split/redis_interface.rb
split-4.0.3 lib/split/redis_interface.rb