singleton-client-test 0.7.0.31 → 0.7.0.34

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.
Files changed (37) hide show
  1. checksums.yaml +4 -4
  2. data/lib/sgtn-client/api/source.rb +10 -66
  3. data/lib/sgtn-client/api/t.rb +20 -18
  4. data/lib/sgtn-client/api/translation.rb +82 -117
  5. data/lib/sgtn-client/cldr/core_ext.rb +4 -1
  6. data/lib/sgtn-client/cldr/localized_date.rb +4 -1
  7. data/lib/sgtn-client/cldr/localized_datetime.rb +4 -1
  8. data/lib/sgtn-client/cldr/localized_str.rb +3 -1
  9. data/lib/sgtn-client/cldr/localized_time.rb +4 -1
  10. data/lib/sgtn-client/common/data.rb +30 -0
  11. data/lib/sgtn-client/common/single_operation.rb +34 -0
  12. data/lib/sgtn-client/core/cache.rb +14 -80
  13. data/lib/sgtn-client/core/config.rb +39 -3
  14. data/lib/sgtn-client/core/exceptions.rb +3 -0
  15. data/lib/sgtn-client/core/logging.rb +3 -1
  16. data/lib/sgtn-client/exceptions.rb +6 -0
  17. data/lib/sgtn-client/formatters/plurals/plural_formatter.rb +4 -1
  18. data/lib/sgtn-client/loader/cache.rb +71 -0
  19. data/lib/sgtn-client/loader/chain_loader.rb +49 -0
  20. data/lib/sgtn-client/loader/consts.rb +15 -0
  21. data/lib/sgtn-client/loader/loader_factory.rb +33 -0
  22. data/lib/sgtn-client/loader/local_translation.rb +50 -0
  23. data/lib/sgtn-client/loader/server.rb +94 -0
  24. data/lib/sgtn-client/loader/single_loader.rb +46 -0
  25. data/lib/sgtn-client/loader/source.rb +53 -0
  26. data/lib/sgtn-client/loader/source_comparer.rb +58 -0
  27. data/lib/sgtn-client/sgtn-client.rb +7 -1
  28. data/lib/sgtn-client/util/cache-util.rb +33 -44
  29. data/lib/sgtn-client/util/locale-util.rb +48 -31
  30. data/lib/sgtn-client/util/string-util.rb +12 -0
  31. data/lib/sgtn-client/util/validate-util.rb +4 -7
  32. data/lib/singleton-client.rb +15 -0
  33. data/lib/singleton-ruby.rb +5 -0
  34. data/lib/version.rb +2 -0
  35. metadata +43 -147
  36. data/lib/sgtn-client/core/request.rb +0 -21
  37. data/lib/sgtn-client/util/file-util.rb +0 -34
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d36c7df9764afccdd5c3e9ca3dac5123bd9feaedd3ada0a38e386836860f8885
4
- data.tar.gz: 9cc9248d0b25744fa3168711040e740f9c07124e60a55a9f0b73150a630ca1c4
3
+ metadata.gz: 6722a7f77f8f9ee4fbf2f481d0c66e30b818c61746a9c7e8471fc1b225079f72
4
+ data.tar.gz: ba0e6acade9bc655f97a731cf760e15097b713e99000cf8f2612e4975a357d2d
5
5
  SHA512:
6
- metadata.gz: f7d9c3d03d862fa00b6e75ad17e3d6ebf6382750bdf41630708136439980ee0ee9cbb07768f19f71f0b23ba2c2f3c2dba5e50e4d9afc136b26aa96f326076d89
7
- data.tar.gz: 0c3b4dbe2f24634090f997b3d980aa7b0a85c41485bd553020fd3e41e196947a3f2a0b27a12da3d87439ecaaee7b903542df4fbbbb4b23ee0021d01ceda96eb5
6
+ metadata.gz: 27e3c13e20185a08049c2d634bc11e3a6d4e05095802e3c76026b74c34acd3a327b2e49c79020b9143479747e9d25fbfe5eaf4c859621afdc23f512859efd0fc
7
+ data.tar.gz: bed85d6578397cd90e0823574f552d7f7e98e19a0f49e35e12963eecc32d5f87d85b8c0a8ab2c50c5c66b3ea81e630334194f8d4600fdc4a0eec2bf391b2decb
@@ -1,71 +1,15 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
4
+ require 'sgtn-client/loader/source'
1
5
 
