示例#1
0
func setCacheResult(dest groupcache.Sink, fill genFunction, zip bool) error {
	glog.V(2).Infoln("Start content generation.")
	var uncompressedContent *bytes.Buffer
	// TODO: Use variable-sized buffer pool for uncompressedContent and content.
	//       Already tried simple buffer bool for uncompressedContent without much
	//       apparent latency benefit, but with large memory overhead.
	uncompressedContent = &bytes.Buffer{}

	if err := fill(uncompressedContent); err != nil {
		return err
	}
	if zip {
		t1 := time.Now()
		// TODO: Use a buffer pool below.  This ultimately gets copied to the outgoing
		//       stream, and is more complicated to release because the outgoing
		//       stream bytes could also come from the cache.
		var content bytes.Buffer
		err := gziphandler.GzipContent(&content, uncompressedContent.Bytes())
		glog.V(3).Infof("PERF: gzip time: %v\n", time.Now().Sub(t1))
		if err != nil {
			return err
		}
		return dest.SetTimestampBytes(content.Bytes(), groupcache.GetTime())
	} else {
		return dest.SetTimestampBytes(uncompressedContent.Bytes(), groupcache.GetTime())
	}
}
示例#2
0
// loadFile is not safe for concurrent use!
func (s *servedFiles) loadFile(filename string) (err error) {
	uncompressedContents, err := getFileContents(filename)
	if err != nil {
		return err
	}
	baseName := filepath.Base(filename)

	contentType := mime.TypeByExtension(filepath.Ext(baseName))
	var contents bytes.Buffer
	if err = gziphandler.GzipContent(&contents, uncompressedContents); err != nil {
		return err
	}
	(*s)[baseName] = &servedFile{Contents: contents.Bytes(), ContentType: contentType}
	return
}