Skip to content

Commit

Permalink
(draft) Improving cache behavior for first entry
Browse files Browse the repository at this point in the history
Resolves gofiber#3072
  • Loading branch information
brunodmartins committed Jul 16, 2024
1 parent 58d07f0 commit d9ceafd
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
5 changes: 3 additions & 2 deletions middleware/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,13 @@ func New(config ...Config) fiber.Handler {
}

// Check if entry is expired
if e.exp != 0 && ts >= e.exp {
if e != nil && e.exp != 0 && ts >= e.exp {
deleteKey(key)
if cfg.MaxBytes > 0 {
_, size := heap.remove(e.heapidx)
storedBytes -= size
}
} else if e.exp != 0 && !hasRequestDirective(c, noCache) {
} else if e != nil && e.exp != 0 && !hasRequestDirective(c, noCache) {
// Separate body value to avoid msgp serialization
// We can store raw bytes with Storage 👍
if cfg.Storage != nil {
Expand Down Expand Up @@ -193,6 +193,7 @@ func New(config ...Config) fiber.Handler {
}
}

e = manager.acquire()
// Cache response
e.body = utils.CopyBytes(c.Response().Body())
e.status = c.Response().StatusCode()
Expand Down
2 changes: 1 addition & 1 deletion middleware/cache/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type heapEntry struct {
// elements in constant time. It does so by handing out special indices
// and tracking entry movement.
//
// indexdedHeap is used for quickly finding entries with the lowest
// indexedHeap is used for quickly finding entries with the lowest
// expiration timestamp and deleting arbitrary entries.
type indexedHeap struct {
// Slice the heap is built on
Expand Down
3 changes: 1 addition & 2 deletions middleware/cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ func (m *manager) get(key string) *item {
return it
}
if it, _ = m.memory.Get(key).(*item); it == nil { //nolint:errcheck // We store nothing else in the pool
it = m.acquire()
return it
return nil
}
return it
}
Expand Down

0 comments on commit d9ceafd

Please sign in to comment.