2
6
  module SgtnClient
3
-
4
- autoload :CacheUtil, "sgtn-client/util/cache-util"
7
+ autoload :CacheUtil, 'sgtn-client/util/cache-util'
5
8
 
6
9
  class Source
7
-
8
- def self.getSource(component, key, locale)
9
- SgtnClient.logger.debug "[Source][getSource]component=#{component}, key=#{key}, locale=#{locale}"
10
- cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
11
- expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
12
- if items.nil?
13
- items = getBundle(component, locale)
14
- SgtnClient::CacheUtil.write_cache(cache_key, items)
15
- else
16
- SgtnClient.logger.debug "[Source][getSource]getting sources from cache with key: " + cache_key
17
- end
18
- s = items.nil?? nil : items[locale][key]
19
- if items.nil? || s.nil?
20
- SgtnClient.logger.debug "[Source][getSource]source not found, return key: " + key
21
- #return key
22
- return nil
23
- else
24
- return s
25
- end
26
- end
27
-
28
- def self.getSources(component, locale)
29
- SgtnClient.logger.debug "[Source][getSources]component=#{component}, locale=#{locale}"
30
- cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
31
- expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
32
- if items.nil? || expired
33
- items = getBundle(component, locale)
34
- SgtnClient::CacheUtil.write_cache(cache_key, items)
35
- else
36
- SgtnClient.logger.debug "[Source][getSources]getting sources from cache with key: " + cache_key
37
- end
38
- return items
39
- end
40
-
41
- def self.loadBundles(locale)
42
- SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
43
- env = SgtnClient::Config.default_environment
44
- SgtnClient::Config.configurations.default = locale
45
- source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
46
- Dir.foreach(source_bundle) do |component|
47
- next if component == '.' || component == '..'
48
- yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
49
- bundle = SgtnClient::FileUtil.read_yml(yamlfile)
50
- cachekey = SgtnClient::CacheUtil.get_cachekey(component, locale)
51
- SgtnClient::CacheUtil.write_cache(cachekey,bundle)
52
- end
53
- end
54
-
55
- private
56
- def self.getBundle(component, locale)
57
- SgtnClient.logger.debug "[Source][getBundle]component=#{component}, locale=#{locale}"
58
- env = SgtnClient::Config.default_environment
59
- source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
60
- bundlepath = source_bundle + "/" + component + "/" + locale + ".yml"
61
- begin
62
- bundle = SgtnClient::FileUtil.read_yml(bundlepath)
63
- rescue => exception
64
- SgtnClient.logger.error exception.message
65
- end
66
- return bundle
67
- end
68
-
10
+ def self.loadBundles(locale)
11
+ SgtnClient.logger.debug "[Source][loadBundles]locale=#{locale}"
12
+ SgtnClient::Config.configurations.default = locale
13
+ end
69
14
  end
70
-
71
- end
15
+ end
@@ -1,22 +1,24 @@
1
- module SgtnClient
2
- class T < Translation
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
3
 
4
- def self.s(key)
5
- locale = RequestStore.store[:locale]
6
- component = RequestStore.store[:component]
7
- return getString(component, key, locale)
8
- end
4
+ module SgtnClient
5
+ class T
6
+ def self.s(key)
7
+ locale = RequestStore.store[:locale]
8
+ component = RequestStore.store[:component]
9
+ Translation.getString(component, key, locale)
10
+ end
9
11
 
