singleton-client-test 0.7.7.3 → 0.7.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55ae7e69d2ec121acde618656b62f88d8376874379d3b6179b7f06d86f4a7034
4
- data.tar.gz: 5b7395f4fed5d9542554a68788d4fd03e5ef7869816b868c45d34738ca58c4ab
3
+ metadata.gz: 3bd55bdad7f46bf9c0cffd42a710cfb6dcae97ecadbbb148af41d2b44be0afca
4
+ data.tar.gz: 026ef3a390e9a88691decb97b1131ac31e358c9ede7628a7b698d38899a80465
5
5
  SHA512:
6
- metadata.gz: f9e93a9961f77802fbabf6c3272787dfae5a4c42ee0d6f12b315b31fe176aefd3feeadf49e611ae60c70cff3c0ba7437f829a8364ea39d0a3edfcebac2f1cef2
7
- data.tar.gz: 16108cd1e8008453bc19a1c9a0a6f3fd2e5fd28e06c95894fdda341180ec21a775802554c18b8da9cc8d938bfeb379646df8651aaeae881be30f98cb68db9b80
6
+ metadata.gz: 5792e7d5f543603927732321cf440be37266fdc5a65fdd6075017068bb98fd9e5b8b4c52e8421fb0508166997993117d6c151921243b642ea448fb5ceea2e542
7
+ data.tar.gz: 7a8938ba9ffe0b6480a4b83e441b48d48eec98f5e2262019ff5db001e94fe50d404cc3c4277e464cab90ab7141076223b370d2c3a733058d95eecd940f8d8e88
@@ -4,12 +4,9 @@
4
4
  require 'sgtn-client/loader/source'
5
5
 
6
6
  module SgtnClient
7
- autoload :CacheUtil, 'sgtn-client/util/cache-util'
8
-
9
7
  class Source
10
8
  def self.loadBundles(locale)
11
9
  SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
12
- SgtnClient::Config.configurations.default = locale
13
10
  end
14
11
  end
15
12
  end
@@ -1,6 +1,8 @@
1
1
  # Copyright 2022 VMware, Inc.
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
+ require 'request_store'
5
+
4
6
  module SgtnClient
5
7
  class T
6
8
  def self.s(key)
@@ -8,19 +8,19 @@ module SgtnClient
8
8
  module Implementation
9
9
  # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
10
10
  def getString(component, key, locale)
11
- SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
11
+ SgtnClient.logger.debug { "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}" }
12
12
  translate(key, component, locale) { nil }
13
13
  end
14
14
 
15
15
  # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
16
16
  def getString_p(component, key, plural_args, locale)
17
- SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
17
+ SgtnClient.logger.debug { "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}" }
18
18
  translate(key, component, locale, **plural_args) { nil }
19
19
  end
20
20
 
21
21
  # <b>DEPRECATED:</b> Please use <tt>Sgtn:translate</tt> instead.
22
22
  def getString_f(component, key, args, locale, *_optionals)
23
- SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
23
+ SgtnClient.logger.debug { "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}" }
24
24
  s = translate(key, component, locale) { nil }
25
25
  return nil if s.nil?
26
26
 
@@ -39,16 +39,16 @@ module SgtnClient
39
39
  get_translations(component, locale)
40
40
  end
41
41
 
42
- # raise error when translation is not found
43
42
  def translate(key, component, locale = nil, **kwargs)
