in-memory lru cache of limited capacity
fix memLimit flag usage use megabytes as a unit for memory store capacity fix incorrect item rearrangement in lru cache use latest 1.x Go on trais
This commit is contained in:
committed by
Serge Zaitsev
parent
6d38ab6363
commit
1411b19ad2
@@ -10,33 +10,98 @@ import (
|
||||
|
||||
type memory struct {
|
||||
sync.Mutex
|
||||
cache []Snapshot
|
||||
log logger
|
||||
limit int64
|
||||
size int64
|
||||
head *lruItem
|
||||
tail *lruItem
|
||||
}
|
||||
|
||||
// Memory creates an in-memory cache.
|
||||
func Memory() Store { return &memory{} }
|
||||
type lruItem struct {
|
||||
Snapshot
|
||||
prev *lruItem
|
||||
next *lruItem
|
||||
}
|
||||
|
||||
// Memory creates an in-memory LRU cache.
|
||||
func Memory(log logger, limit int64) Store { return &memory{log: log, limit: limit} }
|
||||
|
||||
func (m *memory) Put(ctx context.Context, snapshot Snapshot) error {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, item := range m.cache {
|
||||
if item.Module == snapshot.Module && item.Version == snapshot.Version {
|
||||
return nil
|
||||
}
|
||||
if _, err := m.lookup(snapshot.Module, snapshot.Version); err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
item := &lruItem{Snapshot: snapshot, next: m.head}
|
||||
m.insert(item)
|
||||
|
||||
for m.limit >= 0 && m.size > m.limit {
|
||||
m.evict()
|
||||
}
|
||||
m.cache = append(m.cache, snapshot)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *memory) Get(ctx context.Context, module string, version vcs.Version) (Snapshot, error) {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
for _, snapshot := range m.cache {
|
||||
if snapshot.Module == module && snapshot.Version == version {
|
||||
return snapshot, nil
|
||||
return m.lookup(module, version)
|
||||
}
|
||||
|
||||
func (m *memory) lookup(module string, version vcs.Version) (Snapshot, error) {
|
||||
for item := m.head; item != nil; item = item.next {
|
||||
if item.Module == module && item.Version == version {
|
||||
m.update(item)
|
||||
return item.Snapshot, nil
|
||||
}
|
||||
}
|
||||
return Snapshot{}, errors.New("not found")
|
||||
}
|
||||
|
||||
func (m *memory) insert(item *lruItem) {
|
||||
m.log("mem.insert",
|
||||
"module", item.Module, "version", item.Version, "size", len(item.Data),
|
||||
"cachesize", m.size, "cachelimit", m.limit)
|
||||
m.size = m.size + int64(len(item.Data))
|
||||
if m.head == nil {
|
||||
m.head = item
|
||||
m.tail = item
|
||||
return
|
||||
}
|
||||
item.next = m.head
|
||||
m.head.prev = item
|
||||
m.head = item
|
||||
}
|
||||
|
||||
func (m *memory) update(item *lruItem) {
|
||||
m.log("mem.update", "module", item.Module, "version", item.Version, "size", len(item.Data),
|
||||
"cachesize", m.size, "cachelimit", m.limit)
|
||||
if item.prev == nil {
|
||||
return
|
||||
}
|
||||
item.prev.next = item.next
|
||||
if item.next == nil {
|
||||
m.tail = item.prev
|
||||
} else {
|
||||
item.next.prev = item.prev
|
||||
}
|
||||
item.prev = nil
|
||||
item.next = m.head
|
||||
m.head.prev = item
|
||||
m.head = item
|
||||
}
|
||||
|
||||
func (m *memory) evict() {
|
||||
m.log("mem.evict", "module", m.tail.Module, "version", m.tail.Version, "size", len(m.tail.Data),
|
||||
"cachesize", m.size, "cachelimit", m.limit)
|
||||
m.size = m.size - int64(len(m.tail.Data))
|
||||
if m.tail.prev == nil {
|
||||
m.head = nil
|
||||
m.tail = nil
|
||||
return
|
||||
}
|
||||
m.tail.prev.next = nil
|
||||
m.tail = m.tail.prev
|
||||
}
|
||||
|
||||
func (m *memory) Close() error { return nil }
|
||||
|
||||
Reference in New Issue
Block a user