mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
functions: remove lru (#399)
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
// Package routecache is meant to assist in resolving the most used routes at
|
||||
// an application. Implemented as a LRU, it returns always its full context for
|
||||
// iteration at the router handler.
|
||||
package routecache
|
||||
|
||||
// based on groupcache's LRU
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
|
||||
"github.com/iron-io/functions/api/models"
|
||||
)
|
||||
|
||||
// Cache holds an internal linkedlist for hotness management. It is not safe
|
||||
// for concurrent use, must be guarded externally.
|
||||
type Cache struct {
|
||||
MaxEntries int
|
||||
|
||||
ll *list.List
|
||||
cache map[string]*list.Element
|
||||
}
|
||||
|
||||
// New returns a route cache.
|
||||
func New(maxentries int) *Cache {
|
||||
return &Cache{
|
||||
MaxEntries: maxentries,
|
||||
ll: list.New(),
|
||||
cache: make(map[string]*list.Element),
|
||||
}
|
||||
}
|
||||
|
||||
// Refresh updates internal linkedlist either adding a new route to the front,
|
||||
// or moving it to the front when used. It will discard seldom used routes.
|
||||
func (c *Cache) Refresh(route *models.Route) {
|
||||
if c.cache == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ee, ok := c.cache[route.Path]; ok {
|
||||
c.ll.MoveToFront(ee)
|
||||
ee.Value = route
|
||||
return
|
||||
}
|
||||
|
||||
ele := c.ll.PushFront(route)
|
||||
c.cache[route.Path] = ele
|
||||
if c.MaxEntries != 0 && c.ll.Len() > c.MaxEntries {
|
||||
c.removeOldest()
|
||||
}
|
||||
}
|
||||
|
||||
// Get looks up a path's route from the cache.
|
||||
func (c *Cache) Get(path string) (route *models.Route, ok bool) {
|
||||
if c.cache == nil {
|
||||
return
|
||||
}
|
||||
if ele, hit := c.cache[path]; hit {
|
||||
c.ll.MoveToFront(ele)
|
||||
return ele.Value.(*models.Route), true
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Cache) removeOldest() {
|
||||
if c.cache == nil {
|
||||
return
|
||||
}
|
||||
if ele := c.ll.Back(); ele != nil {
|
||||
c.removeElement(ele)
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cache) removeElement(e *list.Element) {
|
||||
c.ll.Remove(e)
|
||||
kv := e.Value.(*models.Route)
|
||||
delete(c.cache, kv.Path)
|
||||
}
|
||||
Reference in New Issue
Block a user