Sha256: 6f3f25ac00e135eaa7e6696a443123999b91aa854c57a8ddf86c1b46f4b51272

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

require 'concurrent'

module SplitIoClient
  module Cache
    module Repositories
      class SplitsRepository < Repository
        def initialize(adapter)
          @adapter = adapter

          @adapter.set_string(namespace_key('split.till'), '-1')
          @adapter.initialize_map(namespace_key('segments.registered'))
        end

        def add_split(split)
          @adapter.set_string(namespace_key("split.#{split[:name]}"), split.to_json)
        end

        def remove_split(name)
          @adapter.delete(namespace_key("split.#{name}"))
        end

        def get_split(name)
          split = @adapter.string(namespace_key("split.#{name}"))

          JSON.parse(split, symbolize_names: true)
        end

        def splits
          splits_hash = {}

          split_names.each do |name|
            splits_hash[name] = get_split(name)
          end

          splits_hash
        end

        # Return an array of Split Names excluding control keys like split.till
        def split_names
          @adapter.find_strings_by_prefix(namespace_key('split'))
            .reject { |split| split == namespace_key('split.till') }
            .map { |split| split.gsub(namespace_key('split.'), '') }
        end

        def set_change_number(since)
          @adapter.set_string(namespace_key('split.till'), since)
        end

        def get_change_number
          @adapter.string(namespace_key('split.till'))
        end

        def set_segment_names(names)
          return if names.nil? || names.empty?

          names.each do |name|
            @adapter.add_to_set(namespace_key('segments.registered'), name)
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
splitclient-rb-3.1.0.pre.rc5 lib/cache/repositories/splits_repository.rb