44
- SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}"
45
-
46
- locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)
47
-
48
- result = get_bundle(component, locale)&.fetch(key, nil)
49
- if result.nil? && !LocaleUtil.is_source_locale(locale)
50
- locale = LocaleUtil.get_source_locale
51
- result = get_bundle(component, locale)&.fetch(key, nil)
43
+ SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}" }
44
+
45
+ begin
46
+ best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
47
+ messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
48
+ result = messages&.fetch(key, nil)
49
+ rescue StandardError => e
50
+ SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] translation is missing. {#{key}, #{component}, #{locale}}. #{e}" }
51
+ result = nil
52
52
  end
53
53
 
54
54
  if result.nil?
@@ -61,31 +61,21 @@ module SgtnClient
61
61
  if kwargs.empty?
62
62
  result
63
63
  else
64
- locale = result.locale if result.is_a?(SgtnClient::StringUtil)
65
- result.localize(locale) % kwargs
64
+ result.localize(actual_locale) % kwargs
66
65
  end
67
66
  end
68
67
  alias t translate
69
68
 
70
69
  def get_translations(component, locale = nil)
71
- SgtnClient.logger.debug "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}"
72
-
73
- locale = locale.nil? ? self.locale : SgtnClient::LocaleUtil.get_best_locale(locale)
74
- items = get_bundle(component, locale)
75
- if items.nil? && !LocaleUtil.is_source_locale(locale)
76
- items = get_bundle(component, LocaleUtil.get_source_locale)
77
- locale = LocaleUtil.get_source_locale
78
- end
70
+ SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}" }
79
71
 
80
- { 'component' => component, 'locale' => locale, 'messages' => items || {} } if items
81
- end
82
-
83
- def locale
84
- RequestStore.store[:locale] ||= SgtnClient::LocaleUtil.get_fallback_locale
85
- end
72
+ best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
73
+ messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
86
74
 
