Sha256: 314029424f6cced35df69115351ded4350fe801aa846fdbed92bb97642c1493b

Contents?: true

Size: 890 Bytes

Versions: 2

Compression:

Stored size: 890 Bytes

Contents

# frozen_string_literal: true

require "redis"
require "json"

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.to_json)
        end

        def retrieve(key)
          value = redis.get(key)
          JSON.parse(value)
        end

        private

        attr_reader :client, :redis

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

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
twitch-bot-5.0.2 lib/twitch/bot/memory/redis.rb
twitch-bot-5.0.1 lib/twitch/bot/memory/redis.rb