singleton-client 0.7.2 → 0.7.3
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 +4 -4
- data/lib/sgtn-client/api/source.rb +4 -4
- data/lib/sgtn-client/api/translation.rb +40 -29
- data/lib/sgtn-client/core/cache.rb +35 -26
- data/lib/sgtn-client/core/config.rb +1 -1
- data/lib/sgtn-client/util/cache-util.rb +12 -3
- metadata +2 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 3fa11214d62c5669477f86cf9cdd5535112c9fa48ec46504e192048e008173cc
         | 
| 4 | 
            +
              data.tar.gz: 29675027bf9d235c59728680dd791a11665209c4e99445032410c38166db6463
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: f62c62c7e7d0e49124e8cdbd648154099e8d3b6bf5a8ac295b25817acefaf71a89dd09d6a8e1ef4f766f9e42d2ffb398af5e92f832933e650fef556aab767d53
         | 
| 7 | 
            +
              data.tar.gz: 0d97b5eb531a64e9be1543edf3bebcc78ebc094fa06db9f711a1bb1e365ec42c4cfc0a96ffa5130f220a1c39f38ab57f10fb3120fea266ad698607557069143a
         | 
| @@ -7,7 +7,7 @@ module SgtnClient | |
| 7 7 |  | 
| 8 8 | 
             
                  def self.getSource(component, key, locale)
         | 
| 9 9 | 
             
                    cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
         | 
| 10 | 
            -
                    items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 10 | 
            +
                    expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 11 11 | 
             
                    if items.nil?
         | 
| 12 12 | 
             
                      items = getBundle(component, locale)
         | 
| 13 13 | 
             
                      SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
         | 
| @@ -15,7 +15,7 @@ module SgtnClient | |
| 15 15 | 
             
                    else
         | 
| 16 16 | 
             
                      SgtnClient.logger.debug "Getting sources from cache with key: " + cache_key
         | 
| 17 17 | 
             
                    end
         | 
| 18 | 
            -
                    s = items[locale][key]
         | 
| 18 | 
            +
                    s = items.nil?? nil : items[locale][key]
         | 
| 19 19 | 
             
                    if items.nil? || s.nil?
         | 
| 20 20 | 
             
                      SgtnClient.logger.debug "Source not found, return key: " + key
         | 
| 21 21 | 
             
                      #return key
         | 
| @@ -27,8 +27,8 @@ module SgtnClient | |
| 27 27 |  | 
| 28 28 | 
             
                  def self.getSources(component, locale)
         | 
| 29 29 | 
             
                    cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
         | 
| 30 | 
            -
                    items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 31 | 
            -
                    if items.nil?
         | 
| 30 | 
            +
                    expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 31 | 
            +
                    if items.nil? || expired
         | 
| 32 32 | 
             
                      items = getBundle(component, locale)
         | 
| 33 33 | 
             
                      SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
         | 
| 34 34 | 
             
                      SgtnClient::CacheUtil.write_cache(cache_key, items)
         | 
| @@ -15,6 +15,9 @@ module SgtnClient | |
| 15 15 | 
             
                    str = getTranslation(component, key, locale)
         | 
| 16 16 | 
             
                    if str.nil?
         | 
| 17 17 | 
             
                      str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
         | 
| 18 | 
            +
                      if str.nil?
         | 
| 19 | 
            +
                        SgtnClient.logger.error "Can't find the key '" + key + "' in source path!"
         | 
| 20 | 
            +
                      end
         | 
| 18 21 | 
             
                    end
         | 
| 19 22 | 
             
                    str
         | 
| 20 23 | 
             
                  end
         | 
| @@ -23,6 +26,10 @@ module SgtnClient | |
| 23 26 | 
             
                    str = getTranslation(component, key, locale)
         | 
| 24 27 | 
             
                    if str.nil?
         | 
| 25 28 | 
             
                      str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
         | 
| 29 | 
            +
                      if str.nil?
         | 
| 30 | 
            +
                        SgtnClient.logger.error "Can't find the key '" + key + "' in source path!"
         | 
| 31 | 
            +
                        return nil
         | 
| 32 | 
            +
                      end
         | 
| 26 33 | 
             
                      str.to_plural_s(:en, plural_args)
         | 
| 27 34 | 
             
                    else
         | 