87
- def locale=(value)
88
- RequestStore.store[:locale] = SgtnClient::LocaleUtil.get_best_locale(value)
75
+ { 'component' => component, 'locale' => actual_locale, 'messages' => messages } if messages
76
+ rescue StandardError => e
77
+ SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] translations are missing. {#{component}, #{locale}}. #{e}"
78
+ nil
89
79
  end
90
80
 
91
81
  private
@@ -99,15 +89,29 @@ module SgtnClient
99
89
  end
100
90
 
101
91
  def get_bundle!(component, locale)
102
- id = SgtnClient::Common::BundleID.new(component, locale)
103
- bundles = SgtnClient::Config.available_bundles
104
- unless bundles.nil? || bundles.empty? || bundles.include?(id)
105
- raise SgtnClient::SingletonError, 'bundle is unavailable.'
106
- end
92
+ SgtnClient.config.loader.get_bundle(component, locale)
93
+ rescue StandardError
94
+ # delete the locale from the available_bundles of component to avoid repeated calls to server
95
+ SgtnClient.config.available_bundles.delete(Common::BundleID.new(component, locale))
96
+ SgtnClient.config.available_locales(component)&.delete(locale)
97
+ SgtnClient.config.changed
98
+ SgtnClient.config.notify_observers(:available_locales, component)
99
+ raise
100
+ end
101
+
102
+ def get_bundle_with_fallback(component, locale)
103
+ messages = get_bundle(component, locale)
104
+ return messages, locale if messages
107
105
 
108
- SgtnClient::Config.loader.get_bundle(component, locale)
106
+ LocaleUtil.locale_fallbacks.each do |l|
107
+ next if l == locale
108
+
109
+ messages = get_bundle(component, l)
110
+ return messages, l if messages
111
+ end
109
112
  end
110
113
  end
114
+
111
115
  extend Implementation
112
116
  end
113
117
  end
@@ -1,9 +1,12 @@
1
1
  # Copyright 2022 VMware, Inc.
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
+ require 'set'
5
+ require 'time'
6
+
4
7
  module SgtnClient
5
8
  module Common
6
- class BundleID
9
+ class BundleID # :nodoc:
7
10
  attr_reader :locale, :component
8
11
 
9
12
  def initialize(component, locale)
@@ -17,14 +20,48 @@ module SgtnClient
17
20
  end
18
21
 
19
22
  def ==(other)
20
- (other.is_a? self.class) && @locale == other.locale && @component == other.component
23
+ (other.is_a? self.class) && @locale == other.locale && @component == other.component
21
24
  end
22
25
 
23
26
  alias eql? ==
24
27
 
25
28
  def to_s
26
- "locale=#{@locale}, component=#{@component}}"
29
+ "{component: #{@component}, locale: #{@locale}}"
30
+ end
31
+ end
32
+
33
+ module DataInfo # :nodoc:
34
+ attr_accessor :last_update
35
+
36
+ def initialize(*)
37
+ @last_update = Time.now
38
+ super
39
+ end
40
+
41
+ def expired?
42
+ Time.now >= @last_update + DataInfo.age
27
43
  end
44
+
45
+ def self.age
46
+ @age ||= SgtnClient.config.cache_expiry_period * 60
47
+ end
48
+ end
49
+
50
+ class BundleData < Hash # :nodoc:
51
+ include DataInfo
52
+
53
+ def initialize(*args)
54
+ if !args.empty? && args[0].is_a?(Hash)
55
+ update(args[0])
56
+ super()
57
+ else
58
+ super
59
+ end
60
+ end
61
+ end
62
+
63
+ class SetData < Set # :nodoc:
64
+ include DataInfo
28
65
  end
29
66
  end
30
67
  end
@@ -3,25 +3,24 @@
3
3
 
4
4
  module SgtnClient
5
5
  class SingleOperation
6
- def initialize(*conditions, &block)
7
- raise 'no way to create a new obj' unless block
6
+ def initialize(*conditions, &creator)
7
+ raise 'no way to create a new obj' unless creator
8
8
 
9
9
  @lock = Mutex.new
10
10
  @hash = {}
11
11
 
12
12
  @conditions = conditions
13
- @creator = block
13
+ @creator = creator
14
14
  end
15
15
 
16
- # return new created object
17
- def operate(id, *args)
16
+ # return new created object (return nil possibly)
17
+ def operate(id, &block)
18
18
  @lock.synchronize do
19
19
  obj = @hash[id]
20
20
  @conditions.each do |con|
21
- return obj unless con.call(id, obj, *args)
21
+ return obj unless con.call(id, obj)
22
22
  end
23
- # TODO: whatif returning nil
24
- @hash[id] = @creator.call(id, obj, *args)
23
+ @hash[id] = @creator.call(id, &block)
25
24
  end
26
25
  end
27
26
 
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2022 VMware, Inc.
4
+ # SPDX-License-Identifier: EPL-2.0
5
+
6
+ module SgtnClient
7
+ module Common # :nodoc:
8
+ autoload :BundleID, 'sgtn-client/common/data'
9
+ end
10
+ end
@@ -1,205 +1,88 @@
1
1
  # Copyright 2022 VMware, Inc.
2
2
  # SPDX-License-Identifier: EPL-2.0
3
3
 
4
- require 'erb'
5
- require 'yaml'
4
+ require 'concurrent/map'
6
5
  require 'observer'
6
+ require 'set'
7
+ require 'singleton'
7
8
 
8
9
  module SgtnClient
9
- #include Exceptions
10
-
11
- module TranslationLoader
12
- autoload :LoaderFactory, 'sgtn-client/loader/loader_factory'
13
- end
10
+ class Config # :nodoc:
11
+ include Observable
12
+ include Singleton
13
+
14
+ attr_accessor :product_name, :version, :vip_server, :translation_bundle, :source_bundle, :cache_expiry_period, :log_file, :log_level
15
+
16
+ attr_writer :logger
17
+
18
+ def logger
19
+ @logger ||= if log_file
20
+ puts "create log file: '#{log_file}', level: #{log_level}"
21
+ require 'logging'
22
+ logger = Logging.logger(log_file, 4, 1_048_576)
23
+ logger.level = log_level
24
+ logger
25
+ else
26
+ require 'logger'
27
+ Logger.new(STDOUT, level: log_level || Logger::INFO)
28
+ end
29
+ end
14
30
 
15
- module Configuration
31
+ def loader
32
+ @loader ||= TranslationLoader::LoaderFactory.create(self)
33
+ end
16
34
 
17
- def config
18
- @config ||= Config.config
35
+ def available_bundles
36
+ loader.available_bundles
37
+ rescue StandardError => e
38
+ SgtnClient.logger.error 'failed to get available bundles'
39
+ SgtnClient.logger.error e
40
+ Set.new
19
41
  end
20
42
 
21
- def set_config(env, override_configurations = {})
22
- @config =
23
- case env
24
- when Config
25
- env
26
- when Hash
27
- begin
28
- config.dup.merge!(env)
29
- rescue Errno::ENOENT => error
30
- Config.new(env)
31
- end
32
- else
33
- Config.config(env, override_configurations)
34
- end
43
+ def available_components
44
+ bundles = available_bundles
45
+ return Set.new if bundles.nil? || bundles.empty?
46
+
47
+ define_bundles_methods(bundles) unless bundles.respond_to?(:components)
48
+ bundles.components
35
49
  end
36
50
 
37
- alias_method :config=, :set_config
51
+ def available_locales(component)
52
+ bundles = available_bundles
53
+ return Set.new if bundles.nil? || bundles.empty?
38
54
 
39
- end
40
-
41
-
42
- class Config
43
- extend Observable
44
-
45
- attr_accessor :username, :password, :signature, :app_id, :cert_path,
46
- :token, :token_secret, :subject,
47
- :http_timeout, :http_proxy,
48
- :device_ipaddress, :sandbox_email_address,
49
- :mode, :endpoint, :merchant_endpoint, :platform_endpoint, :ipn_endpoint,
50
- :rest_endpoint, :rest_token_endpoint, :client_id, :client_secret,
51
- :openid_endpoint, :openid_redirect_uri, :openid_client_id, :openid_client_secret,
52
- :verbose_logging, :product_name, :version, :vip_server,
53
- :translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache, :default_language
54
-
55
-
56
- # Create Config object
57
- # === Options(Hash)
58
- # * <tt>username</tt> -- Username
59
- # * <tt>password</tt> -- Password
60
- # * <tt>signature</tt> (Optional if certificate present) -- Signature
61
- # * <tt>app_id</tt> -- Application ID
62
- # * <tt>cert_path</tt> (Optional if signature present) -- Certificate file path
63
- def initialize(options)
64
- merge!(options)
55
+ define_bundles_methods(bundles) unless bundles.respond_to?(:locales)
56
+ bundles.locales(component)
65
57
  end
66
58
 
67
- # Override configurations
68
- def merge!(options)
59
+ def update(options)
69
60
  options.each do |key, value|
70
61
  send("#{key}=", value)
71
62
  end
72
- self
73
63
  end
74
-
75
- class << self
76
-
77
- @@config_cache = {}
78
- def load(file_name, default_env = default_environment)
79
- @@config_cache = {}
80
- @@configurations = read_configurations(file_name)
81
- @@default_environment = default_env
82
- config
83
- end
84
64
 
65
+ private
85
66
 
86
- # Get default environment name
87
- def default_environment
88
- @@default_environment ||= ENV['SGTN_ENV'] || ENV['RACK_ENV'] || ENV['RAILS_ENV'] || "development"
89
- end
90
-
67
+ def define_bundles_methods(bundles)
68
+ bundles.instance_eval do |_|
69
+ @component_locales ||= Concurrent::Map.new
91
70
 
92
- # Set default environment
93
- def default_environment=(env)
94
- @@default_environment = env.to_s
71
+ def components
72
+ @components ||= reduce(Set.new) { |components, id| components << id.component }
95
73
  end
96
74
 
97
- def configure(options = {}, &block)
98
- begin
99
- self.config.merge!(options)
100
- rescue Errno::ENOENT
101
- self.configurations = { default_environment => options }
102
- end
103
- block.call(self.config) if block
104
- self.config
105
- end
106
- alias_method :set_config, :configure
107
-
108
- # Create or Load Config object based on given environment and configurations.
109
- # === Attributes
110
- # * <tt>env</tt> (Optional) -- Environment name
111
- # * <tt>override_configuration</tt> (Optional) -- Override the configuration given in file.
112
- # === Example
113
- # Config.config
114
- # Config.config(:development)
115
- # Config.config(:development, { :app_id => "XYZ" })
116
- def config(env = default_environment, override_configuration = {})
117
- if env.is_a? Hash
118
- override_configuration = env
119
- env = default_environment
120
- end
121
- if override_configuration.nil? or override_configuration.empty?
122
- default_config(env)
123
- else
124
- default_config(env).dup.merge!(override_configuration)
125
- end
126
- end
127
-
128
- def default_config(env = nil)
129
- env = (env || default_environment).to_s
130
- if configurations[env]
131
- @@config_cache[env] ||= new(configurations[env])
132
- else
133
- raise SgtnClient::Exceptions::MissingConfig.new("Configuration[#{env}] NotFound")
134
- end
135
- end
136
-
137
- # Get raw configurations in Hash format.
138
- def configurations
139
- @@configurations ||= read_configurations
140
- end
141
-
142
- # Set configuration
143
- def configurations=(configs)
144
- @@config_cache = {}
145
- @@configurations = configs && Hash[configs.map{|k,v| [k.to_s, v] }]
146
- end
75
+ def locales(component)
76
+ @component_locales[component] ||= begin
77
+ return Set.new unless components.include?(component)
147
78
 
148
- # Set logger
149
- def logger=(logger)
150
- Logging.logger = logger
151
- end
152
-
153
- # Get logger
154
- def logger
155
- if @@configurations[:mode] == 'live' and Logging.logger.level == Logger::DEBUG
156
- Logging.logger.warn "DEBUG log level not allowed in live mode for security of confidential information. Changing log level to INFO..."
157
- Logging.logger.level = Logger::INFO
79
+ each_with_object(Set.new) { |id, locales| locales << id.locale if id.component == component }
158
80
  end
159
- Logging.logger
160
81
  end
82
+ end
161
83
 
162
-
163
- def loader
164
- @loader ||= begin
165
- config = SgtnClient::Config.configurations[SgtnClient::Config.default_environment]
166
- SgtnClient::TranslationLoader::LoaderFactory.create(config)
167
- end
168
- end
169
-
170
- def available_bundles
171
- loader.available_bundles
172
- rescue StandardError => e
173
- SgtnClient.logger.error 'failed to get available bundles'
174
- SgtnClient.logger.error e
175
- Set.new
176
- end
177
-
178
- def available_locales
179
- bundles = available_bundles
180
- return Set.new if bundles.nil? || bundles.empty?
181
-
182
- unless bundles.respond_to?(:locales)
183
- def bundles.locales
184
- @locales ||= reduce(Set.new) { |locales, id| locales << id.locale }
185
- end
186
- changed
187
- notify_observers(:available_locales)
188
- end
189
- bundles.locales
190
- end
191
-
192
- private
193
- # Read configurations from the given file name
194
- # === Arguments
195
- # * <tt>file_name</tt> (Optional) -- Configuration file path
196
- def read_configurations(file_name = "config/sgtnclient.yml")
197
- erb = ERB.new(File.read(file_name))
198
- erb.filename = file_name
199
- YAML.load(erb.result)
200
- end
201
-
84
+ changed
85
+ notify_observers(:available_locales)
202
86
  end
203
87
  end
204
-
205
88
  end
@@ -0,0 +1,48 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
4
+ require 'i18n'
5
+
6
+ module SgtnClient # :nodoc:
7
+ # When integrating Singleton in a client application that is already using [I18n::Backend (https://www.rubydoc.info/github/svenfuchs/i18n/master/I18n/Backend/), it would be useful to have Singleton override the said module in order to minimize necessary changes. Here is a common usage:
8
+ #
9
+ # I18n::Backend::Simple.include(I18n::Backend::Fallbacks) # add fallbacks behavior to current backend
10
+ # I18n.backend = I18n::Backend::Chain.new(Sgtn::I18nBackend.new(component_name), I18n.backend)
11
+ # I18n.enforce_available_locales=false # disable available locales check
12
+ # I18n.default_locale = :en
13
+ class I18nBackend
14
+ def initialize(component)
15
+ @component = component
16
+ end
17
+
18
+ def initialized?
19
+ @initialized ||= true
20
+ end
21
+
22
+ def load_translations(*) end
23
+
24
+ def store_translations(*) end
25
+
26
+ def available_locales
27
+ SgtnClient.config.available_locales(@component).to_a
28
+ end
29
+
30
+ def reload!; end
31
+
32
+ def eager_load!; end
33
+
34
+ def translations; end
35
+
36
+ def exists?(locale, key, options)
37
+ !!(translate(locale, key, options) { nil })
38
+ end
39
+
40
+ def translate(locale, key, options)
41
+ flat_key = I18n::Backend::Flatten.normalize_flat_keys(locale, key, options[:scope], '.')
42
+ values = options.except(*I18n::RESERVED_KEYS)
43
+ Translation.translate(flat_key, @component, locale, **values) { nil }
44
+ end
45
+
46
+ def localize(locale, object, format, options) end
47
+ end
48
+ end
@@ -3,68 +3,52 @@
3
3
  # Copyright 2022 VMware, Inc.
4
4
  # SPDX-License-Identifier: EPL-2.0
5
5
 
6
- module SgtnClient
7
- autoload :CacheUtil, 'sgtn-client/util/cache-util'
6
+ require 'concurrent/map'
8
7
 
8
+ module SgtnClient
9
9
  module TranslationLoader
10
- autoload :CONSTS, 'sgtn-client/loader/consts'
11
-
12
10
  module Cache # :nodoc:
11
+ def initialize(*)
12
+ @cache_hash = Concurrent::Map.new
13
+ super
14
+ end
15
+
13
16
  # get from cache, return expired data immediately
14
17
  def get_bundle(component, locale)
15
- SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
18
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}" }
16
19
 
