mirror of
https://github.com/v2rayA/v2rayA.git
synced 2024-11-25 16:34:19 +08:00
Use standard library container/list for lru (#1186)
This commit is contained in:
parent
9e0916cd82
commit
93bbed1a81
@ -1,113 +0,0 @@
|
||||
package linklist
|
||||
|
||||
// linklist with head node and tail node
|
||||
type Node struct {
|
||||
prior *Node
|
||||
next *Node
|
||||
Val interface{}
|
||||
}
|
||||
|
||||
func (p *Node) Next() *Node {
|
||||
return p.next
|
||||
}
|
||||
|
||||
func (p *Node) Prior() *Node {
|
||||
return p.prior
|
||||
}
|
||||
|
||||
type Linklist struct {
|
||||
head *Node
|
||||
tail *Node
|
||||
}
|
||||
|
||||
func NewLinklist() *Linklist {
|
||||
head := new(Node)
|
||||
tail := new(Node)
|
||||
head.next = tail
|
||||
tail.prior = head
|
||||
return &Linklist{
|
||||
head: head,
|
||||
tail: tail,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *Linklist) Front() *Node {
|
||||
node := l.head.next
|
||||
if node == l.tail {
|
||||
return nil
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func (l *Linklist) Back() *Node {
|
||||
node := l.tail.prior
|
||||
if node == l.head {
|
||||
return nil
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
func (l *Linklist) Head() *Node {
|
||||
return l.head
|
||||
}
|
||||
|
||||
func (l *Linklist) Tail() *Node {
|
||||
return l.tail
|
||||
}
|
||||
|
||||
func (l *Linklist) Empty() bool {
|
||||
return l.head.next == l.tail
|
||||
}
|
||||
|
||||
func (l *Linklist) InsertAfter(prior *Node, val interface{}) *Node {
|
||||
if prior == l.tail {
|
||||
return nil
|
||||
}
|
||||
p := new(Node)
|
||||
p.Val = val
|
||||
p.prior = prior
|
||||
p.next = prior.next
|
||||
prior.next.prior = p
|
||||
prior.next = p
|
||||
return p
|
||||
}
|
||||
|
||||
func (l *Linklist) PushFront(val interface{}) *Node {
|
||||
return l.InsertAfter(l.head, val)
|
||||
}
|
||||
|
||||
func (l *Linklist) PushBack(val interface{}) *Node {
|
||||
return l.InsertAfter(l.tail.prior, val)
|
||||
}
|
||||
|
||||
func (l *Linklist) Promote(p *Node) {
|
||||
if p == l.Front() {
|
||||
return
|
||||
}
|
||||
p.prior.next = p.next
|
||||
p.next.prior = p.prior
|
||||
p.prior = l.head
|
||||
p.next = l.head.next
|
||||
l.head.next.prior = p
|
||||
l.head.next = p
|
||||
}
|
||||
|
||||
func (l *Linklist) Demote(p *Node) {
|
||||
if p == l.Back() {
|
||||
return
|
||||
}
|
||||
p.prior.next = p.next
|
||||
p.next.prior = p.prior
|
||||
p.prior = l.tail.prior
|
||||
p.next = l.tail
|
||||
l.tail.prior.next = p
|
||||
l.tail.prior = p
|
||||
}
|
||||
|
||||
func (l *Linklist) Remove(p *Node) {
|
||||
if p == l.head || p == l.tail {
|
||||
return
|
||||
}
|
||||
p.prior.next = p.next
|
||||
p.next.prior = p.prior
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
package lru
|
||||
|
||||
import (
|
||||
"github.com/v2rayA/v2rayA/infra/dataStructure/linklist"
|
||||
"container/list"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@ -14,9 +14,9 @@ const (
|
||||
)
|
||||
|
||||
type LRU struct {
|
||||
list *linklist.Linklist
|
||||
index map[interface{}]*linklist.Node
|
||||
reverseIndex map[*linklist.Node]interface{}
|
||||
list *list.List
|
||||
index map[interface{}]*list.Element
|
||||
reverseIndex map[*list.Element]interface{}
|
||||
mutex sync.Mutex
|
||||
strategy LimitStrategy
|
||||
limit int64
|
||||
@ -29,9 +29,9 @@ type EncapsulatedValue struct {
|
||||
|
||||
func New(strategy LimitStrategy, limit int64) *LRU {
|
||||
return &LRU{
|
||||
index: make(map[interface{}]*linklist.Node),
|
||||
reverseIndex: make(map[*linklist.Node]interface{}),
|
||||
list: linklist.NewLinklist(),
|
||||
index: make(map[interface{}]*list.Element),
|
||||
reverseIndex: make(map[*list.Element]interface{}),
|
||||
list: list.New(),
|
||||
strategy: strategy,
|
||||
limit: limit,
|
||||
}
|
||||
@ -59,8 +59,8 @@ func (l *LRU) get(key interface{}) (value interface{}) {
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
l.list.Promote(v)
|
||||
ev := v.Val.(*EncapsulatedValue)
|
||||
l.list.PushFront(v)
|
||||
ev := v.Value.(*EncapsulatedValue)
|
||||
ev.LastUseTime = time.Now()
|
||||
return ev.Value
|
||||
}
|
||||
@ -83,7 +83,7 @@ func (l *LRU) insert(key interface{}, val interface{}) (removed []*EncapsulatedV
|
||||
case FixedLength:
|
||||
if int64(len(l.index)) > l.limit {
|
||||
back := l.list.Back()
|
||||
removed = []*EncapsulatedValue{back.Val.(*EncapsulatedValue)}
|
||||
removed = []*EncapsulatedValue{back.Value.(*EncapsulatedValue)}
|
||||
key := l.reverseIndex[back]
|
||||
l.list.Remove(back)
|
||||
delete(l.index, key)
|
||||
@ -97,7 +97,7 @@ func (l *LRU) insert(key interface{}, val interface{}) (removed []*EncapsulatedV
|
||||
if back == nil {
|
||||
break
|
||||
}
|
||||
ev := back.Val.(*EncapsulatedValue)
|
||||
ev := back.Value.(*EncapsulatedValue)
|
||||
if int64(now.Sub(ev.LastUseTime)) < l.limit {
|
||||
break
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user