Sha256: d620d7c5c726be84b1efa198128e8d1c47347dee6d0f26fd069293f04ac4773b

Contents?: true

Size: 893 Bytes

Versions: 3

Compression:

Stored size: 893 Bytes

Contents

# frozen_string_literal: true

require "redis"

module Twitch
  module Bot
    module Memory
      # Implement persistent memory based on Redis
      class Redis
        def initialize(client:)
          @client = client
          @redis = connect_db
        end

        def store(key, value)
          redis.set(key, value)
        end

        def retrieve(key)
          redis.get(key)
        end

        private

        attr_reader :client, :redis

        def connect_db
          url = redis_config_url || ENV["REDIS_URL"]
          ::Redis.new(url: url)
        end

        def redis_config_url
          config = client.config
          if config.setting("redis_host")
            host = config.setting("redis_host") || "localhost"
            port = config.setting("redis_port") || 6379
            "redis://#{host}:#{port}"
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
twitch-bot-4.1.1 lib/twitch/bot/memory/redis.rb
twitch-bot-4.1.0 lib/twitch/bot/memory/redis.rb
twitch-bot-4.0.1 lib/twitch/bot/memory/redis.rb