17
- key = SgtnClient::CacheUtil.get_cachekey(component, locale)
18
- cache_item = SgtnClient::CacheUtil.get_cache(key)
19
- if cache_item
20
- if SgtnClient::CacheUtil.is_expired(cache_item)
21
- Thread.new do # TODO: Use one thread # refresh in background
22
- begin
23
- load_bundle(component, locale)
24
- rescue StandardError => e
25
- SgtnClient.logger.error "an error occured while loading bundle: component=#{component}, locale=#{locale}"
26
- SgtnClient.logger.error e
27
- end
28
- end
29
- end
30
- return cache_item.dig(:items)
31
- end
20
+ result = @cache_hash[Common::BundleID.new(component, locale)] || load_bundle(component, locale)
21
+ ensure
22
+ load_bundle(component, locale, sync: false) if result&.expired?
23
+ end
32
24
 
33
- load_bundle(component, locale) # refresh synchronously if not in cache
25
+ def available_bundles
26
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
27
+
28
+ result = @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY] || super
29
+ ensure
30
+ super(sync: false) if result&.expired?
34
31
  end
32
+ end
35
33
 
36
- # load and save to cache
34
+ module CacheFiller # :nodoc:
37
35
  def load_bundle(component, locale)
38
- SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}"
36
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] CacheFiller, component=#{component}, locale=#{locale}" }
39
37
 
