11package recache
22
33import (
4- "compress/flate"
54 "crypto/sha1"
65 "encoding/base64"
76 "encoding/binary"
@@ -26,36 +25,6 @@ type Key interface{}
2625// Getter must be thread-safe.
2726type Getter func (Key , * RecordWriter ) error
2827
29- // Readable stream with support for io.WriterTo and conversion to
30- // io.Reader interfaces
31- type Streamer interface {
32- // Can be called safely from multiple goroutines
33- io.WriterTo
34-
35- // Create a new io.Reader for this stream.
36- // Multiple instances of such an io.Reader can exist and be read
37- // concurrently.
38- NewReader () io.Reader
39-
40- // Convenience method for efficiently decoding stream contents as JSON into
41- // the destination variable.
42- //
43- // dst: pointer to destination variable
44- DecodeJSON (dst interface {}) error
45-
46- // Create a new io.ReadCloser for the Decompressped content of this stream.
47- //
48- // It is the caller's responsibility to call Close on the io.ReadCloser
49- // when finished reading.
50- Decompress () io.Reader
51-
52- // Return SHA1 hash of the content
53- SHA1 () [sha1 .Size ]byte
54-
55- // Return strong etag of content
56- ETag () string
57- }
58-
5928// A frontend for accessing the cache contents
6029type Frontend struct {
6130 id int
@@ -64,7 +33,7 @@ type Frontend struct {
6433}
6534
6635// Populates a record using the registered Getter
67- func (f * Frontend ) populate (k Key , rec * record ) (err error ) {
36+ func (f * Frontend ) populate (k Key , rec * Record ) (err error ) {
6837 rw := RecordWriter {
6938 cache : f .cache .id ,
7039 frontend : f .id ,
@@ -125,7 +94,7 @@ func (f *Frontend) populate(k Key, rec *record) (err error) {
12594}
12695
12796// Get a record by key and block until it has been generated
128- func (f * Frontend ) getGeneratedRecord (k Key ) (rec * record , err error ) {
97+ func (f * Frontend ) getGeneratedRecord (k Key ) (rec * Record , err error ) {
12998 loc := recordLocation {f .id , k }
13099 rec , fresh := f .cache .getRecord (loc )
131100 if fresh {
@@ -151,19 +120,15 @@ func (f *Frontend) getGeneratedRecord(k Key) (rec *record, err error) {
151120 return
152121}
153122
154- // Retrieve or generate data by key and return a consumable result Stream
155- func (f * Frontend ) Get (k Key ) (s Streamer , err error ) {
156- rec , err := f .getGeneratedRecord (k )
157- if err != nil {
158- return
159- }
160- s = recordDecoder {rec }
161- return
123+ // Retrieve or generate data by key and return cache Record
124+ func (f * Frontend ) Get (k Key ) (* Record , error ) {
125+ return f .getGeneratedRecord (k )
162126}
163127
164128// Retrieve or generate data by key and write it to w.
165129// Writes ETag to w and returns 304 on ETag match without writing data.
166- // Sets "Content-Encoding" header to "deflate".
130+ // Sets "Content-Encoding" header to "deflate", if client support deflate
131+ // compressions
167132func (f * Frontend ) WriteHTTP (k Key , w http.ResponseWriter , r * http.Request ,
168133) (n int64 , err error ) {
169134 rec , err := f .getGeneratedRecord (k )
@@ -180,14 +145,14 @@ func (f *Frontend) WriteHTTP(k Key, w http.ResponseWriter, r *http.Request,
180145 if ! supportsDeflate {
181146 // Different eTag to maintain strong eTag byte-equivalence guarantee by
182147 // differing it from the compressed eTag.
183- eTag = eTag [: len ( eTag ) - 2 ] + `-uc"`
148+ eTag = rec . ETagDecompressed ()
184149 }
185150 if r .Header .Get ("If-None-Match" ) == eTag {
186151 w .WriteHeader (304 )
187152 return
188153 }
189154 h := w .Header ()
190- h .Set ("ETag" , rec . eTag )
155+ h .Set ("ETag" , eTag )
191156
192157 if supportsDeflate {
193158 // If client accepts deflate compression use efficient deflate stream
@@ -242,15 +207,9 @@ func (f *Frontend) WriteHTTP(k Key, w http.ResponseWriter, r *http.Request,
242207 }
243208 n += 6
244209 } else {
245- // Streaming decompression for clients that don't accept deflate
210+ // Streaming decompression for clients that don't support deflate
246211 // compression
247-
248- dr := flate .NewReader (rec .NewReader ())
249- n , err = io .Copy (w , dr )
250- if err != nil {
251- return
252- }
253- err = dr .Close ()
212+ n , err = io .Copy (w , rec .Decompress ())
254213 }
255214
256215 return
0 commit comments