evictIfNeeded will evict an entity from the cache if needed
()
| 616 | |
| 617 | // evictIfNeeded will evict an entity from the cache if needed |
| 618 | func (sc *SubCache[EntityT, ExcerptT, CacheT]) evictIfNeeded() { |
| 619 | sc.mu.Lock() |
| 620 | defer sc.mu.Unlock() |
| 621 | if sc.lru.Len() <= sc.maxLoaded { |
| 622 | return |
| 623 | } |
| 624 | |
| 625 | for _, id := range sc.lru.GetOldestToNewest() { |
| 626 | b := sc.cached[id] |
| 627 | if b.NeedCommit() { |
| 628 | continue |
| 629 | } |
| 630 | |
| 631 | // as a form of assurance that evicted entities don't get manipulated, we lock them here. |
| 632 | // if something tries to do it anyway, it will lock the program and make it obvious. |
| 633 | b.Lock() |
| 634 | |
| 635 | sc.lru.Remove(id) |
| 636 | delete(sc.cached, id) |
| 637 | |
| 638 | if sc.lru.Len() <= sc.maxLoaded { |
| 639 | return |
| 640 | } |
| 641 | } |
| 642 | } |
no test coverage detected