10
- def self.s_f(key, args)
11
- locale = RequestStore.store[:locale]
12
- component = RequestStore.store[:component]
13
- return getString_f(component, key, args, locale)
14
- end
12
+ def self.s_f(key, args)
13
+ locale = RequestStore.store[:locale]
14
+ component = RequestStore.store[:component]
15
+ Translation.getString_f(component, key, args, locale)
16
+ end
15
17
 
16
- def self.c()
17
- locale = RequestStore.store[:locale]
18
- component = RequestStore.store[:component]
19
- return getStrings(component, locale)
20
- end
18
+ def self.c
19
+ locale = RequestStore.store[:locale]
20
+ component = RequestStore.store[:component]
21
+ Translation.getStrings(component, locale)
21
22
  end
22
- end
23
+ end
24
+ end
@@ -1,148 +1,113 @@
1
- require 'multi_json'
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
2
3
 
3
- module SgtnClient
4
-
5
- module Core
6
- autoload :Request, "sgtn-client/core/request"
7
- autoload :Cache, "sgtn-client/core/cache"
8
- autoload :CacheUtil, "sgtn-client/util/cache-util"
9
- autoload :LocaleUtil, "sgtn-client/util/locale-util"
10
- end
11
-
12
- class Translation
4
+ require 'request_store'
13
5
 
14
- def self.getString(component, key, locale)
6
+ module SgtnClient
7
+ module Translation
8
+ module Implementation
9
+ # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
10
+ def getString(component, key, locale)
15
11
  SgtnClient.logger.debug "[Translation.getString]component: #{component}, key: #{key}, locale: #{locale}"
16
- str = getTranslation(component, key, locale)
17
- if str.nil?
18
- str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
19
- if str.nil?
20
- SgtnClient.logger.debug "[Translation][getString] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
21
- end
22
- end
23
- str
12
+ translate(key, component, locale) { nil }
24
13
  end
25
14
 
26
- def self.getString_p(component, key, plural_args, locale)
15
+ # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
16
+ def getString_p(component, key, plural_args, locale)
27
17
  SgtnClient.logger.debug "[Translation][getString_p]component=#{component}, key=#{key}, locale=#{locale}"
28
- str = getTranslation(component, key, locale)
29
- if str.nil?
30
- str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
31
- if str.nil?
32
- SgtnClient.logger.debug "[Translation][getString_p] Missing source string with key: #{key}, component: #{component}, locale: #{locale}"
33
- return nil
34
- end
35
- str.to_plural_s(:en, plural_args)
36
- else
37
- str.to_plural_s(locale, plural_args)
38
- end
18
+ translate(key, component, locale, **plural_args) { nil }
39
19
  end
40
20
 
41
- def self.getString_f(component, key, args, locale, *optionals)
42
- SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
43
- s = getString(component, key, locale, *optionals)
44
- if s.nil?
45
- return nil
46
- end
47
- if args.is_a?(Hash)
21
+ # <b>DEPRECATED:</b> Please use <tt>Singleton:translate</tt> instead.
22
+ def getString_f(component, key, args, locale, *_optionals)
23
+ SgtnClient.logger.debug "[Translation][getString_f]component=#{component}, key=#{key}, locale=#{locale}"
24
+ s = translate(key, component, locale) { nil }
25
+ return nil if s.nil?
26
+
27
+ if args.is_a?(Hash)
48
28
  args.each do |source, arg|
49
29
  s.gsub! "{#{source}}", arg
50
30
  end
51
- elsif args.is_a?(Array)
52
- s = sprintf s % args
53
- end
54
- return s
31
+ elsif args.is_a?(Array)
32
+ s = s % args
33
+ end
34
+ s
55
35
  end
56
36
 
