Sha256: 80abcd237dca402904867c45a4f3b9d7af5cc5193934d0561e9447f595ffa97d

Contents?: true

Size: 1.84 KB

Versions: 22

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  module Cache
    module Repositories
      module Impressions
        class RedisRepository < ImpressionsRepository
          EXPIRE_SECONDS = 3600

          def initialize(adapter)
            @adapter = adapter
          end

          def add(matching_key, bucketing_key, split_name, treatment, time)
            add_bulk(matching_key, bucketing_key, { split_name => treatment }, time)
          end

          def add_bulk(matching_key, bucketing_key, treatments, time)
            impressions = treatments.map do |split_name, treatment|
              {
                m: metadata,
                i: impression_data(
                  matching_key,
                  bucketing_key,
                  split_name,
                  treatment,
                  time
                )
              }.to_json
            end

            impressions_list_size = @adapter.add_to_queue(key, impressions)

            # Synchronizer might not be running
            @adapter.expire(key, EXPIRE_SECONDS) if impressions.size == impressions_list_size
          end

          def get_impressions(number_of_impressions = 0)
            @adapter.get_from_queue(key, number_of_impressions).map do |e|
              impression = JSON.parse(e, symbolize_names: true)
              impression[:i][:f] = impression[:i][:f].to_sym
              impression
            end
          rescue StandardError => e
            SplitIoClient.configuration.logger.error("Exception while clearing impressions cache: #{e}")
            []
          end

          def batch
            get_impressions(SplitIoClient.configuration.impressions_bulk_size)
          end

          def clear
            get_impressions
          end

          def key
            @key ||= namespace_key('.impressions')
          end
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
splitclient-rb-6.4.1-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc3-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc3 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc2-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc2 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc1-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.1.pre.rc1 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.0-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.0 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.0.pre.rc1-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.4.0.pre.rc1 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.3.0-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.3.0 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.3.0.pre.rc1-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.3.0.pre.rc1 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.2.0-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.2.0 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.2.0.pre.rc2-java lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb
splitclient-rb-6.2.0.pre.rc2 lib/splitclient-rb/cache/repositories/impressions/redis_repository.rb