glider/dns/cache.go

128 lines
2.5 KiB
Go
Raw Normal View History

package dns
import (
"sync"
"time"
)
2020-10-08 18:48:23 +08:00
// LruCache is the struct of LruCache.
type LruCache struct {
mu sync.Mutex
size int
2020-10-09 22:02:19 +08:00
head *item
tail *item
cache map[string]*item
2020-10-08 18:48:23 +08:00
store map[string][]byte
}
2020-10-09 22:02:19 +08:00
// item is the struct of cache item.
type item struct {
2020-10-08 18:48:23 +08:00
key string
val []byte
exp int64
2020-10-09 22:02:19 +08:00
prev *item
next *item
}
2020-10-09 22:02:19 +08:00
// NewLruCache returns a new LruCache.
2020-10-08 18:48:23 +08:00
func NewLruCache(size int) *LruCache {
2020-10-09 22:02:19 +08:00
// init 2 items here, it doesn't matter cuz they will be deleted when the cache is full
head, tail := &item{key: "head"}, &item{key: "tail"}
2020-10-08 18:48:23 +08:00
head.next, tail.prev = tail, head
c := &LruCache{
size: size,
head: head,
tail: tail,
2020-10-09 22:02:19 +08:00
cache: make(map[string]*item, size),
2020-10-08 18:48:23 +08:00
store: make(map[string][]byte),
}
c.cache[head.key], c.cache[tail.key] = head, tail
return c
}
2020-10-08 18:48:23 +08:00
// Get gets an item from cache.
func (c *LruCache) Get(k string) (v []byte, expired bool) {
c.mu.Lock()
defer c.mu.Unlock()
if v, ok := c.store[k]; ok {
return v, false
}
if it, ok := c.cache[k]; ok {
v = it.val
if it.exp < time.Now().Unix() {
expired = true
}
2020-10-08 18:48:23 +08:00
c.moveToHead(it)
}
return
}
2020-10-08 18:48:23 +08:00
// Set sets an item with key, value, and ttl(seconds).
// if the ttl is zero, this item will be set and never be deleted.
// if the key exists, update it with value and exp and move it to head.
// if the key does not exist, put a new item to the cache's head.
2020-10-08 18:48:23 +08:00
// finally, remove the tail if the cache is full.
func (c *LruCache) Set(k string, v []byte, ttl int) {
c.mu.Lock()
defer c.mu.Unlock()
2020-10-08 18:48:23 +08:00
if ttl == 0 {
c.store[k] = v
return
}
2020-10-08 18:48:23 +08:00
exp := time.Now().Add(time.Second * time.Duration(ttl)).Unix()
if it, ok := c.cache[k]; ok {
it.val = v
it.exp = exp
c.moveToHead(it)
return
}
2020-10-08 18:48:23 +08:00
c.putToHead(k, v, exp)
2020-10-09 22:02:19 +08:00
// NOTE: the cache size will always >= 2,
2020-10-09 22:02:19 +08:00
// but it doesn't matter in our environment.
2020-10-08 18:48:23 +08:00
if len(c.cache) > c.size {
c.removeTail()
}
}
// putToHead puts a new item to cache's head.
func (c *LruCache) putToHead(k string, v []byte, exp int64) {
2020-10-09 22:02:19 +08:00
it := &item{key: k, val: v, exp: exp, prev: nil, next: c.head}
2020-10-08 18:48:23 +08:00
it.prev = nil
it.next = c.head
c.head.prev = it
c.head = it
2020-10-09 22:02:19 +08:00
c.cache[k] = it
2020-08-23 23:23:30 +08:00
}
2020-10-08 18:48:23 +08:00
// moveToHead moves an existing item to cache's head.
2020-10-09 22:02:19 +08:00
func (c *LruCache) moveToHead(it *item) {
2020-10-08 18:48:23 +08:00
if it != c.head {
if c.tail == it {
c.tail = it.prev
c.tail.next = nil
} else {
it.prev.next = it.next
it.next.prev = it.prev
}
it.prev = nil
it.next = c.head
c.head.prev = it
c.head = it
}
2020-08-23 23:23:30 +08:00
}
2020-10-08 18:48:23 +08:00
// removeTail removes the tail from cache.
func (c *LruCache) removeTail() {
delete(c.cache, c.tail.key)
2020-10-09 22:02:19 +08:00
c.tail.prev.next = nil
2020-10-08 18:48:23 +08:00
c.tail = c.tail.prev
}