Skip to content

Commit

Permalink
fix(cache): handle content types with parameters in ShouldCache
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmattox committed May 14, 2024
1 parent 9eace07 commit 3be102c
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pkg/cache/ShouldCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package cache
import (
"net/http"
"strconv"
"strings"
)

// ShouldCache decides whether the response should be cached based on Cache-Control directives
// and the content type, with a default caching policy for cacheable content types.
func (c *InMemoryCache) ShouldCache(resp *http.Response, cacheControl map[string]string) bool {
contentType := resp.Header.Get("Content-Type")

// Extract the base content type without parameters (e.g., charset).
baseContentType := strings.Split(contentType, ";")[0]

// Check if the content type is cacheable using this instance's configuration.
isContentTypeCacheable := c.IsCacheableContentType(contentType)
isContentTypeCacheable := c.IsCacheableContentType(baseContentType)

Logger.Debugf("ShouldCache: Checking if content type '%s' is cacheable.", contentType)
Logger.Debugf("ShouldCache: Checking if content type '%s' (base: '%s') is cacheable.", contentType, baseContentType)
Logger.Debugf("ShouldCache: Cacheable content types: %v", c.cacheConfig.CacheableMIMETypes)

// Default to caching if the content type is cacheable and there's no "no-store" directive,
Expand Down

0 comments on commit 3be102c

Please sign in to comment.