singleton-client-test 0.7.0.31 → 0.7.0.34
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sgtn-client/api/source.rb +10 -66
- data/lib/sgtn-client/api/t.rb +20 -18
- data/lib/sgtn-client/api/translation.rb +82 -117
- data/lib/sgtn-client/cldr/core_ext.rb +4 -1
- data/lib/sgtn-client/cldr/localized_date.rb +4 -1
- data/lib/sgtn-client/cldr/localized_datetime.rb +4 -1
- data/lib/sgtn-client/cldr/localized_str.rb +3 -1
- data/lib/sgtn-client/cldr/localized_time.rb +4 -1
- data/lib/sgtn-client/common/data.rb +30 -0
- data/lib/sgtn-client/common/single_operation.rb +34 -0
- data/lib/sgtn-client/core/cache.rb +14 -80
- data/lib/sgtn-client/core/config.rb +39 -3
- data/lib/sgtn-client/core/exceptions.rb +3 -0
- data/lib/sgtn-client/core/logging.rb +3 -1
- data/lib/sgtn-client/exceptions.rb +6 -0
- data/lib/sgtn-client/formatters/plurals/plural_formatter.rb +4 -1
- data/lib/sgtn-client/loader/cache.rb +71 -0
- data/lib/sgtn-client/loader/chain_loader.rb +49 -0
- data/lib/sgtn-client/loader/consts.rb +15 -0
- data/lib/sgtn-client/loader/loader_factory.rb +33 -0
- data/lib/sgtn-client/loader/local_translation.rb +50 -0
- data/lib/sgtn-client/loader/server.rb +94 -0
- data/lib/sgtn-client/loader/single_loader.rb +46 -0
- data/lib/sgtn-client/loader/source.rb +53 -0
- data/lib/sgtn-client/loader/source_comparer.rb +58 -0
- data/lib/sgtn-client/sgtn-client.rb +7 -1
- data/lib/sgtn-client/util/cache-util.rb +33 -44
- data/lib/sgtn-client/util/locale-util.rb +48 -31
- data/lib/sgtn-client/util/string-util.rb +12 -0
- data/lib/sgtn-client/util/validate-util.rb +4 -7
- data/lib/singleton-client.rb +15 -0
- data/lib/singleton-ruby.rb +5 -0
- data/lib/version.rb +2 -0
- metadata +43 -147
- data/lib/sgtn-client/core/request.rb +0 -21
- 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6722a7f77f8f9ee4fbf2f481d0c66e30b818c61746a9c7e8471fc1b225079f72
|
4
|
+
data.tar.gz: ba0e6acade9bc655f97a731cf760e15097b713e99000cf8f2612e4975a357d2d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
9
|
-
|
10
|
-
|
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
|
data/lib/sgtn-client/api/t.rb
CHANGED
@@ -1,22 +1,24 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
1
|
+
# Copyright 2022 VMware, Inc.
|
2
|
+
# SPDX-License-Identifier: EPL-2.0
|
2
3
|
|
3
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
52
|
-
s =
|
53
|
-
|
54
|
-
|
31
|
+
elsif args.is_a?(Array)
|
32
|
+
s = s % args
|
33
|
+
end
|
34
|
+
s
|
55
35
|
end
|
56
36
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
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
|
-
|
57
|
+
result = yield
|
58
|
+
return if result.nil?
|
59
|
+
end
|
78
60
|
|
79
|
-
|
80
|
-
|
81
|
-
if items.nil? || items["messages"] == nil
|
82
|
-
nil
|
61
|
+
if kwargs.empty?
|
62
|
+
result
|
83
63
|
else
|
84
|
-
|
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
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
-
|
107
|
-
|
80
|
+
{ 'component' => component, 'locale' => locale, 'messages' => items || {} } if items
|
81
|
+
end
|
108
82
|
|
109
|
-
def
|
110
|
-
|
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
|
121
|
-
|
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
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
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
|
-
|
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
|
-
|
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, :
|
8
|
+
Entry = Struct.new(:expiry, :items)
|
6
9
|
|
7
10
|
def self.initialize(disabled=false, opts={})
|
8
11
|
@@opts = opts
|
9
|
-
|
10
|
-
|
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
|
-
|
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,
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
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
|
-
|
67
|
-
|
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
|
|