Module: SgtnClient::TranslationLoader::Cache

Defined in:
lib/sgtn-client/loader/cache.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#available_bundlesObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sgtn-client/loader/cache.rb', line 42

def available_bundles
  SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}]"

  cache_item = CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
  if cache_item
    if CacheUtil.is_expired(cache_item)
      Thread.new do # TODO: Use one thread
        begin
          item = super
          CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
        rescue StandardError => e
          SgtnClient.logger.error 'an error occured while loading available bundles.'
          SgtnClient.logger.error e
        end
      end
    end
    return cache_item.dig(:items)
  end

  item = super
  CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
  item
end

#get_bundle(component, locale) ⇒ Object

get from cache, return expired data immediately



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sgtn-client/loader/cache.rb', line 10

def get_bundle(component, locale)
  SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"

  key = CacheUtil.get_cachekey(component, locale)
  cache_item = CacheUtil.get_cache(key)
  if cache_item
    if CacheUtil.is_expired(cache_item)
      Thread.new do # TODO: Use one thread # refresh in background
        begin
          load_bundle(component, locale)
        rescue StandardError => e
          SgtnClient.logger.error "an error occured while loading bundle: component=#{component}, locale=#{locale}"
          SgtnClient.logger.error e
        end
      end
    end
    return cache_item.dig(:items)
  end

  load_bundle(component, locale) # refresh synchronously if not in cache
end

#load_bundle(component, locale) ⇒ Object

load and save to cache



33
34
35
36
37
38
39
40
# File 'lib/sgtn-client/loader/cache.rb', line 33

def load_bundle(component, locale)
  SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"

  key = CacheUtil.get_cachekey(component, locale)
  item = super
  CacheUtil.write_cache(key, item) if item
  item
end