Skip to content

Commit d9ceafd

Browse files
committed
(draft) Improving cache behavior for first entry
Resolves gofiber#3072
1 parent 58d07f0 commit d9ceafd

File tree

3 files changed

+5
-5
lines changed

3 files changed

+5
-5
lines changed

Diff for: middleware/cache/cache.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ func New(config ...Config) fiber.Handler {
123123
}
124124

125125
// Check if entry is expired
126-
if e.exp != 0 && ts >= e.exp {
126+
if e != nil && e.exp != 0 && ts >= e.exp {
127127
deleteKey(key)
128128
if cfg.MaxBytes > 0 {
129129
_, size := heap.remove(e.heapidx)
130130
storedBytes -= size
131131
}
132-
} else if e.exp != 0 && !hasRequestDirective(c, noCache) {
132+
} else if e != nil && e.exp != 0 && !hasRequestDirective(c, noCache) {
133133
// Separate body value to avoid msgp serialization
134134
// We can store raw bytes with Storage 👍
135135
if cfg.Storage != nil {
@@ -193,6 +193,7 @@ func New(config ...Config) fiber.Handler {
193193
}
194194
}
195195

196+
e = manager.acquire()
196197
// Cache response
197198
e.body = utils.CopyBytes(c.Response().Body())
198199
e.status = c.Response().StatusCode()

Diff for: middleware/cache/heap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type heapEntry struct {
1515
// elements in constant time. It does so by handing out special indices
1616
// and tracking entry movement.
1717
//
18-
// indexdedHeap is used for quickly finding entries with the lowest
18+
// indexedHeap is used for quickly finding entries with the lowest
1919
// expiration timestamp and deleting arbitrary entries.
2020
type indexedHeap struct {
2121
// Slice the heap is built on

Diff for: middleware/cache/manager.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,7 @@ func (m *manager) get(key string) *item {
8383
return it
8484
}
8585
if it, _ = m.memory.Get(key).(*item); it == nil { //nolint:errcheck // We store nothing else in the pool
86-
it = m.acquire()
87-
return it
86+
return nil
8887
}
8988
return it
9089
}

0 commit comments

Comments
 (0)