This commit is contained in:
terence tsao
2021-09-14 16:11:25 -07:00
committed by GitHub
parent 9935ca3733
commit 77de467250
33 changed files with 33 additions and 33 deletions

16
cache/lru/BUILD.bazel vendored Normal file
View File

@@ -0,0 +1,16 @@
load("@prysm//tools/go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["lru_wrpr.go"],
importpath = "github.com/prysmaticlabs/prysm/cache/lru",
visibility = ["//visibility:public"],
deps = ["@com_github_hashicorp_golang_lru//:go_default_library"],
)
go_test(
name = "go_default_test",
srcs = ["lru_wrpr_test.go"],
embed = [":go_default_library"],
deps = ["@com_github_stretchr_testify//assert:go_default_library"],
)

26
cache/lru/lru_wrpr.go vendored Normal file
View File

@@ -0,0 +1,26 @@
package lru
import (
"fmt"
lru "github.com/hashicorp/golang-lru"
)
// New creates an LRU of the given size.
func New(size int) *lru.Cache {
cache, err := lru.New(size)
if err != nil {
panic(fmt.Errorf("lru new failed: %w", err))
}
return cache
}
// NewWithEvict constructs a fixed size cache with the given eviction
// callback.
func NewWithEvict(size int, onEvicted func(key interface{}, value interface{})) *lru.Cache {
cache, err := lru.NewWithEvict(size, onEvicted)
if err != nil {
panic(fmt.Errorf("lru new with evict failed: %w", err))
}
return cache
}

37
cache/lru/lru_wrpr_test.go vendored Normal file
View File

@@ -0,0 +1,37 @@
package lru
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
assert.NotPanics(t, func() {
New(10)
})
}
func TestNew_ZeroOrNegativeSize(t *testing.T) {
assert.Panics(t, func() {
New(0)
})
assert.Panics(t, func() {
New(-1)
})
}
func TestNewWithEvict(t *testing.T) {
assert.NotPanics(t, func() {
NewWithEvict(10, func(key interface{}, value interface{}) {})
})
}
func TestNewWithEvict_ZeroOrNegativeSize(t *testing.T) {
assert.Panics(t, func() {
NewWithEvict(0, func(key interface{}, value interface{}) {})
})
assert.Panics(t, func() {
NewWithEvict(-1, func(key interface{}, value interface{}) {})
})
}