Sha256: ee4f6a2f09a73b4e5a4a5ba458441dcdf286c5825c8e070a72e23289e64ab98f

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

# frozen_string_literal: true

module SalesforceStreamer
  class RedisReplay
    class << self
      def redis_connection
        @redis_connection ||= Configuration.instance.redis_connection || raise(RedisConnectionError)
      end

      attr_writer :redis_connection
    end

    def connection
      if RedisReplay.redis_connection.respond_to?(:with)
        RedisReplay.redis_connection.with do |conn|
          yield(conn)
        end
      else
        yield RedisReplay.redis_connection
      end
    end

    # Saves the value in a sorted set named by the key
    # The score is the ReplayId integer value
    def record(key, value)
      return unless key && value

      key = namespaced_key(key)
      value = Integer(value)
      # The score is the value
      connection { |c| c.zadd key, value, value }
    rescue StandardError, TypeError => e
      Configuration.instance.exception_adapter.call e
      nil
    end

    # Retrives the highest value in the sorted set
    def retrieve(key)
      return unless key

      key = namespaced_key(key)
      value = connection { |c| c.zrevrange(key, START, STOP)&.first }
      Integer(value) if value
    rescue StandardError => e
      Configuration.instance.exception_adapter.call e
      nil
    end

    private

    def namespaced_key(key)
      NAMESPACE + key.to_s
    end

    NAMESPACE = 'SalesforceStreamer:'
    START = 0
    STOP = 0
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
salesforce_streamer-0.2.3 lib/salesforce_streamer/redis_replay.rb
salesforce_streamer-0.2.2 lib/salesforce_streamer/redis_replay.rb
salesforce_streamer-0.2.1 lib/salesforce_streamer/redis_replay.rb
salesforce_streamer-0.2.0 lib/salesforce_streamer/redis_replay.rb