Class: TwitterWithAutoPagination::Cache
- Inherits:
-
Object
- Object
- TwitterWithAutoPagination::Cache
- Extended by:
- Forwardable
- Defined in:
- lib/twitter_with_auto_pagination/cache.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
- #fetch(method, user, options = {}, &block) ⇒ Object
-
#initialize(*args) ⇒ Cache
constructor
A new instance of Cache.
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) = args. if ['cache_store'] || [:cache_store] @client = ['cache_store'] || [:cache_store] else path = ['cache_dir'] || [:cache_dir] || File.join('tmp', 'twitter_cache') ttl = ['cache_ttl'] || [: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
#client ⇒ Object (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, = {}, &block) key = normalize_key(method, user, .except(:args)) block_result = nil fetch_result = @client.fetch(key) do block_result = yield Serializer.encode(block_result, args: [:args]) end block_result ? block_result : Serializer.decode(fetch_result, args: [:args]) end |