Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion bigcache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,63 @@ func TestEntryBiggerThanMaxShardSizeError(t *testing.T) {
err := cache.Set("key1", blob('a', 1024*1025))

// then
assertEqual(t, "entry is bigger than max shard size", err.Error())
assertEqual(t, ErrEntryTooBig, err)
}

func TestEntryBiggerThanMaxShardSizeDoesNotAllocateOrEvict(t *testing.T) {
t.Parallel()

// given
cache, err := New(context.Background(), Config{
Shards: 1,
LifeWindow: 5 * time.Second,
MaxEntriesInWindow: 1,
MaxEntrySize: 256,
HardMaxCacheSize: 1,
})
noError(t, err)
noError(t, cache.Set("existing", []byte("value")))
initialEntryBufferSize := len(cache.shards[0].entryBuffer)
initialQueueCapacity := cache.shards[0].entries.Capacity()

// when
err = cache.Set("too-large", blob('a', 1024*1025))

// then
assertEqual(t, ErrEntryTooBig, err)
assertEqual(t, initialEntryBufferSize, len(cache.shards[0].entryBuffer))
assertEqual(t, initialQueueCapacity, cache.shards[0].entries.Capacity())
value, getErr := cache.Get("existing")
noError(t, getErr)
assertEqual(t, []byte("value"), value)
}

func TestAppendEntryBiggerThanMaxShardSizeDoesNotAllocateOrEvict(t *testing.T) {
t.Parallel()

// given
cache, err := New(context.Background(), Config{
Shards: 1,
LifeWindow: 5 * time.Second,
MaxEntriesInWindow: 1,
MaxEntrySize: 256,
HardMaxCacheSize: 1,
})
noError(t, err)
noError(t, cache.Set("existing", []byte("value")))
initialEntryBufferSize := len(cache.shards[0].entryBuffer)
initialQueueCapacity := cache.shards[0].entries.Capacity()

// when
err = cache.Append("existing", blob('a', 1024*1025))

// then
assertEqual(t, ErrEntryTooBig, err)
assertEqual(t, initialEntryBufferSize, len(cache.shards[0].entryBuffer))
assertEqual(t, initialQueueCapacity, cache.shards[0].entries.Capacity())
value, getErr := cache.Get("existing")
noError(t, getErr)
assertEqual(t, []byte("value"), value)
}

func TestHashCollision(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions entry_not_found_error.go → errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ import "errors"
var (
// ErrEntryNotFound is an error type struct which is returned when entry was not found for provided key
ErrEntryNotFound = errors.New("Entry not found") //nolint:staticcheck // keep for backward compatibility

// ErrEntryTooBig is returned when an entry cannot fit in a shard queue.
ErrEntryTooBig = errors.New("entry is bigger than max shard size")
)
13 changes: 13 additions & 0 deletions queue/bytes_queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,19 @@ func (q *BytesQueue) Push(data []byte) (int, error) {
return index, nil
}

// CanFit reports whether data of the given length can fit in the queue when
// the queue is empty without exceeding its maximum capacity.
func (q *BytesQueue) CanFit(dataLen int) bool {
if dataLen < 0 {
return false
}
if q.maxCapacity == 0 {
return true
}

return getNeededSize(dataLen) <= q.maxCapacity-leftMarginIndex
}

func (q *BytesQueue) allocateAdditionalMemory(minimum int) {
start := time.Now()
if q.capacity < minimum {
Expand Down
11 changes: 11 additions & 0 deletions queue/bytes_queue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,17 @@ func TestMaxSizeLimit(t *testing.T) {
assertEqual(t, blob('b', 5), pop(queue))
}

func TestCanFit(t *testing.T) {
t.Parallel()

// given
queue := NewBytesQueue(30, 50, false)

// then
assertEqual(t, true, queue.CanFit(48))
assertEqual(t, false, queue.CanFit(49))
}

func TestPushEntryAfterAllocateAdditionMemory(t *testing.T) {
t.Parallel()

Expand Down
19 changes: 15 additions & 4 deletions shard.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bigcache

import (
"errors"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -118,6 +117,10 @@ func (s *cacheShard) getValidWrapEntry(key string, hashedKey uint64) ([]byte, er
}

func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error {
if !s.entries.CanFit(len(entry) + len(key) + headersSizeInBytes) {
return ErrEntryTooBig
}

currentTimestamp := uint64(s.clock.Epoch())

s.lock.Lock()
Expand Down Expand Up @@ -146,12 +149,16 @@ func (s *cacheShard) set(key string, hashedKey uint64, entry []byte) error {
}
if s.removeOldestEntry(NoSpace) != nil {
s.lock.Unlock()
return errors.New("entry is bigger than max shard size")
return ErrEntryTooBig
}
}
}

func (s *cacheShard) addNewWithoutLock(key string, hashedKey uint64, entry []byte) error {
if !s.entries.CanFit(len(entry) + len(key) + headersSizeInBytes) {
return ErrEntryTooBig
}

currentTimestamp := uint64(s.clock.Epoch())

if !s.cleanEnabled {
Expand All @@ -168,7 +175,7 @@ func (s *cacheShard) addNewWithoutLock(key string, hashedKey uint64, entry []byt
return nil
}
if s.removeOldestEntry(NoSpace) != nil {
return errors.New("entry is bigger than max shard size")
return ErrEntryTooBig
}
}
}
Expand All @@ -192,7 +199,7 @@ func (s *cacheShard) setWrappedEntryWithoutLock(currentTimestamp uint64, w []byt
return nil
}
if s.removeOldestEntry(NoSpace) != nil {
return errors.New("entry is bigger than max shard size")
return ErrEntryTooBig
}
}
}
Expand All @@ -210,6 +217,10 @@ func (s *cacheShard) append(key string, hashedKey uint64, entry []byte) error {
s.lock.Unlock()
return err
}
if !s.entries.CanFit(len(wrappedEntry) + len(entry)) {
s.lock.Unlock()
return ErrEntryTooBig
}

currentTimestamp := uint64(s.clock.Epoch())

Expand Down