2021-03-07 06:39:50 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2021-10-19 19:53:29 +03:00
|
|
|
"sync"
|
2021-03-07 06:39:50 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Lru simple, fast lru cache implementation
|
|
|
|
type Lru interface {
|
|
|
|
Get(key interface{}) (value interface{}, ok bool)
|
|
|
|
GetKeyFromValue(value interface{}) (key interface{}, ok bool)
|
|
|
|
PeekKeyFromValue(value interface{}) (key interface{}, ok bool) // Peek means check but NOT bring to top
|
|
|
|
Put(key, value interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type lru struct {
|
|
|
|
capacity int
|
|
|
|
doubleLinkedlist *list.List
|
|
|
|
keyToElement *sync.Map
|
|
|
|
valueToElement *sync.Map
|
|
|
|
mu *sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
type lruElement struct {
|
|
|
|
key interface{}
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
2022-01-13 04:51:47 +02:00
|
|
|
// NewLru initializes a lru cache
|
2021-03-07 06:39:50 +02:00
|
|
|
func NewLru(cap int) Lru {
|
2021-10-19 19:53:29 +03:00
|
|
|
return &lru{
|
2021-03-07 06:39:50 +02:00
|
|
|
capacity: cap,
|
|
|
|
doubleLinkedlist: list.New(),
|
|
|
|
keyToElement: new(sync.Map),
|
|
|
|
valueToElement: new(sync.Map),
|
|
|
|
mu: new(sync.Mutex),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:53:29 +03:00
|
|
|
func (l *lru) Get(key interface{}) (value interface{}, ok bool) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2021-03-07 06:39:50 +02:00
|
|
|
if v, ok := l.keyToElement.Load(key); ok {
|
|
|
|
element := v.(*list.Element)
|
2021-10-19 19:53:29 +03:00
|
|
|
l.doubleLinkedlist.MoveToFront(element)
|
|
|
|
return element.Value.(*lruElement).value, true
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:53:29 +03:00
|
|
|
func (l *lru) GetKeyFromValue(value interface{}) (key interface{}, ok bool) {
|
|
|
|
l.mu.Lock()
|
|
|
|
defer l.mu.Unlock()
|
2021-03-07 06:39:50 +02:00
|
|
|
if k, ok := l.valueToElement.Load(value); ok {
|
|
|
|
element := k.(*list.Element)
|
2021-10-19 19:53:29 +03:00
|
|
|
l.doubleLinkedlist.MoveToFront(element)
|
|
|
|
return element.Value.(*lruElement).key, true
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:53:29 +03:00
|
|
|
func (l *lru) PeekKeyFromValue(value interface{}) (key interface{}, ok bool) {
|
2021-03-07 06:39:50 +02:00
|
|
|
if k, ok := l.valueToElement.Load(value); ok {
|
|
|
|
element := k.(*list.Element)
|
2021-10-19 19:53:29 +03:00
|
|
|
return element.Value.(*lruElement).key, true
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-10-19 19:53:29 +03:00
|
|
|
func (l *lru) Put(key, value interface{}) {
|
|
|
|
l.mu.Lock()
|
|
|
|
e := &lruElement{key, value}
|
2021-03-07 06:39:50 +02:00
|
|
|
if v, ok := l.keyToElement.Load(key); ok {
|
|
|
|
element := v.(*list.Element)
|
|
|
|
element.Value = e
|
2021-10-19 19:53:29 +03:00
|
|
|
l.doubleLinkedlist.MoveToFront(element)
|
2021-03-07 06:39:50 +02:00
|
|
|
} else {
|
|
|
|
element := l.doubleLinkedlist.PushFront(e)
|
|
|
|
l.keyToElement.Store(key, element)
|
|
|
|
l.valueToElement.Store(value, element)
|
|
|
|
if l.doubleLinkedlist.Len() > l.capacity {
|
|
|
|
toBeRemove := l.doubleLinkedlist.Back()
|
|
|
|
l.doubleLinkedlist.Remove(toBeRemove)
|
2021-10-19 19:53:29 +03:00
|
|
|
l.keyToElement.Delete(toBeRemove.Value.(*lruElement).key)
|
|
|
|
l.valueToElement.Delete(toBeRemove.Value.(*lruElement).value)
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|
|
|
|
}
|
2021-10-19 19:53:29 +03:00
|
|
|
l.mu.Unlock()
|
2021-03-07 06:39:50 +02:00
|
|
|
}
|