Fetch the repository succeeded.
package util
import (
"container/list"
"sync"
)
type Ring struct {
sync.Mutex
*list.List
capacity int
}
func NewRing(capacity int) *Ring {
return &Ring{capacity: capacity, List: list.New()}
}
func (r *Ring) Add(item interface{}) interface{} {
r.Lock()
defer r.Unlock()
// add new item
r.PushFront(item)
// remove old item if at capacity
var old interface{}
if r.Len() >= r.capacity {
old = r.Remove(r.Back())
}
return old
}
func (r *Ring) Slice() []interface{} {
r.Lock()
defer r.Unlock()
i := 0
items := make([]interface{}, r.Len())
for e := r.Front(); e != nil; e = e.Next() {
items[i] = e.Value
i++
}
return items
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。