mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-09 13:28:01 -05:00
add lru (#9592)
This commit is contained in:
16
cache/lru/BUILD.bazel
vendored
Normal file
16
cache/lru/BUILD.bazel
vendored
Normal 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
26
cache/lru/lru_wrpr.go
vendored
Normal 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
37
cache/lru/lru_wrpr_test.go
vendored
Normal 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{}) {})
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user