mirror of
https://github.com/chrislusf/seaweedfs
synced 2025-06-29 08:12:47 +02:00
* improve perfs & fix rclone & refactoring Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> * improve perfs on download + add seaweedfs all-in-one deployment Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> * use helper for topologySpreadConstraints and fix create home dir of sftp users Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> * fix helm lint Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> * add missing ctx param Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com> --------- Signed-off-by: Mohamed Sekour <mohamed.sekour@exfo.com>
52 lines
936 B
Go
52 lines
936 B
Go
package utils
|
|
|
|
import (
|
|
"container/list"
|
|
)
|
|
|
|
type CacheEntry struct {
|
|
key int64
|
|
value []byte
|
|
}
|
|
|
|
type LruCache struct {
|
|
capacity int
|
|
ll *list.List
|
|
cache map[int64]*list.Element
|
|
}
|
|
|
|
func NewLRUCache(capacity int) *LruCache {
|
|
return &LruCache{
|
|
capacity: capacity,
|
|
ll: list.New(),
|
|
cache: make(map[int64]*list.Element),
|
|
}
|
|
}
|
|
|
|
func (c *LruCache) Get(key int64) ([]byte, bool) {
|
|
if ele, ok := c.cache[key]; ok {
|
|
c.ll.MoveToFront(ele)
|
|
return ele.Value.(*CacheEntry).value, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func (c *LruCache) Put(key int64, value []byte) {
|
|
if ele, ok := c.cache[key]; ok {
|
|
c.ll.MoveToFront(ele)
|
|
ele.Value.(*CacheEntry).value = value
|
|
return
|
|
}
|
|
|
|
if c.ll.Len() >= c.capacity {
|
|
oldest := c.ll.Back()
|
|
if oldest != nil {
|
|
c.ll.Remove(oldest)
|
|
delete(c.cache, oldest.Value.(*CacheEntry).key)
|
|
}
|
|
}
|
|
|
|
entry := &CacheEntry{key, value}
|
|
ele := c.ll.PushFront(entry)
|
|
c.cache[key] = ele
|
|
}
|