예제 #1
0
func articleVote(client redis.Client, user, article string) {

	cutoff := time.Now().Add(ONE_WEEK_DAY * time.Second)

	// 記事チェック
	if hit, err := client.Exists("time:"); err != nil {
		log.Fatal("Existsエラーだよ", err.Error())
	} else if !hit {
		log.Fatal("記事がないよ ")
	}

	// 期限チェック
	if score, err := client.Zscore("time:", []byte(article)); err != nil {
		log.Fatal("Zscoreエラーだよ", err.Error())
	} else {
		if cutoff.After(time.Unix(int64(score), 0)) {
			fmt.Println("Afterだった ", cutoff.Unix(), " ", int64(score))
			return
		}
	}

	articleID := strings.TrimPrefix(article, "article:")
	fmt.Println("articleID", articleID)

	ok, err := client.Sadd("voted:"+articleID, []byte(user))
	if err != nil {
		log.Fatal("Saddエラーだよ", err.Error())
	}

	if ok {
		if _, err := client.Zincrby("score:", []byte(article), VOTE_SCORE); err != nil {
			log.Fatal("Zincrbyエラーだよ", err.Error())
		}
		if _, err := client.Hincrby(article, "votes", 1); err != nil {
			log.Fatal("Hincrbyエラーだよ", err.Error())
		}
	}
}