diff options
author | Santiago M. Mola <santi@mola.io> | 2017-07-24 14:19:21 +0200 |
---|---|---|
committer | Santiago M. Mola <santi@mola.io> | 2017-07-27 14:22:40 +0200 |
commit | ae1c4f3df729c3a7fed4cd5a1f530c95d640497a (patch) | |
tree | a1468ab8a942435435c644abdc9bac3338990bc2 /plumbing/cache/object.go | |
parent | b3fc7760ba332306bb1faa64c8a101a2e605077f (diff) | |
download | go-git-ae1c4f3df729c3a7fed4cd5a1f530c95d640497a.tar.gz |
plumbing/cache: change FIFO to LRU cache
Diffstat (limited to 'plumbing/cache/object.go')
-rw-r--r-- | plumbing/cache/object.go | 73 |
1 files changed, 0 insertions, 73 deletions
diff --git a/plumbing/cache/object.go b/plumbing/cache/object.go deleted file mode 100644 index 44e0d32..0000000 --- a/plumbing/cache/object.go +++ /dev/null @@ -1,73 +0,0 @@ -package cache - -import ( - "gopkg.in/src-d/go-git.v4/plumbing" -) - -const ( - initialQueueSize = 20 - MaxSize = 10 * MiByte -) - -type ObjectFIFO struct { - objects map[plumbing.Hash]plumbing.EncodedObject - order *queue - - maxSize FileSize - actualSize FileSize -} - -// NewObjectFIFO returns an Object cache that keeps the newest objects that fit -// into the specific memory size -func NewObjectFIFO(size FileSize) *ObjectFIFO { - return &ObjectFIFO{ - objects: make(map[plumbing.Hash]plumbing.EncodedObject), - order: newQueue(initialQueueSize), - maxSize: size, - } -} - -// Put puts a new object to the cache. If the object size is greater than the -// cache size, the object is not added. -func (c *ObjectFIFO) Put(o plumbing.EncodedObject) { - objSize := FileSize(o.Size()) - - // if the size of the object is bigger or equal than the cache size, - // skip it - if objSize >= c.maxSize { - return - } - - // if the object is into the cache, do not add it again - if _, ok := c.objects[o.Hash()]; ok { - return - } - - // delete the oldest object if cache is full - if c.actualSize >= c.maxSize { - h := c.order.Pop() - o := c.objects[h] - if o != nil { - c.actualSize -= FileSize(o.Size()) - delete(c.objects, h) - } - } - - c.objects[o.Hash()] = o - c.order.Push(o.Hash()) - c.actualSize += FileSize(o.Size()) -} - -// Get returns an object by his hash. If the object is not found in the cache, it -// returns nil -func (c *ObjectFIFO) Get(k plumbing.Hash) (plumbing.EncodedObject, bool) { - obj, ok := c.objects[k] - return obj, ok -} - -// Clear the content of this object cache -func (c *ObjectFIFO) Clear() { - c.objects = make(map[plumbing.Hash]plumbing.EncodedObject) - c.order = newQueue(initialQueueSize) - c.actualSize = 0 -} |