| 28 35 | 
             
                      str.to_plural_s(locale, plural_args)
         | 
| @@ -31,6 +38,9 @@ module SgtnClient | |
| 31 38 |  | 
| 32 39 | 
             
                  def self.getString_f(component, key, args, locale, *optionals)
         | 
| 33 40 | 
             
                     s = getString(component, key, locale, *optionals)
         | 
| 41 | 
            +
                     if s.nil?
         | 
| 42 | 
            +
                      return nil
         | 
| 43 | 
            +
                     end
         | 
| 34 44 | 
             
                     if args.is_a?(Hash)
         | 
| 35 45 | 
             
                      args.each do |source, arg|
         | 
| 36 46 | 
             
                        s.gsub! "{#{source}}", arg
         | 
| @@ -42,24 +52,19 @@ module SgtnClient | |
| 42 52 | 
             
                  end
         | 
| 43 53 |  | 
| 44 54 | 
             
                  def self.getStrings(component, locale)
         | 
| 45 | 
            -
                     | 
| 46 | 
            -
                    cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
         | 
| 47 | 
            -
                    items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 48 | 
            -
                    if items.nil?
         | 
| 49 | 
            -
                      items = getTranslations(component, flocale)
         | 
| 50 | 
            -
                      SgtnClient::CacheUtil.write_cache(cache_key, items)
         | 
| 51 | 
            -
                    else
         | 
| 52 | 
            -
                      SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
         | 
| 53 | 
            -
                    end
         | 
| 54 | 
            -
             | 
| 55 | 
            +
                    items = get_cs(component, locale)
         | 
| 55 56 | 
             
                    default = SgtnClient::Config.configurations.default
         | 
| 56 57 | 
             
                    if items.nil? || items["messages"] == nil
         | 
| 57 58 | 
             
                      items = {}
         | 
| 58 59 | 
             
                      s = SgtnClient::Source.getSources(component, default)
         | 
| 59 | 
            -
                       | 
| 60 | 
            -
             | 
| 61 | 
            -
                       | 
| 62 | 
            -
             | 
| 60 | 
            +
                      if s.nil?
         | 
| 61 | 
            +
                        SgtnClient.logger.error "Can't find the component '" + component + "' in source path!"
         | 
| 62 | 
            +
                      else
         | 
| 63 | 
            +
                        default_component, value = s.first
         | 
| 64 | 
            +
                        items["component"] = component
         | 
| 65 | 
            +
                        items["messages"] = value
         | 
| 66 | 
            +
                        items["locale"] = 'source'
         | 
| 67 | 
            +
                      end
         | 
| 63 68 | 
             
                    end
         | 
| 64 69 | 
             
                    return items
         | 
| 65 70 | 
             
                   end
         | 
| @@ -68,11 +73,20 @@ module SgtnClient | |
| 68 73 | 
             
                  private
         | 
| 69 74 |  | 
| 70 75 | 
             
                  def self.getTranslation(component, key, locale)
         | 
| 76 | 
            +
                    items = get_cs(component, locale)
         | 
| 77 | 
            +
                    if items.nil? || items["messages"] == nil
         | 
| 78 | 
            +
                      nil
         | 
| 79 | 
            +
                    else
         | 
| 80 | 
            +
                      items["messages"][key]
         | 
| 81 | 
            +
                    end
         | 
| 82 | 
            +
                  end
         | 
| 83 | 
            +
             | 
| 84 | 
            +
                  def self.get_cs(component, locale)
         | 
| 71 85 | 
             
                    flocale = SgtnClient::LocaleUtil.fallback(locale)
         | 
| 72 86 | 
             
                    cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
         | 
| 73 | 
            -
                    items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 74 | 
            -
                    if items.nil?
         | 
| 75 | 
            -
                      items =  | 
| 87 | 
            +
                    expired, items = SgtnClient::CacheUtil.get_cache(cache_key)
         | 
| 88 | 
            +
                    if items.nil? || expired
         | 
| 89 | 
            +
                      items = load(component, flocale)
         | 
| 76 90 | 
             
                      if items.nil?
         | 
| 77 91 | 
             
                        items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
         | 
| 78 92 | 
             
                        SgtnClient::Core::Cache.put(cache_key, items, 60)
         | 
