fix golint issues in core/collection, refine cache interface (#475)

This commit is contained in:
Kevin Wan
2021-02-18 15:49:56 +08:00
committed by GitHub
parent f14ab70035
commit 457048bfac
14 changed files with 139 additions and 89 deletions

View File

@@ -2,12 +2,14 @@ package collection
import "sync"
// A Ring can be used as fixed size ring.
type Ring struct {
elements []interface{}
index int
lock sync.Mutex
}
// NewRing returns a Ring object with the given size n.
func NewRing(n int) *Ring {
if n < 1 {
panic("n should be greater than 0")
@@ -18,6 +20,7 @@ func NewRing(n int) *Ring {
}
}
// Add adds v into r.
func (r *Ring) Add(v interface{}) {
r.lock.Lock()
defer r.lock.Unlock()
@@ -26,6 +29,7 @@ func (r *Ring) Add(v interface{}) {
r.index++
}
// Take takes all items from r.
func (r *Ring) Take() []interface{} {
r.lock.Lock()
defer r.lock.Unlock()