57
- def self.getStrings(component, locale)
58
- SgtnClient.logger.debug "[Translation][getStrings]component=#{component}, locale=#{locale}"
59
- items = get_cs(component, locale)
60
- default = SgtnClient::Config.configurations.default
61
- if items.nil? || items["messages"] == nil
62
- items = {}
63
- s = SgtnClient::Source.getSources(component, default)
64
- if s.nil?
65
- SgtnClient.logger.error "[Translation][getStrings] Missing component: #{component}, locale: #{locale}"
66
- else
67
- default_component, value = s.first
68
- items["component"] = component
69
- items["messages"] = value
70
- items["locale"] = 'source'
71
- end
37
+ # <b>DEPRECATED:</b> Please use <tt>Singleton:get_translations</tt> instead.
38
+ def getStrings(component, locale)
39
+ get_translations(component, locale)
40
+ end
41
+
42
+ # raise error when translation is not found
43
+ 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)
72
52
  end
73
- return items
74
- end
75
53
 
54
+ if result.nil?
55
+ return key unless block_given?
76
56
 
77
- private
57
+ result = yield
58
+ return if result.nil?
59
+ end
78
60
 
79
- def self.getTranslation(component, key, locale)
80
- items = get_cs(component, locale)
81
- if items.nil? || items["messages"] == nil
82
- nil
61
+ if kwargs.empty?
62
+ result
83
63
  else
84
- items["messages"][key]
64
+ locale = result.locale if result.is_a?(SgtnClient::StringUtil)
65
+ result.localize(locale) % kwargs
85
66
  end
86
67
  end
68
+ alias t translate
87
69
 
88
- def self.get_cs(component, locale)
89
- locale = SgtnClient::LocaleUtil.process_locale(locale)
90
- flocale = SgtnClient::LocaleUtil.fallback(locale)
91
- cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
92
- SgtnClient.logger.debug "[Translation][get_cs]cache_key=#{cache_key}"
93
- expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
94
- if items.nil? || expired
95
- items = load(component, flocale)
96
- if items.nil?
97
- items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
98
- SgtnClient::Core::Cache.put(cache_key, items, 60)
99
- else
100
- SgtnClient::CacheUtil.write_cache(cache_key, items)
101
- end
102
- else
103
- SgtnClient.logger.debug "[Translation]get translations from cache with key: " + cache_key
70
+ 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
104
78
  end
105
79
 
106
- return items
107
- end
80
+ { 'component' => component, 'locale' => locale, 'messages' => items || {} } if items
81
+ end
108
82
 
109
- def self.load(component, locale)
110
- env = SgtnClient::Config.default_environment
111
- mode = SgtnClient::Config.configurations[env]["bundle_mode"]
112
- SgtnClient.logger.debug "[Translation][load]mode=#{mode}"
113
- if mode == 'offline'
114
- return load_o(component, locale)
115
- else
116
- return load_s(component, locale)
117
- end
83
+ def locale
84
+ RequestStore.store[:locale] ||= SgtnClient::LocaleUtil.get_fallback_locale
118
85
  end
119
86
 
120
- def self.load_o(component, locale)
121
- env = SgtnClient::Config.default_environment
122
- product_name = SgtnClient::Config.configurations[env]["product_name"]
123
- version = SgtnClient::Config.configurations[env]["version"].to_s
124
- translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
125
- bundlepath = translation_bundle + "/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
126
- SgtnClient::FileUtil.read_json(bundlepath)
87
+ def locale=(value)
88
+ RequestStore.store[:locale] = SgtnClient::LocaleUtil.get_best_locale(value)
127
89
  end
128
90
 
129
- def self.load_s(component, locale)
130
- env = SgtnClient::Config.default_environment
131
- product_name = SgtnClient::Config.configurations[env]["product_name"]
132
- vip_server = SgtnClient::Config.configurations[env]["vip_server"]
133
- version = SgtnClient::Config.configurations[env]["version"].to_s
134
- url = vip_server + "/i18n/api/v2/translation/products/" + product_name + "/versions/" + version + "/locales/" + locale + "/components/" + component+ "?checkTranslationStatus=false&machineTranslation=false&pseudo=false"
135
- begin
136
- obj = SgtnClient::Core::Request.get(url)
137
- rescue => exception
138
- SgtnClient.logger.error exception.message
139
- end
140
- if obj != nil
141
- obj = obj["data"]
142
- end
143
- return obj
91
+ private
92
+
93
+ def get_bundle(component, locale)
94
+ get_bundle!(component, locale)
95
+ rescue StandardError => e
96
+ SgtnClient.logger.error "[#{method(__callee__).owner}.#{__callee__}] failed to get a bundle. component: #{component}, locale: #{locale}"
97
+ SgtnClient.logger.error e
98
+ nil
144
99
  end
