Example #1
0
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
}
Example #2
0
// 获取 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
}
Example #3
0
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)
		}
	}
}
Example #4
0
func workerSetOrg(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)
		item.Value = value
		startTime := time.Now()
		if err := client.Set(&item); err != nil {
			stats.errorsCount++
			continue
		}
		updateResponseTimeHistogram(stats, startTime)
	}
}
Example #5
0
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)
	}
}
Example #6
0
func initQueue() {
	var mc *memcache.Client
	conn := utils.Addrcat(host, port)
	mc = memcache.New(conn)

	err := mc.Add(&memcache.Item{Key: topicName, Value: []byte{}})
	if err != nil {
		log.Printf("add error: %v", err)
	}

	fullLineName := topicName + "/" + lineName
	err = mc.Add(&memcache.Item{Key: fullLineName, Value: []byte{}})
	if err != nil {
		log.Printf("add error: %v", err)
	}
}
Example #7
0
func setTestSingle(ch chan bool, mc *memcache.Client, cn, n int) {
	var err error
	v := make([]byte, dataSize)
	for i := 0; i < n; i++ {
		start := time.Now()
		err = mc.Set(&memcache.Item{Key: topicName, Value: v})
		if err != nil {
			log.Printf("set error: c%d %v", cn, err)
		} else {
			end := time.Now()
			duration := end.Sub(start).Seconds()
			log.Printf("set succ: %s spend: %.3fms", topicName, duration*1000)
		}
	}
	ch <- true
}
Example #8
0
// Store the result of an API request in memcached.
func SetCachedRequest(m *memcache.Client, key string, value *ApiResult) {
	// Don't cache errors
	if value.Success == 0 {
		return
	}
	result_json, err := json.MarshalIndent(value, "", "  ")
	if err != nil {
		log.Println(err)
	}
	m.Set(
		&memcache.Item{
			Key:        key,
			Value:      result_json,
			Expiration: 600,
		},
	)
}
Example #9
0
// 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
}
Example #10
0
func getTestSingle(ch chan bool, cn, n int) {
	var mc *memcache.Client
	conn := utils.Addrcat(host, port)
	key := topicName + "/" + lineName
	keys := []string{key, "id"}
	mc = memcache.New(conn)
	for i := 0; i < n; i++ {
		start := time.Now()
		items, err := mc.GetMulti(keys)
		if err != nil {
			log.Printf("get error: c%d %v", cn, err)
		} else {
			end := time.Now()
			duration := end.Sub(start).Seconds()
			id := string(items["id"].Value)
			log.Printf("get succ: %s spend: %.3fms", id, duration*1000)
		}
	}
	ch <- true
}
Example #11
0
func precreateItemsOrg(client *memcache_org.Client) {
	n := *itemsCount / *workersCount
	workerFunc := func(wg *sync.WaitGroup, start int) {
		defer wg.Done()
		item := memcache_org.Item{
			Value: value,
		}
		for i := start; i < start+n; i++ {
			item.Key = fmt.Sprintf("%s_%d", key, i)
			if err := client.Set(&item); err != nil {
				log.Fatalf("Error in Client.Set(): [%s]", err)
			}
		}
	}

	var wg sync.WaitGroup
	defer wg.Wait()
	for i := 0; i < *workersCount; i++ {
		wg.Add(1)
		go workerFunc(&wg, i*n)
	}
}
Example #12
0
func SavePaste(paste Paste, ren render.Render, r *http.Request, cf *swift.Connection, mc *memcache.Client) {
	paste.PasteId = genPasteId()
	payload, _ := json.Marshal(paste)
	seconds, err := getTTL(paste.PasteTtl)
	PanicIf(err)
	headers := swift.Headers{}
	now := time.Now()
	pasteIndex := 9999999999 - now.Unix()
	indexKey := fmt.Sprintf("cfpaste-%d", pasteIndex)
	headers["x-object-meta-pastetype"] = paste.PasteType
	headers["x-object-meta-pasteid"] = paste.PasteId
	headers["x-object-meta-pasteindex"] = fmt.Sprintf("%d", pasteIndex)
	if seconds != 0 {
		headers["x-delete-after"] = fmt.Sprintf("%d", seconds)
	}
	buf := bytes.NewBuffer(payload)
	_, err = cf.ObjectPut("go-cfpaste", paste.PasteId, buf, true, "", "application/json; charset=utf-8", headers)
	PanicIf(err)
	// gholt's listing index hack so that he can spy on pastes
	_, err = cf.ObjectPut("go-cfpaste", indexKey, bytes.NewBuffer([]byte("")), true, "", "application/json; charset=utf-8", headers)
	PanicIf(err)
	mc.Set(&memcache.Item{Key: paste.PasteId, Value: payload})
	ren.JSON(200, map[string]interface{}{"pasteid": paste.PasteId})
}