Class: TwitterWithAutoPagination::Cache

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/twitter_with_auto_pagination/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Cache

Returns a new instance of Cache.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/twitter_with_auto_pagination/cache.rb', line 14

def initialize(*args)
  options = args.extract_options!

  if options['cache_store'] || options[:cache_store]
    @client = options['cache_store'] || options[:cache_store]
  else
    path = options['cache_dir'] || options[:cache_dir] || File.join('tmp', 'twitter_cache')
    ttl = options['cache_ttl'] || options[:cache_ttl] || 1.hour
    Dir.mkdir(path) unless File.exists?(path)
    @client = ActiveSupport::Cache::FileStore.new(path, expires_in: ttl, race_condition_ttl: 5.minutes)
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



10
11
12
# File 'lib/twitter_with_auto_pagination/cache.rb', line 10

def client
  @client
end

Instance Method Details

#fetch(method, user, options = {}, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/twitter_with_auto_pagination/cache.rb', line 27

def fetch(method, user, options = {}, &block)
  key = normalize_key(method, user, options.except(:args))

  block_result = nil
  fetch_result =
    @client.fetch(key) do
      block_result = yield
      Serializer.encode(block_result, args: options[:args])
    end

  block_result ? block_result : Serializer.decode(fetch_result, args: options[:args])
end