| @@ -82,24 +96,21 @@ module SgtnClient | |
| 82 96 | 
             
                    else
         | 
| 83 97 | 
             
                      SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
         | 
| 84 98 | 
             
                    end
         | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
                  end
         | 
| 91 | 
            -
                  
         | 
| 92 | 
            -
                  def self.getTranslations(component, locale)
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                    return items
         | 
| 101 | 
            +
                   end
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                  def self.load(component, locale)
         | 
| 93 104 | 
             
                    env = SgtnClient::Config.default_environment
         | 
| 94 105 | 
             
                    mode = SgtnClient::Config.configurations[env]["bundle_mode"]
         | 
| 95 106 | 
             
                    if mode == 'offline'
         | 
| 96 | 
            -
                      return  | 
| 107 | 
            +
                      return load_o(component, locale)
         | 
| 97 108 | 
             
                    else
         | 
| 98 | 
            -
                      return  | 
| 109 | 
            +
                      return load_s(component, locale)
         | 
| 99 110 | 
             
                    end
         | 
| 100 111 | 
             
                  end
         | 
| 101 112 |  | 
| 102 | 
            -
                  def self. | 
| 113 | 
            +
                  def self.load_o(component, locale)
         | 
| 103 114 | 
             
                    env = SgtnClient::Config.default_environment
         | 
| 104 115 | 
             
                    product_name = SgtnClient::Config.configurations[env]["product_name"]
         | 
| 105 116 | 
             
                    version = SgtnClient::Config.configurations[env]["version"].to_s
         | 
| @@ -109,7 +120,7 @@ module SgtnClient | |
| 109 120 | 
             
                    SgtnClient::FileUtil.read_json(bundlepath)
         | 
| 110 121 | 
             
                  end
         | 
| 111 122 |  | 
| 112 | 
            -
                  def self. | 
| 123 | 
            +
                  def self.load_s(component, locale)
         | 
| 113 124 | 
             
                    env = SgtnClient::Config.default_environment
         | 
| 114 125 | 
             
                    product_name = SgtnClient::Config.configurations[env]["product_name"]
         | 
| 115 126 | 
             
                    vip_server = SgtnClient::Config.configurations[env]["vip_server"]
         | 
| @@ -5,86 +5,95 @@ module SgtnClient::Core | |
| 5 5 | 
             
                        Entry = Struct.new(:expiry, :value)
         | 
| 6 6 |  | 
| 7 7 | 
             
                        def self.initialize(disabled=false, opts={})
         | 
| 8 | 
            -
                             | 
| 8 | 
            +
                            @@opts = opts
         | 
| 9 9 | 
             
                            @mutex = Mutex.new
         | 
| 10 10 | 
             
                            SgtnClient.logger.debug "Initialize cache......"
         | 
| 11 11 | 
             
                            if disabled == false
         | 
| 12 | 
            -
                                 | 
| 12 | 
            +
                                @@data = Hash.new
         | 
| 13 13 | 
             
                                SgtnClient.logger.debug "Cache is enabled!"
         | 
| 14 14 | 
             
                            else
         | 
| 15 | 
            +
                                @@data = nil
         | 
| 15 16 | 
             
                                SgtnClient.logger.debug "Cache is disabled!"
         | 
| 16 17 | 
             
                            end
         | 
| 17 18 | 
             
                        end
         | 
| 18 19 |  | 
| 19 20 | 
             
                        def self.keys
         | 
| 20 | 
            -
                            if  | 
| 21 | 
            +
                            if @@data == nil
         | 
| 21 22 | 
             
                                return nil
         | 
| 22 23 | 
             
                            end
         | 
| 23 24 | 
             
                            SgtnClient.logger.debug "Get cache keys"
         | 
| 24 | 
            -
                             | 
| 25 | 
            +
                            @@data.keys
         | 
| 25 26 | 
             
                        end
         | 
| 26 27 |  | 
| 27 28 | 
             
                        def self.get(key)
         | 
| 28 | 
            -
                            if  | 
| 29 | 
            -
                                return nil
         | 
| 29 | 
            +
                            if @@data == nil
         | 
| 30 | 
            +
                                return nil, nil
         | 
| 30 31 | 
             
                            end
         | 
| 31 32 | 
             
                            SgtnClient.logger.debug "Get cache for key: " + key
         | 
