Class: GitlabInternalEventsCli::HttpCache
- Inherits:
-
Object
- Object
- GitlabInternalEventsCli::HttpCache
- Defined in:
- lib/gitlab_internal_events_cli/http_cache.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(url, timeout: 5) ⇒ Object
-
#initialize ⇒ HttpCache
constructor
A new instance of HttpCache.
- #preload! ⇒ Object
Constructor Details
#initialize ⇒ HttpCache
Returns a new instance of HttpCache.
23 24 25 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 23 def initialize @cache = {} end |
Class Method Details
.clear! ⇒ Object
14 15 16 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 14 def clear! @instance = nil end |
.get(url, timeout: 5) ⇒ Object
10 11 12 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 10 def get(url, timeout: 5) instance.get(url, timeout: timeout) end |
.instance ⇒ Object
6 7 8 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 6 def instance @instance ||= new end |
.preload! ⇒ Object
18 19 20 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 18 def preload! instance.preload! end |
Instance Method Details
#get(url, timeout: 5) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 27 def get(url, timeout: 5) return @cache[url] if @cache.key?(url) @cache[url] = Timeout.timeout(timeout) { Net::HTTP.get(URI(url)) } rescue StandardError @cache[url] = nil end |
#preload! ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/gitlab_internal_events_cli/http_cache.rb', line 35 def preload! config = GitlabInternalEventsCli.configuration urls = [ config.event_schema_url, config.metric_schema_url, config.stages_url, config.feature_categories_url ] threads = urls.map do |url| Thread.new { get(url) } end threads.each(&:join) end |