| 1 |
package cache |
| 2 |
|
| 3 |
import ( |
| 4 |
"container/list" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
// BufferLRU implements an object cache with an LRU eviction policy and a |
| 9 |
// maximum size (measured in object size). |
| 10 |
type BufferLRU struct { |
| 11 |
MaxSize FileSize |
| 12 |
|
| 13 |
actualSize FileSize |
| 14 |
ll *list.List |
| 15 |
cache map[int64]*list.Element |
| 16 |
mut sync.Mutex |
| 17 |
} |
| 18 |
|
| 19 |
// NewBufferLRU creates a new BufferLRU with the given maximum size. The maximum |
| 20 |
// size will never be exceeded. |
| 21 |
func NewBufferLRU(maxSize FileSize) *BufferLRU { |
| 22 |
return &BufferLRU{MaxSize: maxSize} |
| 23 |
} |
| 24 |
|
| 25 |
// NewBufferLRUDefault creates a new BufferLRU with the default cache size. |
| 26 |
func NewBufferLRUDefault() *BufferLRU { |
| 27 |
return &BufferLRU{MaxSize: DefaultMaxSize} |
| 28 |
} |
| 29 |
|
| 30 |
type buffer struct { |
| 31 |
Key int64 |
| 32 |
Slice []byte |
| 33 |
} |
| 34 |
|
| 35 |
// Put puts a buffer into the cache. If the buffer is already in the cache, it |
| 36 |
// will be marked as used. Otherwise, it will be inserted. A buffers might |
| 37 |
// be evicted to make room for the new one. |
| 38 |
func (c *BufferLRU) Put(key int64, slice []byte) { |
| 39 |
c.mut.Lock() |
| 40 |
defer c.mut.Unlock() |
| 41 |
|
| 42 |
if c.cache == nil { |
| 43 |
c.actualSize = 0 |
| 44 |
c.cache = make(map[int64]*list.Element, 1000) |
| 45 |
c.ll = list.New() |
| 46 |
} |
| 47 |
|
| 48 |
bufSize := FileSize(len(slice)) |
| 49 |
if ee, ok := c.cache[key]; ok { |
| 50 |
oldBuf := ee.Value.(buffer) |
| 51 |
// in this case bufSize is a delta: new size - old size |
| 52 |
bufSize -= FileSize(len(oldBuf.Slice)) |
| 53 |
c.ll.MoveToFront(ee) |
| 54 |
ee.Value = buffer{key, slice} |
| 55 |
} else { |
| 56 |
if bufSize > c.MaxSize { |
| 57 |
return |
| 58 |
} |
| 59 |
ee := c.ll.PushFront(buffer{key, slice}) |
| 60 |
c.cache[key] = ee |
| 61 |
} |
| 62 |
|
| 63 |
c.actualSize += bufSize |
| 64 |
for c.actualSize > c.MaxSize { |
| 65 |
last := c.ll.Back() |
| 66 |
lastObj := last.Value.(buffer) |
| 67 |
lastSize := FileSize(len(lastObj.Slice)) |
| 68 |
|
| 69 |
c.ll.Remove(last) |
| 70 |
delete(c.cache, lastObj.Key) |
| 71 |
c.actualSize -= lastSize |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
// Get returns a buffer by its key. It marks the buffer as used. If the buffer |
| 76 |
// is not in the cache, (nil, false) will be returned. |
| 77 |
func (c *BufferLRU) Get(key int64) ([]byte, bool) { |
| 78 |
c.mut.Lock() |
| 79 |
defer c.mut.Unlock() |
| 80 |
|
| 81 |
ee, ok := c.cache[key] |
| 82 |
if !ok { |
| 83 |
return nil, false |
| 84 |
} |
| 85 |
|
| 86 |
c.ll.MoveToFront(ee) |
| 87 |
return ee.Value.(buffer).Slice, true |
| 88 |
} |
| 89 |
|
| 90 |
// Clear the content of this buffer cache. |
| 91 |
func (c *BufferLRU) Clear() { |
| 92 |
c.mut.Lock() |
| 93 |
defer c.mut.Unlock() |
| 94 |
|
| 95 |
c.ll = nil |
| 96 |
c.cache = nil |
| 97 |
c.actualSize = 0 |
| 98 |
} |