145
100
 
146
- end
101
+ 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
147
107
 
148
- end
108
+ SgtnClient::Config.loader.get_bundle(component, locale)
109
+ end
110
+ end
111
+ extend Implementation
112
+ end
113
+ end
@@ -1,4 +1,7 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
1
4
  require 'sgtn-client/cldr/localized_datetime'
2
5
  require 'sgtn-client/cldr/localized_date'
3
6
  require 'sgtn-client/cldr/localized_time'
4
- require 'sgtn-client/cldr/localized_str'
7
+ require 'sgtn-client/cldr/localized_str'
@@ -1,3 +1,6 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
1
4
  require 'date'
2
5
  require 'time'
3
6
 
@@ -24,4 +27,4 @@ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
24
27
  def l_short_s(locale = TwitterCldr.locale)
25
28
  self.to_datetime().localize(locale).to_date().to_short_s
26
29
  end
27
- LOCALIZE
30
+ LOCALIZE
@@ -1,3 +1,6 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
1
4
  require 'date'
2
5
  require 'time'
3
6
 
@@ -60,4 +63,4 @@ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
60
63
  tz.display_name_for(self, display_name)
61
64
  end
62
65
  end
63
- LOCALIZE
66
+ LOCALIZE
@@ -1,3 +1,5 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
1
3
 
2
4
  String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
3
5
  def to_plural_s(locale, arg)
@@ -8,4 +10,4 @@ String.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
8
10
  num_str
9
11
  end
10
12
  end
11
- LOCALIZE
13
+ LOCALIZE
@@ -1,3 +1,6 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
1
4
  require 'date'
2
5
  require 'time'
3
6
 
@@ -24,4 +27,4 @@ Time.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
24
27
  def l_short_s(locale = TwitterCldr.locale)
25
28
  self.localize(locale).to_short_s
26
29
  end
27
- LOCALIZE
30
+ LOCALIZE
@@ -0,0 +1,30 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
4
+ module SgtnClient
5
+ module Common
6
+ class BundleID
7
+ attr_reader :locale, :component
8
+
9
+ def initialize(component, locale)
10
+ @locale = locale
11
+ @component = component
12
+ @key = [@component, @locale].hash
13
+ end
14
+
15
+ def hash
16
+ @key
17
+ end
18
+
19
+ def ==(other)
20
+ (other.is_a? self.class) && @locale == other.locale && @component == other.component
21
+ end
22
+
23
+ alias eql? ==
24
+
25
+ def to_s
26
+ "locale=#{@locale}, component=#{@component}}"
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,34 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
4
+ module SgtnClient
5
+ class SingleOperation
6
+ def initialize(*conditions, &block)
7
+ raise 'no way to create a new obj' unless block
8
+
9
+ @lock = Mutex.new
10
+ @hash = {}
11
+
12
+ @conditions = conditions
13
+ @creator = block
14
+ end
15
+
16
+ # return new created object
17
+ def operate(id, *args)
18
+ @lock.synchronize do
19
+ obj = @hash[id]
20
+ @conditions.each do |con|
21
+ return obj unless con.call(id, obj, *args)
22
+ end
23
+ # TODO: whatif returning nil
24
+ @hash[id] = @creator.call(id, obj, *args)
25
+ end
26
+ end
27
+
28
+ def remove_object(id)
29
+ @lock.synchronize do
30
+ @hash.delete(id)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -1,99 +1,33 @@
1
+ # Copyright 2022 VMware, Inc.
2
+ # SPDX-License-Identifier: EPL-2.0
3
+
1
4
  require 'date'
