Skip to content

Commit

Permalink
improved EntryNotFoundError (#92)
Browse files Browse the repository at this point in the history
* EntryNotFound generate error message only if Error is executed

* added bench readFromCacheNonExistentKeys
  • Loading branch information
yanmhlv authored and cristaloleg committed Sep 10, 2018
1 parent 5e5c707 commit 5946793
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions bigcache_bench_test.go
Expand Up @@ -98,6 +98,14 @@ func BenchmarkWriteToCacheWith1024ShardsAndSmallShardInitSize(b *testing.B) {
writeToCache(b, 1024, 100*time.Second, 100)
}

func BenchmarkReadFromCacheNonExistentKeys(b *testing.B) {
for _, shards := range []int{1, 512, 1024, 8192} {
b.Run(fmt.Sprintf("%d-shards", shards), func(b *testing.B) {
readFromCacheNonExistentKeys(b, 1024)
})
}
}

func writeToCache(b *testing.B, shards int, lifeWindow time.Duration, requestsInLifeWindow int) {
cache, _ := NewBigCache(Config{
Shards: shards,
Expand Down Expand Up @@ -139,3 +147,21 @@ func readFromCache(b *testing.B, shards int) {
}
})
}

func readFromCacheNonExistentKeys(b *testing.B, shards int) {
cache, _ := NewBigCache(Config{
Shards: shards,
LifeWindow: 1000 * time.Second,
MaxEntriesInWindow: max(b.N, 100),
MaxEntrySize: 500,
})
b.ResetTimer()

b.RunParallel(func(pb *testing.PB) {
b.ReportAllocs()

for pb.Next() {
cache.Get(strconv.Itoa(rand.Intn(b.N)))
}
})
}
6 changes: 3 additions & 3 deletions entry_not_found_error.go
Expand Up @@ -4,14 +4,14 @@ import "fmt"

// EntryNotFoundError is an error type struct which is returned when entry was not found for provided key
type EntryNotFoundError struct {
message string
key string
}

func notFound(key string) error {
return &EntryNotFoundError{fmt.Sprintf("Entry %q not found", key)}
return &EntryNotFoundError{key}
}

// Error returned when entry does not exist.
func (e EntryNotFoundError) Error() string {
return e.message
return fmt.Sprintf("Entry %q not found", e.key)
}

0 comments on commit 5946793

Please sign in to comment.