Esempio n. 1
0
func (h *APIHandler) createCacheEntry(ctx context.Context, resp *httpSessionResponse, cacheKey []byte, req *http.Request) (*cache.CacheEntry, *gorpc.CallHandlerError) {
	content, err := json.Marshal(resp)
	if err != nil {
		return nil, &gorpc.CallHandlerError{
			Type: gorpc.ErrorWriteResponse,
			Err:  err,
		}
	}
	cacheEntry := cache.CacheEntry{
		Content: content,
	}
	if len(content) > 1024 && (cacheKey != nil || strings.Contains(req.Header.Get("Accept-Encoding"), "gzip")) {
		buf := new(bytes.Buffer)
		gzipWriter := gzip.NewWriter(buf)
		gzipWriter.Write(content)
		gzipWriter.Close()
		cacheEntry.CompressedContent = buf.Bytes()
	}
	return &cacheEntry, nil
}
Esempio n. 2
0
func (h *APIHandler) createCacheEntry(ctx context.Context, resp *httpSessionResponse, cacheKey []byte, req *http.Request) (*cache.CacheEntry, *gorpc.CallHandlerError) {
	content, err := resp.MarshalJSON()
	if err != nil {
		return nil, &gorpc.CallHandlerError{
			Type: gorpc.ErrorWriteResponse,
			Err:  err,
		}
	}
	cacheEntry := cache.CacheEntry{
		Content: content,
	}
	if len(content) > 4096 && cacheKey != nil && strings.Contains(req.Header.Get("Accept-Encoding"), "gzip") {
		buf := new(bytes.Buffer)
		if gzipWriter, err := gzip.NewWriterLevel(buf, gzip.BestSpeed); err == nil {
			gzipWriter.Write(content)
			gzipWriter.Close()
			cacheEntry.CompressedContent = buf.Bytes()
		}
	}
	return &cacheEntry, nil
}