Class: Opener::ChainedDaemon::LanguagesCache

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin
Defined in:
lib/opener/chained_daemon/languages_cache.rb

Constant Summary collapse

UPDATE_INTERVAL =
(ENV['CACHE_EXPIRE_MINS']&.to_i || 5).minutes

Instance Method Summary collapse

Constructor Details

#initializeLanguagesCache

Returns a new instance of LanguagesCache.



9
10
11
12
13
14
15
# File 'lib/opener/chained_daemon/languages_cache.rb', line 9

def initialize
  super #MonitorMixin

  @url          = ENV['SUPPORTED_LANGUAGES_URL']
  @cache        = []
  @last_updated = nil
end

Instance Method Details

#cache_updateObject



24
25
26
27
28
29
30
31
# File 'lib/opener/chained_daemon/languages_cache.rb', line 24

def cache_update
  puts "loading supported languages from url #{@url}" if ENV['DEBUG']

  languages     = SymMash.new JSON.parse ChainedDaemon.http.get(@url).body
  @last_updated = Time.now
  @cache        = languages.data.each.with_object({}){ |l,h| h[l.code] = l }
  @cache
end

#getObject



17
18
19
20
21
22
# File 'lib/opener/chained_daemon/languages_cache.rb', line 17

def get
  synchronize do
    break @cache if @last_updated and @last_updated > UPDATE_INTERVAL.ago
    cache_update
  end
end