Skip to content

Commit b109437

Browse files
author
mangotree
committed
fix: can not resuce bytes queue
Change-Id: I3a655b4cc0f63d457487c24d250cdcd542ed5cb1
1 parent 37b9eb2 commit b109437

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

bigcache_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,46 @@ func TestOldestEntryDeletionWhenMaxCacheSizeIsReached(t *testing.T) {
747747
assertEqual(t, blob('c', 1024*800), entry3)
748748
}
749749

750+
func TestNewEntryWriteWhenMaxCacheSizeIsReached(t *testing.T) {
751+
t.Parallel()
752+
753+
// given
754+
cache, _ := NewBigCache(Config{
755+
Shards: 1,
756+
LifeWindow: 5 * time.Second,
757+
MaxEntriesInWindow: 2,
758+
MaxEntrySize: 400,
759+
HardMaxCacheSize: 1,
760+
})
761+
762+
// when
763+
key1Err := cache.Set("key1", blob('a', 1024*400))
764+
key2Err := cache.Set("key2", blob('b', 1024*400))
765+
key3Err := cache.Set("key3", blob('c', 1024*400))
766+
key4Err := cache.Set("key4", blob('d', 1024*400))
767+
768+
769+
//then
770+
noError(t,key1Err)
771+
noError(t,key2Err)
772+
noError(t,key3Err)
773+
noError(t,key4Err)
774+
775+
// when
776+
_, key1Err = cache.Get("key1")
777+
_, key2Err = cache.Get("key2")
778+
entry3, _ := cache.Get("key3")
779+
entry4, _ := cache.Get("key4")
780+
781+
// then
782+
assertEqual(t, key1Err, ErrEntryNotFound)
783+
assertEqual(t, key2Err, ErrEntryNotFound)
784+
assertEqual(t, blob('c', 1024*400), entry3)
785+
assertEqual(t, blob('d', 1024*400), entry4)
786+
787+
}
788+
789+
750790
func TestRetrievingEntryShouldCopy(t *testing.T) {
751791
t.Parallel()
752792

queue/bytes_queue.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ var (
2222
// BytesQueue is a non-thread safe queue type of fifo based on bytes array.
2323
// For every push operation index of entry is returned. It can be used to read the entry later
2424
type BytesQueue struct {
25-
full bool
2625
array []byte
2726
capacity int
2827
maxCapacity int
@@ -78,7 +77,6 @@ func (q *BytesQueue) Reset() {
7877
q.head = leftMarginIndex
7978
q.rightMargin = leftMarginIndex
8079
q.count = 0
81-
q.full = false
8280
}
8381

8482
// Push copies entry at the end of queue and moves tail pointer. Allocates more space if needed.
@@ -143,9 +141,6 @@ func (q *BytesQueue) push(data []byte, len int) {
143141
if q.tail > q.head {
144142
q.rightMargin = q.tail
145143
}
146-
if q.tail == q.head {
147-
q.full = true
148-
}
149144

150145
q.count++
151146
}
@@ -238,9 +233,6 @@ func (q *BytesQueue) peek(index int) ([]byte, int, error) {
238233

239234
// canInsertAfterTail returns true if it's possible to insert an entry of size of need after the tail of the queue
240235
func (q *BytesQueue) canInsertAfterTail(need int) bool {
241-
if q.full {
242-
return false
243-
}
244236
if q.tail >= q.head {
245237
return q.capacity-q.tail >= need
246238
}
@@ -253,9 +245,6 @@ func (q *BytesQueue) canInsertAfterTail(need int) bool {
253245

254246
// canInsertBeforeHead returns true if it's possible to insert an entry of size of need before the head of the queue
255247
func (q *BytesQueue) canInsertBeforeHead(need int) bool {
256-
if q.full {
257-
return false
258-
}
259248
if q.tail >= q.head {
260249
return q.head-leftMarginIndex == need || q.head-leftMarginIndex >= need+minimumHeaderSize
261250
}

shard.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"sync"
66
"sync/atomic"
77

8-
"github.com/allegro/bigcache/v2/queue"
8+
"github.com/allegro/bigcache/queue"
99
)
1010

1111
type onRemoveCallback func(wrappedEntry []byte, reason RemoveReason)

0 commit comments

Comments
 (0)