马提尼& appengine / golang,返回memcached JSON数据

时间:2014-04-26 01:25:15

标签: google-app-engine go

我现在使用Martini和AppEngine / Go进行编码。

我想使用memcache来提供JSON数据。 但是响应是无效的JSON格式。为什么" null"是在回应?

null{"results":[{"Title":"Nikkei225","PriceTime":"2014-04-25 06:28:00 UTC","Price":"14,429.26","Diff":"+24.27(0.1%)"},{"Title":"USD/JPY","PriceTime":"2014-04-25 20:49:00 UTC","Price":"102.12-102.16","Diff":"-0.15(-0.1%)"},{"Title":"EURO/JPY","PriceTime":"2014-04-25 20:49:00 UTC","Price":"141.28-141.32","Diff":"-0.19(-0.1%)"},{"Title":"EURO/USD","PriceTime":"2014-04-25 20:48:00 UTC","Price":"1.3833-1.3836","Diff":"+0.0002(0.0%)"},{"Title":"USD/CNY","PriceTime":"2014-04-25 20:48:00 UTC","Price":"6.2536-6.2546","Diff":"+0.0063(0.1%)"},{"Title":"DJIA","PriceTime":"2014-04-25 07:29:00 UTC","Price":"16,361.46","Diff":"-140.19(-0.8%)"},{"Title":"Nasdaq","PriceTime":"2014-04-25 07:00:00 UTC","Price":"4,075.561","Diff":"-72.777(-1.7%)"},{"Title":"FTSE100","PriceTime":"2014-04-25 07:35:00 UTC","Price":"6,685.69","Diff":"-17.31(-0.2%)"}]}

控制台说如下:

http: multiple response.WriteHeader calls

我的代码如下。
从缓存中检索:

    c := appengine.NewContext(req)
    memcacheKey := "markets"
    results := []Result{}
    cachedItem, getCacheErr := memcache.JSON.Get(c, memcacheKey,&map[string]interface{}{"results": results})
    if getCacheErr != nil && getCacheErr != memcache.ErrCacheMiss {
        c.Infof("get cache error")
    }
    if getCacheErr == nil {
        c.Infof("cached data found")
        c.Infof("cached data: %v",cachedItem.Value)
        r.JSON(200, cachedItem.Object)
    } else {
        c.Infof("cached data not found")
    }

设置为memcache:

    client := urlfetch.Client(c)
    resp, err := client.Get("http://www.nikkei.com/markets/kaigai/worldidx.aspx")
    if err != nil {
        http.Error(w, err.Error(), http.StatusInternalServerError)
    }
    indexes := Indexes()
    doc, _ := goquery.NewDocumentFromResponse(resp)
    doc.Find("div.mk-world_market div table tr").Each(func(_ int, s *goquery.Selection) {
            title := s.Find("th").Text()
            title = strings.Trim(strings.Replace(title,"※","",-1)," ")
            if val,ok := indexes[title]; ok {
                price := s.Find("th").Next().Text()
                diff := s.Find("td:nth-child(3)").Text()
                pricetime := s.Find("td:nth-child(4)").Text()
                t := time.Now()
                pricetime = StringToTime(pricetime,t)
                result := Result{val,pricetime,price,diff}
                results = append(results,result)
            }
    })
    item := &memcache.Item{
        Key:memcacheKey,
        Object: &map[string]interface{}{"results": results},
    }
    setErr := memcache.JSON.Set(c, item)
    if setErr != nil {
        c.Infof("set error: %v",setErr)
    }

我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

自分辨率:

我想做的是在下面。

    m.Get("/api/Markets", func(w http.ResponseWriter,r render.Render,req *http.Request) {
    c := appengine.NewContext(req)
    memcache_key := "markets"
    var item_list []Result
    results := []Result{}
    _, get_cache_err := memcache.JSON.Get(c,memcache_key,&item_list)
    if get_cache_err != nil && get_cache_err != memcache.ErrCacheMiss {
        c.Infof("get cache error")
    }
    if get_cache_err == nil {
        c.Infof("cached data found")
        c.Infof("cached data: %v",item_list)
        results = item_list
    } else {
        c.Infof("cached data not found")
        client := urlfetch.Client(c)
        resp, err := client.Get("http://www.nikkei.com/markets/kaigai/worldidx.aspx")
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
        indexes := Indexes()
        doc, _ := goquery.NewDocumentFromResponse(resp)
        doc.Find("div.mk-world_market div table tr").Each(func(_ int, s *goquery.Selection) {
            title := s.Find("th").Text()
            title = strings.Trim(strings.Replace(title,"※","",-1)," ")
            if val,ok := indexes[title]; ok {
                price := s.Find("th").Next().Text()
                diff := s.Find("td:nth-child(3)").Text()
                pricetime := s.Find("td:nth-child(4)").Text()
                t := time.Now()
                pricetime = StringToTime(pricetime,t)
                result := Result{val,pricetime,price,diff}
                results = append(results,result)
            }
        })
        item_list = results
        item := &memcache.Item{
            Key:memcache_key,
            Object: &item_list,
        }
        setErr := memcache.JSON.Set(c, item)
        if setErr != nil {
            c.Infof("set error: %v",setErr)
        }
    }
    r.JSON(200, map[string]interface{}{"results": item_list})
})