40
- key = SgtnClient::CacheUtil.get_cachekey(component, locale)
41
- item = super
42
- SgtnClient::CacheUtil.write_cache(key, item) if item
43
- item
38
+ @cache_hash[Common::BundleID.new(component, locale)] = Common::BundleData.new(super)
44
39
  end
45
40
 
46
41
  def available_bundles
47
- SgtnClient.logger.debug "[#{__FILE__}][#{__callee__}]"
48
-
49
- cache_item = SgtnClient::CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
50
- if cache_item
51
- if SgtnClient::CacheUtil.is_expired(cache_item)
52
- Thread.new do # TODO: Use one thread
53
- begin
54
- item = super
55
- SgtnClient::CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
56
- rescue StandardError => e
57
- SgtnClient.logger.error 'an error occured while loading available bundles.'
58
- SgtnClient.logger.error e
59
- end
60
- end
61
- end
62
- return cache_item.dig(:items)
63
- end
42
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] CacheFiller" }
64
43
 
65
44
  item = super
66
- SgtnClient::CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item
67
- item
45
+ old_item = @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY]
46
+ if item != old_item # only update if different
47
+ @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY] = Common::SetData.new(item)
48
+ else # if same, don't need to update the data, but update last_update
49
+ old_item.last_update = Time.now
50
+ old_item
51
+ end
68
52
  end
69
53
  end
70
54
  end