2
5
 
3
6
  module SgtnClient::Core
4
7
  class Cache
5
- Entry = Struct.new(:expiry, :value)
8
+ Entry = Struct.new(:expiry, :items)
6
9
 
7
10
  def self.initialize(disabled=false, opts={})
8
11
  @@opts = opts
9
- @mutex = Mutex.new
10
- if disabled == false
11
- @@data = Hash.new
12
- SgtnClient.logger.debug "[Cache][initialize]cache is enabled!"
13
- else
14
- @@data = nil
15
- SgtnClient.logger.debug "[Cache][initialize]cache is disabled!"
16
- end
17
- end
18
-
19
- def self.keys
20
- if @@data == nil
21
- return nil
22
- end
23
- SgtnClient.logger.debug "[Cache][keys]get cache keys"
24
- @@data.keys
12
+ SgtnClient.logger.debug "[Cache][initialize] Disable cache? #{disabled}"
13
+ @@data = Hash.new
25
14
  end
26
15
 
27
16
  def self.get(key)
28
- if @@data == nil
29
- return nil, nil
30
- end
31
17
  SgtnClient.logger.debug "[Cache][get]get cache for key: " + key
32
- invalidate(key)
33
- end
34
-
35
- def self.has(key)
36
- if @@data == nil
37
- return nil
38
- end
39
- SgtnClient.logger.debug "[Cache][has]check if the cache has key: #{(@@data.has_key? key)}"
40
- @@data.has_key? key
18
+ return @@data&.dig(key)
41
19
  end
42
20
 
43
- def self.put(key, value, ttl=nil)
44
- @mutex.synchronize do
45
- if @@data == nil || value == nil
46
- return nil
47
- end
48
- ttl ||= @@opts[:ttl]
49
- # hours from new
50
- SgtnClient.logger.debug "[Cache][put]put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
51
- @@data[key] = Entry.new(Time.now + ttl*60, value)
52
- end
53
- end
54
-
55
- def self.delete(key)
56
- @mutex.synchronize do
57
- if @@data == nil
58
- return nil
59
- end
60
- SgtnClient.logger.debug "[Cache][delete]delete cache for key: " + key
61
- @@data.delete key
62
- end
21
+ def self.put(key, items, ttl=nil)
22
+ ttl ||= @@opts[:ttl]
23
+ # hours from new
24
+ SgtnClient.logger.debug "[Cache][put]put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
25
+ @@data[key] = Entry.new(Time.now + ttl*60, items)
63
26
  end
64
27
 
65
28
  def self.clear
66
- @mutex.synchronize do
67
- if @@data == nil
68
- return nil
69
- end
70
- SgtnClient.logger.debug "[Cache][clear]clear cache!"
71
- @@data = Hash.new
72
- end
73
- end
74
-
75
- def self.invalidate(key)
76
- @mutex.synchronize do
77
- if @@data == nil
78
- return nil, nil
79
- end
80
- SgtnClient.logger.debug "[Cache][invalidate]invalidate expired cache......"
81
- now = Time.now
82
- if has(key)
83
- v = @@data[key]
84
- expired = false
85
- SgtnClient.logger.debug "[Cache][invalidate]check cache: key=#{key}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
86
- if v[:expiry] < now
87
- SgtnClient.logger.debug "[Cache][invalidate]before deleting the cache: data=#{@@data}"
88
- @@data.delete(key)
89
- SgtnClient.logger.debug "[Cache][invalidate]after deleting the cache: data=#{@@data}"
90
- expired = true
91
- end
92
- return expired, v[:value]
93
- else
94
- return nil, nil
95
- end
96
- end
29
+ SgtnClient.logger.debug "[Cache][clear]clear cache!"
30
+ @@data = Hash.new
97
31
  end
98
32
  end
99
33