singleton-client-test 0.7.7.4 → 0.7.7.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2b1a0ab0b7bfa8a2fef42c229aae91032941499667f0885882f924d6a3f75694
4
- data.tar.gz: be51ed5dbdf30cbc7f67353b5d96ba49e4e20a5de61ea9bd7d99d4f27785e1a9
3
+ metadata.gz: 6ab2a482cbb7c83aca190392287160802cae5488a7356dfd24a79476817f40ac
4
+ data.tar.gz: e691e2eca4f3c2a2f747970ff746b045e4c1cbaa3de23e73eeba84a7f812c2ec
5
5
  SHA512:
6
- metadata.gz: d3a9b167165b5d6f6726ed8be0b07fadc7ef4aeb0c8ecb8f941bc77e5ff7ada988557f97098c33d1907fb7f24b1cac17f7df4fa3de3b96cce2e64a4240633c58
7
- data.tar.gz: 44e84a38eb2e592793546c812817b751b0366b5af2ed0591eeaed1e4dd5b014af70b33674b7da69cac2873e16fb9ab3b47ff85d7d5f463b3a3bcfd987930ee7e
6
+ metadata.gz: 619fed54ca5da856c0d8933724e69bb286deba7aebd94a6c98d674dc8c532dc2937cbee650ebb0e337351a3d0b8515dd06f1884066412009098f85c09a4373fc
7
+ data.tar.gz: 9500161a55b6e4a2c43f2955168895ddfbde82ae1fe68a6a2fcbe4aa1f1498bfaa99a3fe3f82962fac288f455f5a775e26c2193a945940eaaa4cdafbeca90fe8
@@ -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,7 +20,7 @@ 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? ==
@@ -26,5 +29,39 @@ module SgtnClient
26
29
  "{component: #{@component}, locale: #{@locale}}"
27
30
  end
28
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
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
65
+ end
29
66
  end
30
67
  end
@@ -3,51 +3,52 @@
3
3
  # Copyright 2022 VMware, Inc.
4
4
  # SPDX-License-Identifier: EPL-2.0
5
5
 
6
+ require 'concurrent/map'
7
+
6
8
  module SgtnClient
7
9
  module TranslationLoader
8
10
  module Cache # :nodoc:
11
+ def initialize(*)
12
+ @cache_hash = Concurrent::Map.new
13
+ super
14
+ end
15
+
9
16
  # get from cache, return expired data immediately
10
17
  def get_bundle(component, locale)
11
- SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}" }
12
-
13
- key = Common::BundleID.new(component, locale)
14
- cache_item = CacheUtil.get_cache(key)
15
- if cache_item
16
- load_bundle(component, locale, sync: false) if CacheUtil.is_expired(cache_item)
17
- cache_item[:items]
18
- else
19
- load_bundle(component, locale)
20
- end
18
+ SgtnClient.logger.debug { "[#{caller[2]}] component=#{component}, locale=#{locale}" }
19
+
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?
21
23
  end
22
24
 
23
25
  def available_bundles
24
- SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
25
-
26
- cache_item = CacheUtil.get_cache(CONSTS::AVAILABLE_BUNDLES_KEY)
27
- if cache_item
28
- super(sync: false) if CacheUtil.is_expired(cache_item)
29
- cache_item[:items]
30
- else
31
- super
32
- end
26
+ SgtnClient.logger.debug { "[#{caller[2]}]" }
27
+
28
+ result = @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY] || super
29
+ ensure
30
+ super(sync: false) if result&.expired?
33
31
  end
34
32
  end
35
33
 
36
- module CacheFiller
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 { "[#{caller[2]}] component=#{component}, locale=#{locale}" }
39
37
 
40
- item = super
41
- CacheUtil.write_cache(Common::BundleID.new(component, locale), item) if item
42
- item
38
+ @cache_hash[Common::BundleID.new(component, locale)] = Common::BundleData.new(super)
43
39
  end
44
40
 
45
41
  def available_bundles
46
- SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
42
+ SgtnClient.logger.debug { "[#{caller[2]}]" }
47
43
 
48
44
  item = super
49
- CacheUtil.write_cache(CONSTS::AVAILABLE_BUNDLES_KEY, item) if item # TODO: don't save when empty
50
- 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
51
52
  end
52
53
  end
53
54
  end
