Skip to content

Commit

Permalink
add option to skip local cache
Browse files Browse the repository at this point in the history
  • Loading branch information
philchia committed Dec 3, 2020
1 parent 0cb269f commit e02f346
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ func defaultOperation() *operation {

// Client for apollo
type client struct {
conf *Conf
conf *Conf
skipLocalCache bool

logger Logger

Expand Down Expand Up @@ -107,10 +108,12 @@ func NewClient(conf *Conf, opts ...ClientOption) Client {
// Start sync config
func (c *client) Start() error {
c.logger.Infof("start agollo client...")
// check cache dir
if err := c.autoCreateCacheDir(); err != nil {
c.logger.Errorf("fail to create cache dir: %v", err)
return err
if !c.skipLocalCache {
// check cache dir
if err := c.autoCreateCacheDir(); err != nil {
c.logger.Errorf("fail to create cache dir: %v", err)
return err
}
}

// preload all config to local first
Expand Down Expand Up @@ -156,6 +159,9 @@ func (c *client) OnUpdate(handler func(*ChangeEvent)) {
// fetchAllCinfig fetch from remote, if failed load from local file
func (c *client) preload() error {
if err := c.longPoller.preload(); err != nil {
if c.skipLocalCache {
return err
}
return c.loadLocal(c.getDumpFileName())
}
return nil
Expand All @@ -168,6 +174,9 @@ func (c *client) loadLocal(name string) error {

// dump caches to file
func (c *client) dump(name string) error {
if c.skipLocalCache {
return nil
}
c.logger.Infof("dump config to local file:%s", name)
return c.caches.dump(name)
}
Expand Down Expand Up @@ -228,6 +237,7 @@ func (c *client) sync(namespace string) (*ChangeEvent, error) {
if err != nil || len(bts) == 0 {
return nil, err
}

var result result
if err := json.Unmarshal(bts, &result); err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ func WithLogger(logger Logger) ClientOption {
c.logger = logger
}
}

func SkipLocalCache() ClientOption {
return func(c *client) {
c.skipLocalCache = true
}
}

0 comments on commit e02f346

Please sign in to comment.