From b5d67ff8e3bea32027bbc4d3d6d58b169bcb2915 Mon Sep 17 00:00:00 2001 From: mushroomsir Date: Thu, 19 Nov 2020 17:29:44 +0800 Subject: [PATCH] Improve user cache label expired --- src/bll/user.go | 12 +++++++++--- src/conf/config.go | 35 +++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 17 deletions(-) diff --git a/src/bll/user.go b/src/bll/user.go index dc488de..cf20f6b 100644 --- a/src/bll/user.go +++ b/src/bll/user.go @@ -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) diff --git a/src/conf/config.go b/src/conf/config.go index 14924cf..4761120 100644 --- a/src/conf/config.go +++ b/src/conf/config.go @@ -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 的配置。 @@ -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 } @@ -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