<commit_before>module CachedResource
  # The Caching module is included in ActiveResource and
  # handles caching and recaching of responses.
  module Caching
    extend ActiveSupport::Concern

    # when included, setup a middle man for find
    included do
      class << self
        alias_method_chain :find, :cache
      end
    end

    module ClassMethods

      # find a resource using the cache or resend the request
      # if :reload is set to true or caching is disabled
      def find_with_cache(*arguments)
        arguments << {} unless arguments.last.is_a?(Hash)
        should_reload = arguments.last.delete(:reload) || !CachedResource.config.cache_enabled
        arguments.pop if arguments.last.empty?
        key = cache_key(arguments)

        begin
          (should_reload ? find_with_reload(key, *arguments) : find_with_read_through_cache(key, *arguments))
        rescue ActiveResource::ServerError, ActiveResource::ConnectionError, SocketError => e
          raise(e)
        end
      end

      private

      # try to find a cached response for the given key.  If
      # no cache entry exists, send a new request.
      def find_with_read_through_cache(key, *arguments)
        result = CachedResource.cache.read(key).try(:dup)
        result && log(:read, "#{key} for #{arguments.inspect}")
        result || find_with_reload(key, *arguments)
      end

      # re/send the request to fetch the resource. Cache the response
      # for the request.
      def find_with_reload(key, *arguments)
        result = find_without_cache(*arguments)
        CachedResource.cache.write(key, result, :expires_in => CachedResource.config.cache_time_to_live)
        log(:write, "#{key} for #{arguments.inspect}")
        result
      end

      # generate the request cache key
      def cache_key(*arguments)
        "#{name.parameterize.gsub("-", "/")}/#{arguments.join('/')}".downcase
      end

      # log a message indicating a cached resource event
      def log(type, msg)
        c = Term::ANSIColor
        type_string = "CachedResource #{type.to_s.classify.upcase}"

        case type
        when :read
          type_string = c.intense_black + c.bold + type_string + c.clear
        when :write
          type_string = c.yellow + c.bold + type_string + c.clear<commit_msg>Break CachedResource into two words for log messages<commit_after>module CachedResource
  # The Caching module is included in ActiveResource and
  # handles caching and recaching of responses.
  module Caching
    extend ActiveSupport::Concern

    # when included, setup a middle man for find
    included do
      class << self
        alias_method_chain :find, :cache
      end
    end

    module ClassMethods

      # find a resource using the cache or resend the request
      # if :reload is set to true or caching is disabled
      def find_with_cache(*arguments)
        arguments << {} unless arguments.last.is_a?(Hash)
        should_reload = arguments.last.delete(:reload) || !CachedResource.config.cache_enabled
        arguments.pop if arguments.last.empty?
        key = cache_key(arguments)

        begin
          (should_reload ? find_with_reload(key, *arguments) : find_with_read_through_cache(key, *arguments))
        rescue ActiveResource::ServerError, ActiveResource::ConnectionError, SocketError => e
          raise(e)
        end
      end

      private

      # try to find a cached response for the given key.  If
      # no cache entry exists, send a new request.
      def find_with_read_through_cache(key, *arguments)
        result = CachedResource.cache.read(key).try(:dup)
        result && log(:read, "#{key} for #{arguments.inspect}")
        result || find_with_reload(key, *arguments)
      end

      # re/send the request to fetch the resource. Cache the response
      # for the request.
      def find_with_reload(key, *arguments)
        result = find_without_cache(*arguments)
        CachedResource.cache.write(key, result, :expires_in => CachedResource.config.cache_time_to_live)
        log(:write, "#{key} for #{arguments.inspect}")
        result
      end

      # generate the request cache key
      def cache_key(*arguments)
        "#{name.parameterize.gsub("-", "/")}/#{arguments.join('/')}".downcase
      end

      # log a message indicating a cached resource event
      def log(type, msg)
        c = Term::ANSIColor
        type_string = "Cached Resource #{type.to_s.upcase}"

        case type
        when :read
          type_string = c.intense_black + c.bold + type_string + c.clear
        when :write
          type_string = c.yellow + c.bold + type_string + c.clear
{'fraction_non_alphanumeric': 0.08989006251347273, 'fraction_numerical': 0.0, 'mean_word_length': 2.6593059936908516, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 7, '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': '7564201', 'n_tokens_mistral': 1299, 'n_tokens_neox': 1284, 'n_words': 387}