Module: TwitterFriendly::Caching

Included in:
Client
Defined in:
lib/twitter_friendly/caching.rb

Instance Method Summary collapse

Instance Method Details

#caching_resources_with_cursor(*method_names) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/twitter_friendly/caching.rb', line 59

def caching_resources_with_cursor(*method_names)
  method_names.each do |method_name|
    define_method(method_name) do |*args|
      options = args.dup.extract_options!

      if options.has_key?(:cursor)
        TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(method_name, options)

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

#caching_tweets_with_max_id(*method_names) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/twitter_friendly/caching.rb', line 27

def caching_tweets_with_max_id(*method_names)
  method_names.each do |method_name|
    max_count =
        case method_name
        when :home_timeline     then TwitterFriendly::REST::Timelines::MAX_TWEETS_PER_REQUEST
        when :user_timeline     then TwitterFriendly::REST::Timelines::MAX_TWEETS_PER_REQUEST
        when :mentions_timeline then TwitterFriendly::REST::Timelines::MAX_TWEETS_PER_REQUEST
        when :favorites         then TwitterFriendly::REST::Favorites::MAX_TWEETS_PER_REQUEST
        when :search            then TwitterFriendly::REST::Search::MAX_TWEETS_PER_REQUEST
        else raise "Unknown method #{method_name}"
        end

    define_method(method_name) do |*args|
      options = {count: max_count}.merge(args.extract_options!)
      args << options

      if options[:count] <= max_count
        TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(method_name, options)

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

#caching_usersObject

他のメソッドと違い再帰的に呼ばれるため、全体をキャッシュすると、すべてを再帰的にキャッシュしてしまう。 それを防ぐために、特別にここでキャッシュの処理を登録している。



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twitter_friendly/caching.rb', line 6

def caching_users
  method_name = :users

  define_method(method_name) do |*args|
    if args[0].size <= TwitterFriendly::REST::Users::MAX_USERS_PER_REQUEST
      options = args.dup.extract_options!
      TwitterFriendly::CachingAndLogging::Instrumenter.start_processing(method_name, options)

      TwitterFriendly::CachingAndLogging::Instrumenter.complete_processing(method_name, options) do

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