| 32 | 
            -
                            invalidate
         | 
| 33 | 
            -
                            $data[key][:value] if has(key)
         | 
| 33 | 
            +
                            invalidate(key)
         | 
| 34 34 | 
             
                        end
         | 
| 35 35 |  | 
| 36 36 | 
             
                        def self.has(key)
         | 
| 37 | 
            -
                            if  | 
| 37 | 
            +
                            if @@data == nil
         | 
| 38 38 | 
             
                                return nil
         | 
| 39 39 | 
             
                            end
         | 
| 40 | 
            -
                            SgtnClient.logger.debug " | 
| 41 | 
            -
                             | 
| 40 | 
            +
                            SgtnClient.logger.debug "Check if the cache has key: #{(@@data.has_key? key)}"
         | 
| 41 | 
            +
                            @@data.has_key? key
         | 
| 42 42 | 
             
                        end
         | 
| 43 43 |  | 
| 44 44 | 
             
                        def self.put(key, value, ttl=nil)
         | 
| 45 45 | 
             
                            @mutex.synchronize do
         | 
| 46 | 
            -
                                if  | 
| 46 | 
            +
                                if @@data == nil || value == nil
         | 
| 47 47 | 
             
                                    return nil
         | 
| 48 48 | 
             
                                end
         | 
| 49 | 
            -
                                ttl ||=  | 
| 49 | 
            +
                                ttl ||= @@opts[:ttl]
         | 
| 50 50 | 
             
                                # hours from new
         | 
| 51 51 | 
             
                                SgtnClient.logger.debug "Put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
         | 
| 52 | 
            -
                                 | 
| 52 | 
            +
                                @@data[key] = Entry.new(Time.now + ttl*60, value)
         | 
| 53 53 | 
             
                            end
         | 
| 54 54 | 
             
                        end
         | 
| 55 55 |  | 
| 56 56 | 
             
                        def self.delete(key)
         | 
| 57 57 | 
             
                            @mutex.synchronize do
         | 
| 58 | 
            -
                                if  | 
| 58 | 
            +
                                if @@data == nil
         | 
| 59 59 | 
             
                                    return nil
         | 
| 60 60 | 
             
                                end
         | 
| 61 61 | 
             
                                SgtnClient.logger.debug "Delete cache for key: " + key
         | 
| 62 | 
            -
                                 | 
| 62 | 
            +
                                @@data.delete key
         | 
| 63 63 | 
             
                            end
         | 
| 64 64 | 
             
                        end
         | 
| 65 65 |  | 
| 66 66 | 
             
                        def self.clear
         | 
| 67 67 | 
             
                            @mutex.synchronize do
         | 
| 68 | 
            -
                                if  | 
| 68 | 
            +
                                if @@data == nil
         | 
| 69 69 | 
             
                                    return nil
         | 
| 70 70 | 
             
                                end
         | 
| 71 71 | 
             
                                SgtnClient.logger.debug "Clear cache!"
         | 
| 72 | 
            -
                                 | 
| 72 | 
            +
                                @@data = Hash.new
         | 
| 73 73 | 
             
                            end
         | 
| 74 74 | 
             
                        end
         | 
| 75 75 |  | 
| 76 | 
            -
                        def self.invalidate
         | 
| 76 | 
            +
                        def self.invalidate(key)
         | 
| 77 77 | 
             
                            @mutex.synchronize do
         | 
| 78 | 
            -
                                if  | 
| 79 | 
            -
                                    return nil
         | 
| 78 | 
            +
                                if @@data == nil
         | 
| 79 | 
            +
                                    return nil, nil
         | 
| 80 80 | 
             
                                end
         | 
| 81 81 | 
             
                                SgtnClient.logger.debug "Invalidating expired cache......"
         | 
| 82 82 | 
             
                                now = Time.now
         | 
| 83 | 
            -
                                 | 
| 84 | 
            -
                                     | 
| 85 | 
            -
             | 
| 86 | 
            -
                                    }
         | 
| 87 | 
            -
             | 
| 83 | 
            +
                                if has(key)
         | 
| 84 | 
            +
                                    v = @@data[key]
         | 
| 85 | 
            +
                                    expired = false
         | 
| 86 | 
            +
                                    SgtnClient.logger.debug "Checking cache: key=#{key}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
         | 
