Skip to content

Commit

Permalink
feat: supports ignore headers for http cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Aug 1, 2023
1 parent 1bb1f60 commit 35591da
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.17

require (
github.com/andybalholm/brotli v1.0.5
github.com/stretchr/testify v1.8.2
github.com/tidwall/gjson v1.14.4
github.com/stretchr/testify v1.8.4
github.com/tidwall/gjson v1.15.0
github.com/vicanso/hes v0.6.2
github.com/vicanso/intranet-ip v0.1.0
github.com/vicanso/keygrip v1.2.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/tidwall/gjson v1.15.0 h1:5n/pM+v3r5ujuNl4YLZLsQ+UE5jlkLVm7jMzT5Mpolw=
github.com/tidwall/gjson v1.15.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
Expand Down
10 changes: 7 additions & 3 deletions middleware/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type CacheConfig struct {
// Marshal marshal function for cache, if BodyBuffer is nil,
// the body will be marshaled to body buffer. The default marshal function will be json.Marshal
Marshal func(interface{}) ([]byte, error)
// IgnoreHeaders ignore the headers for cache
IgnoreHeaders []string
}

type CacheStatus uint8
Expand All @@ -71,7 +73,7 @@ type CacheStore interface {
const HeaderAge = "Age"
const HeaderXCache = "X-Cache"

var ignoreHeaders = []string{
var defaultIgnoreHeaders = []string{
"Content-Encoding",
"Content-Length",
"Connection",
Expand Down Expand Up @@ -193,13 +195,14 @@ var hitForPassData = (&CacheResponse{
}).Bytes()

// Bytes converts the cache response to bytes
func (cp *CacheResponse) Bytes() []byte {
func (cp *CacheResponse) Bytes(ignoreHeaders ...string) []byte {
// 只有hit的才需要保存后续的数据
if cp.Status != StatusHit {
return []byte{
byte(cp.Status),
}
}
ignoreHeaders = append(ignoreHeaders, defaultIgnoreHeaders...)
headers := NewHTTPHeaders(cp.Header, ignoreHeaders...)
headersLength := len(headers)
// 1个字节保存状态
Expand Down Expand Up @@ -369,6 +372,7 @@ func NewCache(config CacheConfig) elton.Handler {
if marshal == nil {
marshal = json.Marshal
}
ignoreHeaders := config.IgnoreHeaders
compressor := config.Compressor
return func(c *elton.Context) error {
if skipper(c) {
Expand Down Expand Up @@ -435,7 +439,7 @@ func NewCache(config CacheConfig) elton.Handler {
Header: c.Header(),
Body: buffer,
}
data = cacheResp.Bytes()
data = cacheResp.Bytes(ignoreHeaders...)
// 如果想忽略store的错误,则自定义store时,
// 不要返回出错则可
err = store.Set(ctx, key, data, time.Duration(cacheAge)*time.Second)
Expand Down

0 comments on commit 35591da

Please sign in to comment.