Sha256: 064795cf1156ac6e4e2ee0c6e647edb7b6f6cebc21479d7e7e70b7c24068f162

Contents?: true

Size: 1.92 KB

Versions: 5

Compression:

Stored size: 1.92 KB

Contents

module TwitterFriendly
  module CachingAndLogging

    module_function

    # TODO 1つのメソッドに対して1回しか実行されないようにする
    # 全体をキャッシュさせ、さらにロギングを行う
    def caching(*method_names)
      method_names.each do |method_name|

        define_method(method_name) do |*args|
          options = args.dup.extract_options!
          Instrumenter.start_processing(method_name, options)

          Instrumenter.complete_processing(method_name, options) do

            key = CacheKey.gen(method_name, args, hash: credentials_hash)
            @cache.fetch(key, args: [method_name, options]) do
              Instrumenter.perform_request(method_name, options) {super(*args)}
            end
          end
        end
      end
    end

    # 全体をキャッシュせずにロギングだけを行う
    def logging(*root_args)
      root_args.each do |method_name|
        define_method(method_name) do |*args|
          options = args.dup.extract_options!
          Instrumenter.start_processing(method_name, options)

          Instrumenter.complete_processing(method_name, options) {super(*args)}
        end
      end
    end

    module Instrumenter

      module_function

      def start_processing(method_name, options)
        payload = {operation: method_name}.merge(options)
        ::ActiveSupport::Notifications.instrument('start_processing.twitter_friendly', payload) {}
      end

      def complete_processing(method_name, options)
        payload = {operation: method_name}.merge(options)
        ::ActiveSupport::Notifications.instrument('complete_processing.twitter_friendly', payload) { yield(payload) }
      end

      def perform_request(method_name, options, &block)
        payload = {operation: 'request', args: [method_name, options]}
        ::ActiveSupport::Notifications.instrument('request.twitter_friendly', payload) { yield(payload) }
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
twitter_friendly-2.0.1.pre.alpha lib/twitter_friendly/caching_and_logging.rb
twitter_friendly-2.0.0.pre.alpha lib/twitter_friendly/caching_and_logging.rb
twitter_friendly-1.2.3 lib/twitter_friendly/caching_and_logging.rb
twitter_friendly-1.2.2 lib/twitter_friendly/caching_and_logging.rb
twitter_friendly-1.2.1 lib/twitter_friendly/caching_and_logging.rb