-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfifo.go
More file actions
53 lines (45 loc) · 1.1 KB
/
Copy pathfifo.go
File metadata and controls
53 lines (45 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package heatwave
// fifo implements FIFO (First-In-First-Out) algorithm
type fifo[T any] struct {
items []*CacheItem[T]
}
// newFIFO creates a new FIFO updater
func newFIFO[T any]() *fifo[T] {
return &fifo[T]{
items: make([]*CacheItem[T], 0),
}
}
// Add adds a new item to the FIFO updater
func (f *fifo[T]) Add(item *CacheItem[T]) {
f.items = append(f.items, item)
}
// Access does nothing in FIFO strategy (no reordering on access)
func (f *fifo[T]) Access(item *CacheItem[T]) {
// FIFO doesn't reorder on access
}
// Remove removes an item from the FIFO updater
func (f *fifo[T]) Remove(item *CacheItem[T]) {
for i, it := range f.items {
if it == item {
f.items = append(f.items[:i], f.items[i+1:]...)
break
}
}
}
// Evict returns the first item (oldest) for eviction
func (f *fifo[T]) Evict() *CacheItem[T] {
if len(f.items) == 0 {
return nil
}
item := f.items[0]
f.items = f.items[1:]
return item
}
// Size returns the current size
func (f *fifo[T]) Size() int {
return len(f.items)
}
// Clear removes all items from the updater
func (f *fifo[T]) Clear() {
f.items = f.items[:0]
}