Skip to content

Format Cache-Control header #22

@missinglink

Description

@missinglink

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions