Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Sep 22, 2023
1 parent a0aa876 commit 439b222
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
11 changes: 7 additions & 4 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func newBackend[K comparable, V any](capacity int) *backend[K, V] {
}
}

func (b *backend[K, V]) close() error {
func (b *backend[K, V]) Close() error {
close(b.done)
return nil
}
Expand Down Expand Up @@ -130,8 +130,7 @@ func (b *backend[K, V]) runGC(now int64) {

var overflowed map[*list.Element[record[V]]]bool

n := b.overflow()
if n > 0 {
if n := b.overflow(); n > 0 {
overflowed = make(map[*list.Element[record[V]]]bool, n)

elem := b.list.Front()
Expand All @@ -147,7 +146,11 @@ func (b *backend[K, V]) runGC(now int64) {

b.xmap.Range(func(key K, elem *list.Element[record[V]]) bool {
if elem.Value.initialized.Load() {
if n > 0 && overflowed[elem] || elem.Value.deadline > 0 && elem.Value.deadline < now {
if len(overflowed) > 0 && overflowed[elem] {
delete(overflowed, elem)
b.xmap.Delete(key)
b.list.Remove(elem)
} else if elem.Value.deadline > 0 && elem.Value.deadline < now {
b.xmap.Delete(key)
b.list.Remove(elem)
} else if elem.Value.deadline > 0 && (earliest == 0 || elem.Value.deadline < earliest) {
Expand Down
2 changes: 1 addition & 1 deletion cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func New[K comparable, V any](capacity int) *Cache[K, V] {
}

runtime.SetFinalizer(c, func(any) {
b.close()
b.Close()
})

return c
Expand Down

0 comments on commit 439b222

Please sign in to comment.