public
Created

Generic LRU cache in Go

  • Download Gist
lru.go
Go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
// Copyright 2013 Lars Buitinck
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
 
// Caches for arbitrary functions with least-recently used (LRU) eviction
// strategy.
package lru
 
// Cache for function Func.
type LRU struct {
Func func(interface{}) interface{}
index map[interface{}]int // index of key in queue
queue list
}
 
// Create a new LRU cache for function f with the desired capacity.
func New(f func(interface{}) interface{}, capacity int) *LRU {
if capacity < 1 {
panic("capacity < 1")
}
c := &LRU{Func: f, index: make(map[interface{}]int)}
c.queue.init(capacity)
return c
}
 
// Fetch value for key in the cache, calling Func to compute it if necessary.
func (c *LRU) Get(key interface{}) (value interface{}) {
i, stored := c.index[key]
if stored {
value = c.queue.valueAt(i)
c.queue.moveToFront(i)
} else {
value = c.Func(key)
c.insert(key, value)
}
return value
}
 
// Number of items currently in the cache.
func (c *LRU) Len() int {
return len(c.queue.links)
}
 
func (c *LRU) Capacity() int {
return cap(c.queue.links)
}
 
// Iterate over the cache in LRU order. Useful for debugging.
func (c *LRU) Iter(keys chan interface{}, values chan interface{}) {
for i := c.queue.tail; i != -1; {
n := c.queue.links[i]
keys <- n.key
values <- n.value
i = n.next
}
close(keys)
close(values)
}
 
func (c *LRU) insert(key interface{}, value interface{}) {
var i int
q := &c.queue
if q.full() {
// evict least recently used item
var k interface{}
i, k = q.popTail()
delete(c.index, k)
} else {
i = q.grow()
}
q.putFront(key, value, i)
c.index[key] = i
}
 
// Doubly linked list containing key/value pairs.
type list struct {
front, tail, empty int
links []link
}
 
type link struct {
key, value interface{}
prev, next int
}
 
// Initialize l with capacity c.
func (l *list) init(c int) {
l.front = -1
l.tail = -1
l.links = make([]link, 0, c)
}
 
func (l *list) full() bool {
return len(l.links) == cap(l.links)
}
 
// Grow list by one element and return its index.
func (l *list) grow() (i int) {
i = len(l.links)
l.links = l.links[:i+1]
return
}
 
// Make the node at position i the front of the list.
// Precondition: the list is not empty.
func (l *list) moveToFront(i int) {
nf := &l.links[i]
of := &l.links[l.front]
 
nf.prev = l.front
of.next = i
l.front = i
}
 
// Pop the tail off the list and return its index and its key.
// Precondition: the list is full.
func (l *list) popTail() (i int, key interface{}) {
i = l.tail
t := &l.links[i]
key = t.key
l.links[t.next].prev = -1
l.tail = t.next
return
}
 
// Put (key, value) in position i and make it the front of the list.
func (l *list) putFront(key, value interface{}, i int) {
f := &l.links[i]
f.key = key
f.value = value
f.prev = l.front
f.next = -1
 
if l.tail == -1 {
l.tail = i
} else {
l.links[l.front].next = i
}
l.front = i
}
 
func (l *list) valueAt(i int) interface{} {
return l.links[i].value
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.