2020-01-31 23:18:11 -05:00
|
|
|
package d2common
|
2019-12-16 22:23:01 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-12-26 11:13:05 -05:00
|
|
|
"log"
|
2019-12-16 22:23:01 -05:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cacheNode struct {
|
|
|
|
next *cacheNode
|
|
|
|
prev *cacheNode
|
|
|
|
key string
|
|
|
|
value interface{}
|
|
|
|
weight int
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
type Cache struct {
|
2019-12-26 11:13:05 -05:00
|
|
|
head *cacheNode
|
|
|
|
tail *cacheNode
|
|
|
|
lookup map[string]*cacheNode
|
|
|
|
weight int
|
|
|
|
budget int
|
|
|
|
verbose bool
|
|
|
|
mutex sync.Mutex
|
2019-12-16 22:23:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func CreateCache(budget int) *Cache {
|
|
|
|
return &Cache{lookup: make(map[string]*cacheNode), budget: budget}
|
|
|
|
}
|
|
|
|
func (c *Cache) SetCacheVerbose(verbose bool) {
|
|
|
|
c.verbose = verbose
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetWeight() int {
|
|
|
|
return c.weight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetBudget() int {
|
|
|
|
return c.budget
|
2019-12-16 22:23:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (c *Cache) Insert(key string, value interface{}, weight int) error {
|
2019-12-16 22:23:01 -05:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
if _, found := c.lookup[key]; found {
|
2020-01-31 23:18:11 -05:00
|
|
|
return errors.New("key already exists in Cache")
|
2019-12-16 22:23:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
node := &cacheNode{
|
|
|
|
key: key,
|
|
|
|
value: value,
|
|
|
|
weight: weight,
|
|
|
|
next: c.head,
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.head != nil {
|
|
|
|
c.head.prev = node
|
|
|
|
}
|
|
|
|
|
|
|
|
c.head = node
|
|
|
|
if c.tail == nil {
|
|
|
|
c.tail = node
|
|
|
|
}
|
|
|
|
|
|
|
|
c.lookup[key] = node
|
|
|
|
c.weight += node.weight
|
|
|
|
|
|
|
|
for ; c.tail != nil && c.tail != c.head && c.weight > c.budget; c.tail = c.tail.prev {
|
|
|
|
c.weight -= c.tail.weight
|
|
|
|
c.tail.prev.next = nil
|
2019-12-26 11:13:05 -05:00
|
|
|
|
|
|
|
if c.verbose {
|
|
|
|
log.Printf(
|
2020-01-31 23:18:11 -05:00
|
|
|
"warning -- Cache is evicting %s (%d) for %s (%d); spare weight is now %d",
|
2019-12-26 11:13:05 -05:00
|
|
|
c.tail.key,
|
|
|
|
c.tail.weight,
|
|
|
|
key,
|
|
|
|
weight,
|
|
|
|
c.budget-c.weight,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-12-16 22:23:01 -05:00
|
|
|
delete(c.lookup, c.tail.key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (c *Cache) Retrieve(key string) (interface{}, bool) {
|
2019-12-16 22:23:01 -05:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
node, found := c.lookup[key]
|
|
|
|
if !found {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if node != c.head {
|
|
|
|
if node.next != nil {
|
|
|
|
node.next.prev = node.prev
|
|
|
|
}
|
|
|
|
if node.prev != nil {
|
|
|
|
node.prev.next = node.next
|
|
|
|
}
|
|
|
|
|
|
|
|
if node == c.tail {
|
|
|
|
c.tail = c.tail.prev
|
|
|
|
}
|
|
|
|
|
|
|
|
node.next = c.head
|
|
|
|
node.prev = nil
|
|
|
|
|
|
|
|
if c.head != nil {
|
|
|
|
c.head.prev = node
|
|
|
|
}
|
|
|
|
|
|
|
|
c.head = node
|
|
|
|
}
|
|
|
|
|
|
|
|
return node.value, true
|
|
|
|
}
|
2019-12-26 11:13:05 -05:00
|
|
|
|
2020-01-31 23:18:11 -05:00
|
|
|
func (c *Cache) Clear() {
|
2019-12-26 11:13:05 -05:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
|
|
|
c.head = nil
|
|
|
|
c.tail = nil
|
|
|
|
c.lookup = make(map[string]*cacheNode)
|
|
|
|
c.weight = 0
|
|
|
|
}
|