Example #1
0
func getCasidForCachedItem(cache ybc.Cacher, key []byte) (casid uint64, cacheMiss, ok bool) {
	item, err := cache.GetItem(key)
	if err != nil {
		if err == ybc.ErrCacheMiss {
			cacheMiss = true
			return
		}
		log.Fatalf("Unexpected error returned from Cache.GetItem() for key=[%s]: [%s]", key, err)
	}
	// do not use defer item.Close() for performance reasons

	var buf [casidSize]byte
	n, err := item.Read(buf[:])
	item.Close()
	if err != nil {
		log.Printf("Error when reading casid for the item: [%s]", err)
		return
	}
	if n != len(buf) {
		log.Printf("Unexpected result returned from ybc.Item.Read(): %d. Expected %d", n, len(buf))
		return
	}
	casid = binary.LittleEndian.Uint64(buf[:])
	ok = true
	return
}
Example #2
0
func cGetFromCache(cache ybc.Cacher, key []byte, etag *uint64) (item *ybc.Item, err error) {
	item, err = cache.GetItem(key)
	if err == ybc.ErrCacheMiss {
		return
	}
	if err != nil {
		log.Fatalf("Unexpected error returned from Cache.GetItem() for key=[%s]: [%s]", key, err)
	}
	defer func() {
		if err != nil {
			item.Close()
		}
	}()

	etagOld := *etag
	if err = binaryRead(item, etag, "etag"); err != nil {
		return
	}
	if etagOld == *etag {
		item.Close()
		item = nil
		return
	}
	return
}
Example #3
0
func cachedItemExists(cache ybc.Cacher, key []byte) bool {
	item, err := cache.GetItem(key)
	if err == ybc.ErrCacheMiss {
		return false
	}
	if err != nil {
		log.Fatalf("Unexpected error returned from Cacher.GetItem(): [%s]", err)
	}
	item.Close()
	return true
}
Example #4
0
func getItemAndWriteResponse(w *bufio.Writer, cache ybc.Cacher, key []byte, cas bool, scratchBuf *[]byte) bool {
	item, err := cache.GetItem(key)
	if err != nil {
		if err == ybc.ErrCacheMiss {
			return true
		}
		log.Fatalf("Unexpected error returned by cache.GetItem(key=[%s]): [%s]", key, err)
	}
	defer item.Close()

	return writeGetResponse(w, key, item, cas, scratchBuf)
}
Example #5
0
func getItemAndWriteResponse(w *bufio.Writer, cache ybc.Cacher, key []byte, shouldWriteCasid bool, scratchBuf *[]byte) bool {
	item, err := cache.GetItem(key)
	if err != nil {
		if err == ybc.ErrCacheMiss {
			return true
		}
		log.Fatalf("Unexpected error returned by cache.GetItem(key=[%s]): [%s]", key, err)
	}
	// do not use defer item.Close() for performance reasons

	ok := writeGetResponse(w, key, item, shouldWriteCasid, scratchBuf)
	item.Close()
	return ok
}
Example #6
0
func processCgetCmd(c *bufio.ReadWriter, cache ybc.Cacher, line []byte, scratchBuf *[]byte) bool {
	n := -1

	key := nextToken(line, &n, "key")
	if key == nil {
		return false
	}
	casid, ok := parseUint64Token(line, &n, "casid")
	if !ok {
		return false
	}
	if !expectEof(line, n) {
		return false
	}

	item, err := cache.GetItem(key)
	if err == ybc.ErrCacheMiss {
		return writeStr(c.Writer, strEndCrLf)
	}
	if err != nil {
		log.Fatalf("Unexpected error returned: [%s]", err)
	}
	// do not use defer item.Close() for performance reasons

	isModified, ok := checkAndUpdateCasid(item, &casid)
	if !ok {
		item.Close()
		return false
	}
	if !isModified {
		item.Close()
		return writeStr(c.Writer, strNotModifiedCrLf)
	}

	ok = writeGetResponseWithEof(c.Writer, key, item, scratchBuf)
	item.Close()
	return ok
}