Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 如果获取不到config,尝试远端同步获取一次,获取到的内容需要更新缓存 #301

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func init() {

var syncApolloConfig = remote.CreateSyncApolloConfig()

//Client apollo 客户端接口
// Client apollo 客户端接口
type Client interface {
GetConfig(namespace string) *storage.Config
GetConfigAndInit(namespace string) *storage.Config
Expand Down Expand Up @@ -147,12 +147,12 @@ func StartWithConfig(loadAppConfig func() (*config.AppConfig, error)) (Client, e
return c, nil
}

//GetConfig 根据namespace获取apollo配置
// GetConfig 根据namespace获取apollo配置
func (c *internalClient) GetConfig(namespace string) *storage.Config {
return c.GetConfigAndInit(namespace)
}

//GetConfigAndInit 根据namespace获取apollo配置
// GetConfigAndInit 根据namespace获取apollo配置
func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {
if namespace == "" {
return nil
Expand All @@ -165,15 +165,18 @@ func (c *internalClient) GetConfigAndInit(namespace string) *storage.Config {
storage.CreateNamespaceConfig(namespace)

//sync config
syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig)
apolloConfig := syncApolloConfig.SyncWithNamespace(namespace, c.getAppConfig)
if apolloConfig != nil {
c.cache.UpdateApolloConfig(apolloConfig, c.getAppConfig)
}
}

config = c.cache.GetConfig(namespace)

return config
}

//GetConfigCache 根据namespace获取apollo配置的缓存
// GetConfigCache 根据namespace获取apollo配置的缓存
func (c *internalClient) GetConfigCache(namespace string) agcache.CacheInterface {
config := c.GetConfigAndInit(namespace)
if config == nil {
Expand All @@ -183,7 +186,7 @@ func (c *internalClient) GetConfigCache(namespace string) agcache.CacheInterface
return config.GetCache()
}

//GetDefaultConfigCache 获取默认缓存
// GetDefaultConfigCache 获取默认缓存
func (c *internalClient) GetDefaultConfigCache() agcache.CacheInterface {
config := c.GetConfigAndInit(storage.GetDefaultNamespace())
if config != nil {
Expand All @@ -192,42 +195,42 @@ func (c *internalClient) GetDefaultConfigCache() agcache.CacheInterface {
return nil
}

//GetApolloConfigCache 获取默认namespace的apollo配置
// GetApolloConfigCache 获取默认namespace的apollo配置
func (c *internalClient) GetApolloConfigCache() agcache.CacheInterface {
return c.GetDefaultConfigCache()
}

//GetValue 获取配置
// GetValue 获取配置
func (c *internalClient) GetValue(key string) string {
return c.GetConfig(storage.GetDefaultNamespace()).GetValue(key)
}

//GetStringValue 获取string配置值
// GetStringValue 获取string配置值
func (c *internalClient) GetStringValue(key string, defaultValue string) string {
return c.GetConfig(storage.GetDefaultNamespace()).GetStringValue(key, defaultValue)
}

//GetIntValue 获取int配置值
// GetIntValue 获取int配置值
func (c *internalClient) GetIntValue(key string, defaultValue int) int {
return c.GetConfig(storage.GetDefaultNamespace()).GetIntValue(key, defaultValue)
}

//GetFloatValue 获取float配置值
// GetFloatValue 获取float配置值
func (c *internalClient) GetFloatValue(key string, defaultValue float64) float64 {
return c.GetConfig(storage.GetDefaultNamespace()).GetFloatValue(key, defaultValue)
}

//GetBoolValue 获取bool 配置值
// GetBoolValue 获取bool 配置值
func (c *internalClient) GetBoolValue(key string, defaultValue bool) bool {
return c.GetConfig(storage.GetDefaultNamespace()).GetBoolValue(key, defaultValue)
}

//GetStringSliceValue 获取[]string 配置值
// GetStringSliceValue 获取[]string 配置值
func (c *internalClient) GetStringSliceValue(key string, defaultValue []string) []string {
return c.GetConfig(storage.GetDefaultNamespace()).GetStringSliceValue(key, separator, defaultValue)
}

//GetIntSliceValue 获取[]int 配置值
// GetIntSliceValue 获取[]int 配置值
func (c *internalClient) GetIntSliceValue(key string, defaultValue []int) []int {
return c.GetConfig(storage.GetDefaultNamespace()).GetIntSliceValue(key, separator, defaultValue)
}
Expand Down
Loading