Skip to content

Commit 732374b

Browse files
committed
added AsFIFO option
1 parent 3904740 commit 732374b

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

cache.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"sync"
55
"time"
66

7+
"github.com/Code-Hex/go-generics-cache/fifo"
78
"github.com/Code-Hex/go-generics-cache/lfu"
89
"github.com/Code-Hex/go-generics-cache/lru"
910
"github.com/Code-Hex/go-generics-cache/simple"
@@ -21,6 +22,7 @@ var (
2122
_ Interface[any, any] = (*simple.Cache[any, any])(nil)
2223
_ Interface[any, any] = (*lru.Cache[any, any])(nil)
2324
_ Interface[any, any] = (*lfu.Cache[any, any])(nil)
25+
_ Interface[any, any] = (*fifo.Cache[any, any])(nil)
2426
)
2527

2628
// Item is an item
@@ -95,6 +97,13 @@ func AsLFU[K comparable, V any](opts ...lfu.Option) Option[K, V] {
9597
}
9698
}
9799

100+
// AsFIFO is an option to make a new Cache as FIFO algorithm.
101+
func AsFIFO[K comparable, V any](opts ...fifo.Option) Option[K, V] {
102+
return func(o *options[K, V]) {
103+
o.cache = fifo.NewCache[K, *Item[K, V]](opts...)
104+
}
105+
}
106+
98107
// New creates a new Cache.
99108
func New[K comparable, V any](opts ...Option[K, V]) *Cache[K, V] {
100109
o := newOptions[K, V]()

example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func ExampleCache() {
2121
}
2222

2323
func ExampleCacheWithExpiration() {
24-
c := cache.New(cache.AsLFU[string, int]())
24+
c := cache.New(cache.AsFIFO[string, int]())
2525
exp := 250 * time.Millisecond
2626
c.Set("a", 1, cache.WithExpiration(exp))
2727

0 commit comments

Comments
 (0)