예제 #1
0
파일: http_json.go 프로젝트: wtertius/gorpc
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
}
예제 #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
}