Skip to content

Commit 4753ee8

Browse files
committed
added optional functions to lfu lru
1 parent 090b841 commit 4753ee8

File tree

6 files changed

+65
-27
lines changed

6 files changed

+65
-27
lines changed

cache.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ func newOptions[K comparable, V any]() *options[K, V] {
8282
}
8383

8484
// AsLRU is an option to make a new Cache as LRU algorithm.
85-
func AsLRU[K comparable, V any](cap int) Option[K, V] {
85+
func AsLRU[K comparable, V any](opts ...lru.Option) Option[K, V] {
8686
return func(o *options[K, V]) {
87-
o.cache = lru.NewCacheWithCap[K, *Item[K, V]](cap)
87+
o.cache = lru.NewCache[K, *Item[K, V]](opts...)
8888
}
8989
}
9090

9191
// AsLFU is an option to make a new Cache as LFU algorithm.
92-
func AsLFU[K comparable, V any](cap int) Option[K, V] {
92+
func AsLFU[K comparable, V any](opts ...lfu.Option) Option[K, V] {
9393
return func(o *options[K, V]) {
94-
o.cache = lfu.NewCacheWithCap[K, *Item[K, V]](cap)
94+
o.cache = lfu.NewCache[K, *Item[K, V]](opts...)
9595
}
9696
}
9797

example_test.go

Lines changed: 4 additions & 4 deletions
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](128))
24+
c := cache.New(cache.AsLFU[string, int]())
2525
exp := 250 * time.Millisecond
2626
c.Set("a", 1, cache.WithExpiration(exp))
2727

@@ -46,7 +46,7 @@ func ExampleCacheWithExpiration() {
4646
}
4747

4848
func ExampleDelete() {
49-
c := cache.New(cache.AsLRU[string, int](128))
49+
c := cache.New(cache.AsLRU[string, int]())
5050
c.Set("a", 1)
5151
c.Delete("a")
5252
gota, aok := c.Get("a")
@@ -56,7 +56,7 @@ func ExampleDelete() {
5656
}
5757

5858
func ExampleKeys() {
59-
c := cache.New(cache.AsLFU[string, int](128))
59+
c := cache.New(cache.AsLFU[string, int]())
6060
c.Set("a", 1)
6161
c.Set("b", 1)
6262
c.Set("c", 1)
@@ -66,7 +66,7 @@ func ExampleKeys() {
6666
}
6767

6868
func ExampleContains() {
69-
c := cache.New(cache.AsLRU[string, int](128))
69+
c := cache.New(cache.AsLRU[string, int]())
7070
c.Set("a", 1)
7171
fmt.Println(c.Contains("a"))
7272
fmt.Println(c.Contains("b"))

lfu/lfu.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,36 @@ type Cache[K comparable, V any] struct {
1111
items map[K]*entry[K, V]
1212
}
1313

14-
// NewCache creates a new LFU cache whose capacity is the default size (128).
15-
func NewCache[K comparable, V any]() *Cache[K, V] {
16-
return NewCacheWithCap[K, V](128)
14+
// Option is an option for LFU cache.
15+
type Option func(*options)
16+
17+
type options struct {
18+
capacity int
19+
}
20+
21+
func newOptions() *options {
22+
return &options{
23+
capacity: 128,
24+
}
1725
}
1826

19-
// NewCacheWithCap creates a new LFU cache whose capacity is the specified size.
20-
func NewCacheWithCap[K comparable, V any](cap int) *Cache[K, V] {
27+
// WithCapacity is an option to set cache capacity.
28+
func WithCapacity(cap int) Option {
29+
return func(o *options) {
30+
o.capacity = cap
31+
}
32+
}
33+
34+
// NewCache creates a new LFU cache whose capacity is the default size (128).
35+
func NewCache[K comparable, V any](opts ...Option) *Cache[K, V] {
36+
o := newOptions()
37+
for _, optFunc := range opts {
38+
optFunc(o)
39+
}
2140
return &Cache[K, V]{
22-
cap: cap,
23-
queue: newPriorityQueue[K, V](cap),
24-
items: make(map[K]*entry[K, V], cap),
41+
cap: o.capacity,
42+
queue: newPriorityQueue[K, V](o.capacity),
43+
items: make(map[K]*entry[K, V], o.capacity),
2544
}
2645
}
2746

lfu/lfu_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestSet(t *testing.T) {
1010
// set capacity is 1
11-
cache := lfu.NewCacheWithCap[string, int](1)
11+
cache := lfu.NewCache[string, int](lfu.WithCapacity(1))
1212
cache.Set("foo", 1)
1313
if got := cache.Len(); got != 1 {
1414
t.Fatalf("invalid length: %d", got)
@@ -44,7 +44,7 @@ func TestSet(t *testing.T) {
4444
}
4545

4646
func TestDelete(t *testing.T) {
47-
cache := lfu.NewCacheWithCap[string, int](1)
47+
cache := lfu.NewCache[string, int](lfu.WithCapacity(1))
4848
cache.Set("foo", 1)
4949
if got := cache.Len(); got != 1 {
5050
t.Fatalf("invalid length: %d", got)

lru/lru.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,36 @@ type entry[K comparable, V any] struct {
1616
val V
1717
}
1818

19-
// NewCache creates a new LRU cache whose capacity is the default size (128).
20-
func NewCache[K comparable, V any]() *Cache[K, V] {
21-
return NewCacheWithCap[K, V](128)
19+
// Option is an option for LFU cache.
20+
type Option func(*options)
21+
22+
type options struct {
23+
capacity int
24+
}
25+
26+
func newOptions() *options {
27+
return &options{
28+
capacity: 128,
29+
}
2230
}
2331

24-
// NewCacheWithCap creates a new LRU cache whose capacity is the specified size.
25-
func NewCacheWithCap[K comparable, V any](cap int) *Cache[K, V] {
32+
// WithCapacity is an option to set cache capacity.
33+
func WithCapacity(cap int) Option {
34+
return func(o *options) {
35+
o.capacity = cap
36+
}
37+
}
38+
39+
// NewCache creates a new LRU cache whose capacity is the default size (128).
40+
func NewCache[K comparable, V any](opts ...Option) *Cache[K, V] {
41+
o := newOptions()
42+
for _, optFunc := range opts {
43+
optFunc(o)
44+
}
2645
return &Cache[K, V]{
27-
cap: cap,
46+
cap: o.capacity,
2847
list: list.New(),
29-
items: make(map[K]*list.Element, cap),
48+
items: make(map[K]*list.Element, o.capacity),
3049
}
3150
}
3251

lru/lru_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
func TestSet(t *testing.T) {
1010
// set capacity is 1
11-
cache := lru.NewCacheWithCap[string, int](1)
11+
cache := lru.NewCache[string, int](lru.WithCapacity(1))
1212
cache.Set("foo", 1)
1313
if got := cache.Len(); got != 1 {
1414
t.Fatalf("invalid length: %d", got)
@@ -44,7 +44,7 @@ func TestSet(t *testing.T) {
4444
}
4545

4646
func TestDelete(t *testing.T) {
47-
cache := lru.NewCacheWithCap[string, int](1)
47+
cache := lru.NewCache[string, int](lru.WithCapacity(1))
4848
cache.Set("foo", 1)
4949
if got := cache.Len(); got != 1 {
5050
t.Fatalf("invalid length: %d", got)

0 commit comments

Comments
 (0)