<commit_before>require 'redis'

module Makara
  module StateCaches
    class Redis < Abstract

      class << self

        def connect(options = {})
          @client ||= ::Redis.new(options)
          true
        end

        def client
          @client || ::Redis.current
        end

      end

      def get(key)
        with_session_key(key) do |session_key|
          client.get(session_key)
        end
      end

      def set(key, value, ttl)
        with_session_key(key) do |session_key|
          client.setex(session_key, ttl, value)
        end
      end

      def del(key)
        with_session_key(key) do |session_key|
          client.del(session_key)
        end
      end

      protected

      def client
        self.class.client
      end

    end
  end
end<commit_msg>add timeout around redis state cache<commit_after>require 'timeout'
require 'redis'

module Makara
  module StateCaches
    class Redis < Abstract

      class << self

        def connect(options = {})
          @client ||= begin
            @timeout = options[:makara_timeout]
            ::Redis.new(options.except(:makara_timeout))
          end
          true
        end

        def client
          @client || ::Redis.current
        end

        def timeout
          @timeout || 1
        end

      end

      def get(key)
        with_session_key(key) do |session_key|
          client.get(session_key)
        end
      end

      def set(key, value, ttl)
        with_session_key(key) do |session_key|
          client.setex(session_key, ttl, value)
        end
      end

      def del(key)
        with_session_key(key) do |session_key|
          client.del(session_key)
        end
      end

      protected

      def client
        self.class.client
      end

      def with_session_key(key)
        Timeout::timeout(self.class.timeout) do
          super(key)
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.07612095933263817, 'fraction_numerical': 0.0005213764337851929, 'mean_word_length': 1.5758389261744967, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6624090', 'n_tokens_mistral': 586, 'n_tokens_neox': 561, 'n_words': 140}