Skip to content

Commit

Permalink
Merge pull request #14 from teambition/feature/cacheExpire
Browse files Browse the repository at this point in the history
Improve user cache label expired
  • Loading branch information
zensh authored Nov 19, 2020
2 parents 6eeb41d + b5d67ff commit 7fdf905
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
12 changes: 9 additions & 3 deletions src/bll/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,15 @@ func (b *User) ListCachedLabels(ctx context.Context, uid, product string) *tpl.C
return res
}
} else if conf.Config.IsCacheLabelExpired(now.Unix()-5, activeAt) { // 提前 5s 异步处理
util.Go(10*time.Second, func(gctx context.Context) {
b.ms.TryApplyLabelRulesAndRefreshUserLabels(gctx, productID, product, user.ID, now, false)
})
if conf.Config.IsCacheLabelDoubleExpired(now.Unix(), activeAt) { // 大于等于 2 倍过期时间的缓存,同步等待结果。
if user = b.ms.TryApplyLabelRulesAndRefreshUserLabels(ctx, productID, product, user.ID, now, false); user == nil {
return res
}
} else {
util.Go(10*time.Second, func(gctx context.Context) {
b.ms.TryApplyLabelRulesAndRefreshUserLabels(gctx, productID, product, user.ID, now, false)
})
}
}
userCache := user.GetCache(product)

Expand Down
35 changes: 21 additions & 14 deletions src/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ type OpenTrust struct {

// ConfigTpl ...
type ConfigTpl struct {
GlobalCtx context.Context
SrvAddr string `json:"addr" yaml:"addr"`
CertFile string `json:"cert_file" yaml:"cert_file"`
KeyFile string `json:"key_file" yaml:"key_file"`
Logger Logger `json:"logger" yaml:"logger"`
MySQL SQL `json:"mysql" yaml:"mysql"`
MySQLRd SQL `json:"mysql_read" yaml:"mysql_read"`
CacheLabelExpire string `json:"cache_label_expire" yaml:"cache_label_expire"`
Channels []string `json:"channels" yaml:"channels"`
Clients []string `json:"clients" yaml:"clients"`
HIDKey string `json:"hid_key" yaml:"hid_key"`
AuthKeys []string `json:"auth_keys" yaml:"auth_keys"`
OpenTrust OpenTrust `json:"open_trust" yaml:"open_trust"`
cacheLabelExpire int64 // seconds, default to 60 seconds
GlobalCtx context.Context
SrvAddr string `json:"addr" yaml:"addr"`
CertFile string `json:"cert_file" yaml:"cert_file"`
KeyFile string `json:"key_file" yaml:"key_file"`
Logger Logger `json:"logger" yaml:"logger"`
MySQL SQL `json:"mysql" yaml:"mysql"`
MySQLRd SQL `json:"mysql_read" yaml:"mysql_read"`
CacheLabelExpire string `json:"cache_label_expire" yaml:"cache_label_expire"`
Channels []string `json:"channels" yaml:"channels"`
Clients []string `json:"clients" yaml:"clients"`
HIDKey string `json:"hid_key" yaml:"hid_key"`
AuthKeys []string `json:"auth_keys" yaml:"auth_keys"`
OpenTrust OpenTrust `json:"open_trust" yaml:"open_trust"`
cacheLabelExpire int64 // seconds, default to 60 seconds
cacheLabelDoubleExpire int64 // cacheLabelDoubleExpire * 2
}

// Validate 用于完成基本的配置验证和初始化工作。业务相关的配置验证建议放到相关代码中实现,如 mysql 的配置。
Expand All @@ -70,6 +71,7 @@ func (c *ConfigTpl) Validate() error {
du = time.Minute
}
c.cacheLabelExpire = int64(du / time.Second)
c.cacheLabelDoubleExpire = 2 * c.cacheLabelExpire
return nil
}

Expand All @@ -78,5 +80,10 @@ func (c *ConfigTpl) IsCacheLabelExpired(now, activeAt int64) bool {
return now-activeAt > c.cacheLabelExpire
}

// IsCacheLabelDoubleExpired 判断用户缓存的 labels 是否超过缓存时间的 2 倍
func (c *ConfigTpl) IsCacheLabelDoubleExpired(now, activeAt int64) bool {
return now-activeAt > c.cacheLabelDoubleExpire
}

// Config ...
var Config ConfigTpl

0 comments on commit 7fdf905

Please sign in to comment.