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
}
示例#2
0
文件: redis2.go 项目: aimeiyan/gocode
func main() {
	client := redis.Client{Addr: "192.168.1.101:6379"}
	// client.Addr = "127.0.0.1:6379"

	client.Hmset("h1", map[string]string{"Key3": "abc", "Key4": "sdfs",
		"key1": "abc", "key2": "sdfs"})

	m := make(map[string]string)
	client.Hgetall("h1", m)
	log.Println(m)

	a := AA{}
	client.Hgetall("h1", &a)
	log.Println(a)
}