in dax/internal/lru/lru.go [65:117]
func (c *Lru) GetWithContext(ctx context.Context, okey Key) (interface{}, error) {
ikey := okey
if c.KeyMarshaller != nil {
ikey = c.KeyMarshaller(okey)
}
if en, ok := c.lookup(ikey); ok {
return en.value, nil
}
v, err := c.loadGroup.do(ikey, func() (interface{}, error) {
if en, ok := c.lookup(ikey); ok {
return en.value, nil
}
val, err := c.LoadFunc(ctx, okey)
if err != nil {
return nil, err
}
c.mu.Lock()
defer c.mu.Unlock()
en := &entry{key: ikey, value: val}
if c.tail == nil {
c.head = en
c.tail = en
} else {
en.prev = c.tail
c.tail.next = en
c.tail = en
}
if c.cache == nil {
c.cache = make(map[Key]*entry)
}
c.cache[ikey] = en
// Evict oldest entry if over the max.
if c.MaxEntries > 0 && len(c.cache) > c.MaxEntries {
evict := c.head
if evict != nil {
delete(c.cache, evict.key)
c.head = evict.next
if c.head != nil {
c.head.prev = nil
}
evict.next = nil
}
}
return val, nil
})
return v, err
}