func GetPaste(params martini.Params, ren render.Render, r *http.Request, cf *swift.Connection, mc *memcache.Client) { cachedPaste, err := mc.Get(params["pasteid"]) format := params["format"] if err != nil { log.Println(err) } var paste Paste paste.PasteId = params["pasteid"] if cachedPaste == nil { log.Println("Asking swift for ", params["pasteid"]) cfPaste, err := cf.ObjectGetBytes("go-cfpaste", params["pasteid"]) if err != nil { if err.Error() == "Object Not Found" { ren.HTML(404, "404", paste) return } else { log.Println(err) ren.Error(500) return } } err = json.Unmarshal(cfPaste, &paste) PanicIf(err) } else { log.Println("Cache hit for ", params["pasteid"]) err = json.Unmarshal(cachedPaste.Value, &paste) PanicIf(err) } if format == "json" { ren.JSON(200, paste) } else { ren.HTML(200, "paste", paste) } return }
func workerGetSetOrg(client *memcache_org.Client, wg *sync.WaitGroup, ch <-chan int, stats *Stats) { defer wg.Done() var item memcache_org.Item for _ = range ch { n := rand.Intn(*itemsCount) item.Key = fmt.Sprintf("%s_%d", key, n) startTime := time.Now() if rand.Float64() < *getRatio { _, err := client.Get(item.Key) if err == memcache_org.ErrCacheMiss { stats.cacheMissCount++ continue } if err != nil { stats.errorsCount++ continue } stats.cacheHitCount++ updateResponseTimeHistogram(stats, startTime) } else { item.Value = value if err := client.Set(&item); err != nil { stats.errorsCount++ continue } updateResponseTimeHistogram(stats, startTime) } } }
// 获取 Session, 如果找不到返回 frontend.ErrNotFound. func SessionGet(token *SessionToken) (*Session, error) { if token == nil { return nil, errors.New("nil SessionToken") } var ( memcacheClient *memcache.Client memcacheItemKey string ) if token.Authenticated { memcacheClient = mc.Client() memcacheItemKey = mc.SessionCacheKey(token.SessionId) } else { memcacheClient = secondarymc.Client() memcacheItemKey = secondarymc.SessionCacheKey(token.SessionId) } item, err := memcacheClient.Get(memcacheItemKey) if err != nil { if err == memcache.ErrCacheMiss { err = frontend.ErrNotFound } return nil, err } var ss Session if err = json.Unmarshal(item.Value, &ss); err != nil { return nil, err } return &ss, nil }
func workerGetMissOrg(client *memcache_org.Client, wg *sync.WaitGroup, ch <-chan int, stats *Stats) { defer wg.Done() for _ = range ch { n := rand.Intn(*itemsCount) keyStr := fmt.Sprintf("miss_%s_%d", key, n) startTime := time.Now() if _, err := client.Get(keyStr); err != memcache_org.ErrCacheMiss { stats.errorsCount++ continue } stats.cacheMissCount++ updateResponseTimeHistogram(stats, startTime) } }
// Fetches a previously cached API request from memcached, or returns false if // none exists. Values should only have been set using the CacheSetRequest // method, and so should be value JSON consisting of a result encapsulated by // an ApiResult struct. func GetCachedRequest(memcache *memcache.Client, key string) (bool, []byte) { // Try and retrieve the serialized data cached_json, err := memcache.Get(key) if err != nil { // Errors are cache misses return false, nil } return true, cached_json.Value }