@@ -8,11 +8,6 @@ module SgtnClient # :nodoc:
8
8
  autoload :Common, 'sgtn-client/common'
9
9
  autoload :TranslationLoader, 'sgtn-client/loader'
10
10
  autoload :SingleOperation, 'sgtn-client/common/single_operation'
11
-
12
- module Core # :nodoc:
13
- autoload :Cache, 'sgtn-client/core/cache'
14
- end
15
-
16
11
  autoload :Translation, 'sgtn-client/api/translation'
17
12
  autoload :T, 'sgtn-client/api/t'
18
13
  autoload :Source, 'sgtn-client/api/source'
@@ -20,7 +15,6 @@ module SgtnClient # :nodoc:
20
15
  autoload :Exceptions, 'sgtn-client/core/exceptions'
21
16
  autoload :ValidateUtil, 'sgtn-client/util/validate-util'
22
17
  autoload :LocaleUtil, 'sgtn-client/util/locale-util'
23
- autoload :CacheUtil, 'sgtn-client/util/cache-util'
24
18
  autoload :StringUtil, 'sgtn-client/util/string-util'
25
19
  autoload :SingletonError, 'sgtn-client/exceptions'
26
20
  autoload :I18nBackend, 'sgtn-client/i18n_backend'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singleton-client-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7.4
4
+ version: 0.7.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - VMware G11n Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-07-12 00:00:00.000000000 Z
11
+ date: 2022-07-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -141,7 +141,6 @@ files:
141
141
  - lib/sgtn-client/common.rb
142
142
  - lib/sgtn-client/common/data.rb
143
143
  - lib/sgtn-client/common/single_operation.rb
144
- - lib/sgtn-client/core/cache.rb
145
144
  - lib/sgtn-client/core/config.rb
146
145
  - lib/sgtn-client/core/exceptions.rb
147
146
  - lib/sgtn-client/exceptions.rb
@@ -158,7 +157,6 @@ files:
158
157
  - lib/sgtn-client/loader/source.rb
159
158
  - lib/sgtn-client/loader/source_comparer.rb
160
159
  - lib/sgtn-client/sgtn-client.rb
161
- - lib/sgtn-client/util/cache-util.rb
162
160
  - lib/sgtn-client/util/locale-util.rb
163
161
  - lib/sgtn-client/util/string-util.rb
164
162
  - lib/sgtn-client/util/validate-util.rb
@@ -1,31 +0,0 @@
1
- # Copyright 2022 VMware, Inc.
2
- # SPDX-License-Identifier: EPL-2.0
3
-
4
- require 'time'
5
-
6
- module SgtnClient::Core
7
- class Cache
8
- Entry = Struct.new(:expiry, :items)
9
-
10
- def self.get(key)
11
- SgtnClient.logger.debug { "[Cache][get]get cache for key: #{key}" }
12
- return data&.dig(key)
13
- end
14
-
15
- def self.put(key, items, ttl)
16
- # hours from new
17
- SgtnClient.logger.debug { "[Cache][put]put cache for key '#{key}' with expired time at'" + (Time.now + ttl*60).to_s }
18
- data[key] = Entry.new(Time.now + ttl*60, items)
19
- end
20
-
21
- def self.clear
22
- SgtnClient.logger.debug { "[Cache][clear]clear cache!" }
23
- @data = {}
24
- end
25
-
26
- def self.data
27
- @data ||= {}
28
- end
29
- end
30
-
31
- end
@@ -1,30 +0,0 @@
1
- # Copyright 2022 VMware, Inc.
2
- # SPDX-License-Identifier: EPL-2.0
3
-
4
- require 'time'
5
-
6
- module SgtnClient
7
- class CacheUtil
8
- def self.get_cache(cache_key)
9
- Core::Cache.get(cache_key)
10
- end
11
-
12
- def self.clear_cache
13
- Core::Cache.clear
14
- end
15
-
16
- def self.write_cache(cache_key, items)
17
- return nil if items.nil? || items.empty?
18
-
19
- cache_expiry_period = SgtnClient.config.cache_expiry_period
20
- # expired after 24 hours
21
- cache_expiry_period = 24 * 60 if cache_expiry_period.nil?
22
- Core::Cache.put(cache_key, items, cache_expiry_period)
23
- end
24
-
25
- def self.is_expired(cache_item)
26
- cache_item[:expiry] < Time.now
27
- end
28
-
29
- end
30
- end