| 87 | 
            +
                                    if v[:expiry] < now
         | 
| 88 | 
            +
                                        SgtnClient.logger.debug "Before deleting the cache: data=#{@@data}"
         | 
| 89 | 
            +
                                        @@data.delete(key)
         | 
| 90 | 
            +
                                        SgtnClient.logger.debug "After deleting the cache: data=#{@@data}"
         | 
| 91 | 
            +
                                        expired = true
         | 
| 92 | 
            +
                                    end
         | 
| 93 | 
            +
                                    return expired, v[:value]
         | 
| 94 | 
            +
                                else
         | 
| 95 | 
            +
                                    return nil, nil
         | 
| 96 | 
            +
                                end
         | 
| 88 97 | 
             
                            end
         | 
| 89 98 | 
             
                        end
         | 
| 90 99 | 
             
                    end
         | 
| @@ -40,7 +40,7 @@ module SgtnClient | |
| 40 40 | 
             
                :rest_endpoint, :rest_token_endpoint, :client_id, :client_secret,
         | 
| 41 41 | 
             
                :openid_endpoint, :openid_redirect_uri, :openid_client_id, :openid_client_secret,
         | 
| 42 42 | 
             
                :verbose_logging, :product_name, :version, :vip_server, :bundle_mode,
         | 
| 43 | 
            -
                :translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache
         | 
| 43 | 
            +
                :translation_bundle, :source_bundle, :cache_expiry_period, :disable_cache, :default_language
         | 
| 44 44 |  | 
| 45 45 |  | 
| 46 46 | 
             
                # Create Config object
         | 
| @@ -10,8 +10,8 @@ module SgtnClient | |
| 10 10 | 
             
              class CacheUtil
         | 
| 11 11 |  | 
| 12 12 | 
             
                  def self.get_cache(cache_key)
         | 
| 13 | 
            -
                    items = SgtnClient::Core::Cache.get(cache_key)
         | 
| 14 | 
            -
                    return items
         | 
| 13 | 
            +
                    expired, items = SgtnClient::Core::Cache.get(cache_key)
         | 
| 14 | 
            +
                    return expired, items
         | 
| 15 15 | 
             
                  end
         | 
| 16 16 |  | 
| 17 17 | 
             
                  def self.clear_cache()
         | 
| @@ -19,6 +19,10 @@ module SgtnClient | |
| 19 19 | 
             
                  end
         | 
| 20 20 |  | 
| 21 21 | 
             
                  def self.write_cache(cache_key, items)
         | 
| 22 | 
            +
                    if items.nil?
         | 
| 23 | 
            +
                      SgtnClient.logger.debug "write cache with key #{cache_key}, itmes #{items}"
         | 
| 24 | 
            +
                      return nil
         | 
| 25 | 
            +
                    end
         | 
| 22 26 | 
             
                    env = SgtnClient::Config.default_environment
         | 
| 23 27 | 
             
                    cache_expiry_period = SgtnClient::Config.configurations[env]["cache_expiry_period"]
         | 
| 24 28 | 
             
                    # expired after 24 hours
         | 
| @@ -32,7 +36,12 @@ module SgtnClient | |
| 32 36 | 
             
                    env = SgtnClient::Config.default_environment
         | 
| 33 37 | 
             
                    product_name = SgtnClient::Config.configurations[env]["product_name"]
         | 
| 34 38 | 
             
                    version = SgtnClient::Config.configurations[env]["version"].to_s
         | 
| 35 | 
            -
                     | 
| 39 | 
            +
                    default_l = SgtnClient::Config.configurations[env]["default_language"]
         | 
| 40 | 
            +
                    if default_l == nil
         | 
| 41 | 
            +
                      default_l = 'en'
         | 
| 42 | 
            +
                    end
         | 
| 43 | 
            +
                    lc = locale == default_l ? SgtnClient::Config.configurations.default: locale
         | 
| 44 | 
            +
                    return product_name + "_" + version + "_" + component + "_" + lc
         | 
| 36 45 | 
             
                  end
         | 
| 37 46 | 
             
              end
         | 
| 38 47 |  | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: singleton-client
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.7. | 
| 4 | 
            +
              version: 0.7.3
         | 
| 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-01- | 
| 11 | 
            +
            date: 2022-01-28 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: rest-client
         |