Format Cache-Control header
Heya,
I was wondering if there was some functionality which allows reversing the parse process to generate a Cache-Control string value from a ResponseCacheDirectives struct?
My usecase is that I would like to act as a proxy, parse the response header coming from my downstream service, check if the max-age is set, and if the value is unset or too low I will modify it before proxying it to the user.
I ended up just doing this:
// enforce a default CacheControl max-age value if:
// 1. A default value is configured for this endpoint.
// 2. The existing 'Cache-Control' header is unset or 'public'.
// 3. The existing 'max-age' value is unset or lower than the default.
if e.DefaultAge > 0 {
policy, _ := cacheobject.ParseResponseCacheControl(resp.Header.Get("Cache-Control"))
if policy.Public && int(policy.MaxAge) < e.DefaultAge {
// policy.MaxAge = cacheobject.DeltaSeconds(e.DefaultAge)
resp.Header.Set("Cache-Control", fmt.Sprintf("public, max-age:%d", e.DefaultAge))
}
}
I was hoping to find something more elegant than the fmt.Sprintf("public, max-age:%d", e.DefaultAge) line because presumably it's more complex than just ignoring the other properties?
I think this would generally be a good feature - two quick thoughts: we might need a new structure to properly serialize it, and we might want to emit other headers beyond cache-control.
+1 on this. Would love to take a ResponseCacheDirectives and convert that to a string :)