History

Set X-Cache and X-Cache-Lookup headers for cache (#8794)

X-Cache sets cache status of HIT if object is served from the disk cache, or MISS otherwise. X-Cache-Lookup is set to HIT if object was found in the cache even if not served (for e.g. if cache entry was invalidated by ETag verification)

poornas committed 6 years ago

Commit 91990331 parent 2bf6cf06 files · +50 −10Browse files

Unified · Split

6 files
  1. cmd/api-headers.go +4 −1
  2. cmd/disk-cache-backend.go +4 −2
  3. cmd/disk-cache-utils.go +18 −0
  4. cmd/disk-cache.go +13 −7
  5. cmd/http/headers.go +6 −0
  6. cmd/object-api-datatypes.go +5 −0
cmd/api-headers.go+4−1
@@ -91,7 +91,10 @@ func setObjectHeaders(w http.ResponseWriter, objInfo ObjectInfo, rs *HTTPRangeSp · 90 unmodified lines
9191 if !objInfo.Expires.IsZero() {
9292 w.Header().Set(xhttp.Expires, objInfo.Expires.UTC().Format(http.TimeFormat))
9393 }
94−
94+ if globalCacheConfig.Enabled {
95+ w.Header().Set(xhttp.XCache, objInfo.CacheStatus.String())
96+ w.Header().Set(xhttp.XCacheLookup, objInfo.CacheLookupStatus.String())
97+ }
9598 // Set all other user defined metadata.
9699 for k, v := range objInfo.UserDefined {
97100 if HasPrefix(k, ReservedMetadataPrefix) {
cmd/disk-cache-backend.go+4−2
@@ -89,8 +89,10 @@ func (m *cacheMeta) ToObjectInfo(bucket, object string) (o ObjectInfo) { · 88 unmodified lines
8989 }
9090
9191 o = ObjectInfo{
92− Bucket: bucket,
93− Name: object,
92+ Bucket: bucket,
93+ Name: object,
94+ CacheStatus: CacheHit,
95+ CacheLookupStatus: CacheHit,
9496 }
9597
9698 // We set file info only if its valid.
cmd/disk-cache-utils.go+18−0
@@ -28,6 +28,24 @@ import ( · 27 unmodified lines
2828 "github.com/minio/minio/cmd/crypto"
2929 )
3030
31+// CacheStatusType - whether the request was served from cache.
32+type CacheStatusType string
33+
34+const (
35+ // CacheHit - whether object was served from cache.
36+ CacheHit CacheStatusType = "HIT"
37+
38+ // CacheMiss - object served from backend.
39+ CacheMiss CacheStatusType = "MISS"
40+)
41+
42+func (c CacheStatusType) String() string {
43+ if c != "" {
44+ return string(c)
45+ }
46+ return string(CacheMiss)
47+}
48+
3149 type cacheControl struct {
3250 expiry time.Time
3351 maxAge int
cmd/disk-cache.go+13−7
@@ -223,7 +223,10 @@ func (c *cacheObjects) GetObjectNInfo(ctx context.Context, bucket, object string · 222 unmodified lines
223223 }
224224 if cc.noStore {
225225 c.cacheStats.incMiss()
226− return c.GetObjectNInfo(ctx, bucket, object, rs, h, lockType, opts)
226+ bReader, err := c.GetObjectNInfo(ctx, bucket, object, rs, h, lockType, opts)
227+ bReader.ObjInfo.CacheLookupStatus = CacheHit
228+ bReader.ObjInfo.CacheStatus = CacheMiss
229+ return bReader, err
227230 }
228231 }
229232
@@ -272,8 +275,14 @@ func (c *cacheObjects) GetObjectNInfo(ctx context.Context, bucket, object string · 42 unmodified lines
272275 default:
273276 }
274277 }
278+
279+ bkReader, bkErr := c.GetObjectNInfoFn(ctx, bucket, object, rs, h, lockType, opts)
280+ // Record if cache has a hit that was invalidated by ETag verification
281+ if cacheErr == nil {
282+ bkReader.ObjInfo.CacheLookupStatus = CacheHit
283+ }
275284 if !dcache.diskAvailable(objInfo.Size) {
276− return c.GetObjectNInfoFn(ctx, bucket, object, rs, h, lockType, opts)
285+ return bkReader, bkErr
277286 }
278287
279288 if rs != nil {
@@ -290,12 +299,9 @@ func (c *cacheObjects) GetObjectNInfo(ctx context.Context, bucket, object string · 10 unmodified lines
290299 c.put(ctx, dcache, bucket, object, bReader, bReader.ObjInfo.Size, rs, ObjectOptions{UserDefined: getMetadata(bReader.ObjInfo)})
291300 }
292301 }()
293− return c.GetObjectNInfoFn(ctx, bucket, object, rs, h, lockType, opts)
294− }
295− bkReader, bkErr := c.GetObjectNInfoFn(ctx, bucket, object, rs, h, lockType, opts)
296− if bkErr != nil {
297− return nil, bkErr
302+ return bkReader, bkErr
298303 }
304+
299305 // Initialize pipe.
300306 pipeReader, pipeWriter := io.Pipe()
301307 teeReader := io.TeeReader(bkReader, pipeWriter)
cmd/http/headers.go+6−0
@@ -40,6 +40,12 @@ const ( · 39 unmodified lines
4040 Action = "Action"
4141 )
4242
43+// Non standard S3 HTTP response constants
44+const (
45+ XCache = "X-Cache"
46+ XCacheLookup = "X-Cache-Lookup"
47+)
48+
4349 // Standard S3 HTTP request constants
4450 const (
4551 IfModifiedSince = "If-Modified-Since"
cmd/object-api-datatypes.go+5−0
@@ -143,6 +143,11 @@ type ObjectInfo struct { · 142 unmodified lines
143143 // Date and time at which the object is no longer able to be cached
144144 Expires time.Time
145145
146+ // CacheStatus sets status of whether this is a cache hit/miss
147+ CacheStatus CacheStatusType
148+ // CacheLookupStatus sets whether a cacheable response is present in the cache
149+ CacheLookupStatus CacheStatusType
150+
146151 // Specify object storage class
147152 StorageClass string
148153

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t