本文整理匯總了Golang中github.com/hoisie/redis.Client.Sadd方法的典型用法代碼示例。如果您正苦於以下問題:Golang Client.Sadd方法的具體用法?Golang Client.Sadd怎麽用?Golang Client.Sadd使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hoisie/redis.Client
的用法示例。
在下文中一共展示了Client.Sadd方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: addRemoveGroups
func addRemoveGroups(client redis.Client, articleID int, toAdd, toRemove []string) {
article := "article:" + strconv.Itoa(articleID)
for _, group := range toAdd {
if _, err := client.Sadd("group:"+group, []byte(article)); err != nil {
log.Fatal("Sadd エラーだよ", err.Error())
}
}
for _, group := range toRemove {
if _, err := client.Srem("group:"+group, []byte(article)); err != nil {
log.Fatal("Srem エラーだよ", err.Error())
}
}
}
示例2: postArticle
func postArticle(client redis.Client, user, title, link string) string {
id, err := client.Incr("article:")
if err != nil {
log.Fatal("Incrエラーだよ", err.Error())
}
articleID := strconv.FormatInt(id, 20)
voted := "voted:" + articleID
if _, err := client.Sadd(voted, []byte(user)); err != nil {
log.Fatal("Saddエラーだよ", err.Error())
}
if _, err := client.Expire(voted, int64(ONE_WEEK_DAY*time.Second)); err != nil {
log.Fatal("Expireエラーだよ", err.Error())
}
now := time.Now()
article := "article:" + articleID
fmt.Println("articleID", articleID)
if err := client.Hmset(article, map[string]string{
"title": title,
"link": link,
"poster": user,
"time": now.String(),
"votes": "1",
}); err != nil {
log.Fatal("Hmsetエラーだよ", err.Error())
}
if _, err := client.Zadd("score:", []byte(article), float64(now.Unix()+VOTE_SCORE)); err != nil {
log.Fatal("Zaddエラーだよ", err.Error())
}
if _, err := client.Zadd("time:", []byte(article), float64(now.Unix())); err != nil {
log.Fatal("Zaddエラーだよ", err.Error())
}
return articleID
}
示例3: articleVote
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())
}
}
}