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 }
func ReceiveScanEvent(content string) string { var this ScanEvent fmt.Println(content) err := xml.Unmarshal([]byte(content), &this) if err != nil { return "" } data := "请联系系统管理员进行身份认证" if "101" == this.EventKey && "oecJ3jhN5usPBQMIXqc9bVP0toi4" == this.Ticket { data = "吼吼吼" var redcli redis.Client redcli.Addr = beego.AppConfig.String("redis_addr") redcli.Hset(this.Ticket, "scan", []byte("true")) } fmt.Println(data) rcontent := `<xml> <ToUserName><![CDATA[` + this.FromUserName + `]]></ToUserName> <FromUserName><![CDATA[` + this.ToUserName + `]]></FromUserName> <CreateTime>` + fmt.Sprint((time.Now().Unix())) + `</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[` + data + `]]></Content> </xml>` return rcontent }
func getArticle(client redis.Client, page int, order string) []map[string]string { if order == "" { order = "score:" } start := (page - 1) * ARTICLES_PER_PAGE end := start + ARTICLES_PER_PAGE - 1 ids, err := client.Zrevrange(order, start, end) if err != nil { log.Fatal("Zrevrange エラーだよ", err.Error()) } articles := make([]map[string]string, 0) for _, id := range ids { articleData := make(map[string]string, 0) if err := client.Hgetall(string(id), articleData); err != nil { log.Fatal("Hgetall エラーだよ", err.Error()) } articleData["id"] = string(id) articles = append(articles, articleData) } return articles }
func main() { // 创建一个redis客户端 var client redis.Client // 尝试从缓存中拿数据 val, err := client.Get("Dog") // 检查缓存命中 if err != nil { fmt.Println("Error fetching from", err) } else { fmt.Println("Cache hit") dog, err := DecodeData(val) if err != nil { fmt.Println("Error decoding data from memcache", err) } else { fmt.Println("Dog name is: ", dog.Name) } } // 创建对象实例 spot := Dog{Name: "Spot", Color: "brown"} err = client.Set("Dog", EncodeData(spot)) if err != nil { fmt.Println("Error setting memcache item", err) } }
func alreadyBeingHandled(input string) bool { c := redis.Client{Addr: addr, Password: pass} c.Auth(pass) check, _ := c.Exists(input) return check }
func (c App) Index(call string) revel.Result { var client redis.Client // c.Response.ContentType = "application/json" callsign, _ := client.Get("callsign:" + strings.ToUpper(call)) log.Printf(string(callsign)) // log.Printf(json.Marshal(callsign)) callsign_data := string(callsign) return c.Render(call, callsign_data) }
/// 获取用户信息列表 func GetUserListInfo() { var redcli redis.Client redcli.Addr = beego.AppConfig.String("redis_addr") key := "wx_UserList" b, _ := redcli.Lrange(key, 0, -1) for _, v := range b { GetUserInfo(fmt.Sprintf("%s", v)) } }
/// 获取用户列表并报错到redis数据库中 KEY=wx_UserList func GetUserList() { url := "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" + GetAccessToken() key := "wx_UserList" var v WXUserList c, status := global.HttpGet(url) if status >= 0 { dec := json.NewDecoder(strings.NewReader(c)) for { if err := dec.Decode(&v); err == io.EOF { break } else if err != nil { log.Fatal(err) } } var redcli redis.Client redcli.Addr = beego.AppConfig.String("redis_addr") if ok, err := redcli.Exists(key); err != nil { log.Println(err.Error()) } else { if ok { redcli.Del(key) } } for _, value := range v.Data.Openid { redcli.Rpush(key, []byte(value)) } log.Println("Successful: Add Userlist ") } }
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()) } } }
func checkLearningDB(msg string) bool { /*set up redis client*/ c := redis.Client{Addr: addr, Password: pass} c.Auth(pass) // hash the message hash := md5hash(msg) // check databse check, _ := c.Exists(hash) if check == true { return true } else { return false } }
func getGroupArticle(client redis.Client, group string, page int) []map[string]string { order := "score:" key := order + group if ok, err := client.Exists(key); err != nil { log.Fatal("Exists エラーだよ", err.Error()) } else if !ok { //if _, err := client.Zinterstore(key, "group:" + group, order); err != nil { // log.Fatal("Zinterstore エラーだよ", err.Error()) //} if _, err := client.Expire(key, 60); err != nil { log.Fatal("Expire エラーだよ", err.Error()) } } return getArticle(client, page, key) }
func setToBeHandled(input string) { c := redis.Client{Addr: addr, Password: pass} c.Auth(pass) c.Set(input, "1") c.Expire(input, EXPIRETIME) }
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()) } } }
/* * Poll the worker's job queue * jobHandlers - a reference to the clients map of available job handlers */ func (this Worker) Poll(jobHandlers map[string]reflect.Type) { for { // Pop a job off the queue var client redis.Client _, message, err := client.Blpop([]string{this.Queue}, 1) if err != nil { fmt.Println(fmt.Sprintf("ERROR: %s\n", err)) } else if len(message) > 0 { // Basic debugging information // fmt.Println(this.Name + ": Yay Work Work Work!") // fmt.Println(string(message)) // Parse the message name, params, err := this.parseMessage(message) if err != nil { fmt.Println(fmt.Sprintf("ERROR: %s\n", err)) } else { // Construct the handler and call the execute() function jobHandler := reflect.New(jobHandlers[name]) executeMethod := jobHandler.MethodByName("Execute") if executeMethod.IsValid() { executeMethod.Call([]reflect.Value{0: reflect.ValueOf(params)}) } else { fmt.Println(fmt.Sprintf("ERROR: Invalid job handler")) } } } else { // fmt.Println(this.Name + ": Nothing to do :(\n") } // Sleep for 2 seconds before polling the queue again time.Sleep(2 * time.Second) } }
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) }
func GetTempTicket(expire_seconds int, scene_id int, scene_str string) string { url := `https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=` + GetAccessToken() data := `{"expire_seconds": ` + strconv.Itoa(expire_seconds) + `, "action_name": "QR_SCENE", ` + `"action_info": {"scene": {"scene_id": ` + strconv.Itoa(scene_id) + `,"scene_str":"` + scene_str + `"}}}` if ticket := getTicket(url, data); len(ticket) < 1 { return "" } else { var redcli redis.Client redcli.Addr = beego.AppConfig.String("redis_addr") redcli.Hset(ticket, "scene_id", []byte(strconv.Itoa(scene_id))) redcli.Expire(ticket, 120) return ticket } }
func main() { // Create client and tell it about our job client := gtw.New(4, "go-to-work") client.AddJobHandler("SayHello", SayHelloJob{}) client.BeginPolling() // Push sample messages wait.Add(5) var redisClient redis.Client redisClient.Rpush("go-to-work", []byte(`{"Name": "SayHello", "Params": {"Greeting": "Susan"}}`)) redisClient.Rpush("go-to-work", []byte(`{"Name": "SayHello", "Params": {"Greeting": "Jim"}}`)) time.Sleep(1 * time.Second) redisClient.Rpush("go-to-work", []byte(`{"Name": "SayHello", "Params": {"Greeting": "Stefanie"}}`)) redisClient.Rpush("go-to-work", []byte(`{"Name": "SayHello", "Params": {"Greeting": "Brian"}}`)) redisClient.Rpush("go-to-work", []byte(`{"Name": "SayHello", "Params": {"Greeting": "Dan"}}`)) wait.Wait() }