Sha256: 537e8188ce6b1e91a0413b216cef43782f64b2405c28a8518d06218a4a68411f

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 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 to a key with expiration
    def record(key, value)
      return unless key && value

      key = namespaced_key(key)
      value = Integer(value)
      connection { |c| c.setex key, SECONDS_TO_EXPIRE, value }
    rescue StandardError, TypeError => e
      Configuration.instance.exception_adapter.call e
      nil
    end

    def retrieve(key)
      return unless key

      key = namespaced_key(key)
      value = connection { |c| c.get key }
      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:'
    SECONDS_TO_EXPIRE = 24 * 60 * 60 # 24 hours
    START = 0
    STOP = 0
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
salesforce_streamer-0.3.0 lib/salesforce_streamer/redis_replay.rb