Exemplo n.º 1
0
func processGetDeCmd(c *bufio.ReadWriter, cache ybc.Cacher, line []byte, scratchBuf *[]byte) bool {
	n := -1

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

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

	ok = writeGetResponseWithEof(c.Writer, key, item, scratchBuf)
	item.Close()
	return ok
}
Exemplo n.º 2
0
func cGetDeFromCache(cache ybc.Cacher, key []byte, etag *uint64, graceDuration time.Duration) (item *ybc.Item, err error) {
	item, err = cache.GetDeAsyncItem(key, graceDuration)
	if err == ybc.ErrCacheMiss || err == ybc.ErrWouldBlock {
		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
}
Exemplo n.º 3
0
func processCgetDeCmd(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
	}
	graceDuration, ok := parseMillisecondsToken(line, &n, "graceDuration")
	if !ok {
		return false
	}
	if !expectEof(line, n) {
		return false
	}

	item, err := cache.GetDeAsyncItem(key, graceDuration)
	if err == ybc.ErrWouldBlock {
		return writeStr(c.Writer, strWouldBlockCrLf)
	}
	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
}