Skip to content

Commit

Permalink
upstream cache control configurable ttl header
Browse files Browse the repository at this point in the history
resolves #1194

Extends the existing EnableUpstreamCacheControl option to add a
configuration object that lets the header to listen for the TTL value
that is currently used. This defaults to our existing custom header if
unset (retaining backwards compatibility), and allows the user to
specify Expires as the header to use in order to offer an MvP of the
functionality
  • Loading branch information
asoorm committed Nov 28, 2017
1 parent ffc3979 commit 24a78b7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
11 changes: 6 additions & 5 deletions apidef/api_definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,12 @@ type MiddlewareSection struct {
}

type CacheOptions struct {
CacheTimeout int64 `bson:"cache_timeout" json:"cache_timeout"`
EnableCache bool `bson:"enable_cache" json:"enable_cache"`
CacheAllSafeRequests bool `bson:"cache_all_safe_requests" json:"cache_all_safe_requests"`
CacheOnlyResponseCodes []int `bson:"cache_response_codes" json:"cache_response_codes"`
EnableUpstreamCacheControl bool `bson:"enable_upstream_cache_control" json:"enable_upstream_cache_control"`
CacheTimeout int64 `bson:"cache_timeout" json:"cache_timeout"`
EnableCache bool `bson:"enable_cache" json:"enable_cache"`
CacheAllSafeRequests bool `bson:"cache_all_safe_requests" json:"cache_all_safe_requests"`
CacheOnlyResponseCodes []int `bson:"cache_response_codes" json:"cache_response_codes"`
EnableUpstreamCacheControl bool `bson:"enable_upstream_cache_control" json:"enable_upstream_cache_control"`
CacheControlTTLHeader string `bson:"cache_control_ttl_header" json:"cache_control_ttl_header"`
}

type ResponseProcessor struct {
Expand Down
14 changes: 9 additions & 5 deletions mw_redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import (
)

const (
upstreamCacheHeader = "x-tyk-cache-action-set"
upstreamCacheTTLHeader = "x-tyk-cache-action-set-ttl"
upstreamCacheHeader = "x-tyk-cache-action-set"
)

// RedisCacheMiddleware is a caching middleware that will pull data from Redis instead of the upstream proxy
Expand Down Expand Up @@ -191,8 +190,13 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
log.Warning("Upstream cache action not found, not caching")
cacheThisRequest = false
}
// Do we override TTL?
ttl := reqVal.Header.Get(upstreamCacheTTLHeader)

cacheTTLHeader := "x-tyk-cache-action-set-ttl"
if m.Spec.CacheOptions.CacheControlTTLHeader != "" {
cacheTTLHeader = m.Spec.CacheOptions.CacheControlTTLHeader
}

ttl := reqVal.Header.Get(cacheTTLHeader)
if ttl != "" {
log.Debug("TTL Set upstream")
cacheAsInt, err := strconv.Atoi(ttl)
Expand All @@ -215,8 +219,8 @@ func (m *RedisCacheMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Req
go m.CacheStore.SetKey(key, toStore, cacheTTL)

}
return nil, mwStatusRespond

return nil, mwStatusRespond
}

cachedData, timestamp, err := m.decodePayload(retBlob)
Expand Down

0 comments on commit 24a78b7

Please sign in to comment.