go time

// 每隔一段时间同步文章统计信息、推荐文章信息、评论统计信息的变动到mysql
func initArticleActionSync() {
	ticker := time.NewTicker(time.Duration(config.GlobalConfig.Sync.ArticleAction) * time.Second)
	recommend := time.NewTicker(time.Duration(config.GlobalConfig.Sync.UpdateRecommendList) * time.Second)
	recommendScore := time.NewTicker(time.Duration(config.GlobalConfig.Sync.UpdateRecommendScore) * time.Second)
	comment := time.NewTicker(time.Duration(config.GlobalConfig.Sync.ArticleCommentList) * time.Second)
	calculateHot := time.NewTicker(time.Duration(config.GlobalConfig.Sync.CalculateHot) * time.Second)
	tag := time.NewTicker(time.Duration(config.GlobalConfig.Sync.ArticleCommentList) * time.Second)
	for {
		select {
		case <-ticker.C:
			key := fmt.Sprintf("a:s:%d", time.Now().Minute()/(config.GlobalConfig.Sync.ArticleAction/60))
			// 不需要解锁
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.ArticleAction-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync article action this time", key)
			}
			_ = store.ArticleRedis.SyncArticleActionChanged()
		case <-recommend.C:
			key := fmt.Sprintf("a:r:%d", time.Now().Minute()/(config.GlobalConfig.Sync.UpdateRecommendList/60))
			// 不需要解锁
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.UpdateRecommendList-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync article action this time", key)
			}
			err := service.UpdateRecommendList()
			if err != nil {
				logrus.Infof("update  recommend list err : %v", err)
			}
		case <-comment.C:
			key := fmt.Sprintf("c:s:%d", time.Now().Minute()/(config.GlobalConfig.Sync.ArticleCommentList/60))
			// 不需要解锁
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.ArticleCommentList-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync comment action this time", key)
			}
			err := store.CommentRedis.SyncArticleCommentActionChanged()
			if err != nil {
				logrus.Infof("syncArticleCommentActionChanged err : %v", err)
			}
		case <-calculateHot.C:
			key := fmt.Sprintf("c:c:%d", time.Now().Minute()/(config.GlobalConfig.Sync.CalculateHot/60))
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.CalculateHot-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync comment action this time", key)
			}
			err := service.CalculateHot()
			if err != nil {
				logrus.Infof("CalculateHot err : %v", err)
			}
		case <-tag.C:
			key := fmt.Sprintf("t:s:%d", time.Now().Minute()/(config.GlobalConfig.Sync.ArticleTag/60))
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.ArticleTag-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync comment action this time", key)
			}
			err := store.ArticleRedis.SyncTag()
			if err != nil {
				logrus.Infof("tag err : %v", err)
			}
		case <-recommendScore.C:
			key := fmt.Sprintf("r:e:%d", time.Now().Minute()/(config.GlobalConfig.Sync.UpdateRecommendScore/60))
			if _, err := store.RedisClient.Lock(key, time.Duration(config.GlobalConfig.Sync.UpdateRecommendScore-5)*time.Second); err != nil {
				logrus.Infof("get lock key %s fail, this node don't sync update recommend score this time", key)
			}
			err := service.UpdateRecommendScore()
			if err != nil {
				logrus.Infof("tag err : %v", err)
			}
		}
	}
}

https://godoc.org/time

 

time.Duration

store.RedisClient.Set(backupKey, time.Now().String(), time.Duration(config.GlobalConfig.UseBackupSMSMinutes)*time.Minute)

文档:(https://studygolang.com/pkgdoc

type Duration

type Duration int64

Duration类型代表两个时间点之间经过的时间,以纳秒为单位。可表示的最长时间段大约290年。

 

你可能感兴趣的:(go)