Skip to content

Commit 5e3f071

Browse files
committed
embed map directly, create new ptr element constraint
1 parent 974029d commit 5e3f071

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

backend.go

+1-9
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@ import (
77
"github.com/mgnsk/evcache/v3/ringlist"
88
)
99

10-
type storageMap[K comparable, V any] interface {
11-
Load(key K) (value V, ok bool)
12-
LoadOrStore(key K, value V) (actual V, loaded bool)
13-
Delete(key K)
14-
Range(f func(key K, value V) bool)
15-
}
16-
1710
type recordList[V any] struct {
1811
ringlist.List[record[V], *record[V]]
1912
}
2013

2114
type backend[K comparable, V any] struct {
2215
timer *time.Timer
2316
done chan struct{}
24-
xmap storageMap[K, *record[V]]
17+
xmap syncMapWrapper[K, *record[V]]
2518
list recordList[V]
2619
earliestExpireAt int64
2720
cap int
@@ -36,7 +29,6 @@ func newBackend[K comparable, V any](capacity int) *backend[K, V] {
3629
return &backend[K, V]{
3730
timer: t,
3831
done: make(chan struct{}),
39-
xmap: newSyncMapWrapper[K, *record[V]](capacity),
4032
cap: capacity,
4133
}
4234
}

map.go

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package evcache
22

3-
import "sync"
3+
import (
4+
"sync"
5+
)
46

7+
// syncMapWrapper wraps sync.Map. The zero syncMapWrapper is empty and ready to use.
58
type syncMapWrapper[K comparable, V any] struct {
69
sync.Map
710
}
811

9-
var _ storageMap[string, string] = &syncMapWrapper[string, string]{}
10-
11-
func newSyncMapWrapper[K comparable, V any](capacity int) storageMap[K, V] {
12-
return &syncMapWrapper[K, V]{}
13-
}
14-
1512
func (w *syncMapWrapper[K, V]) Load(key K) (value V, ok bool) {
1613
if v, ok := w.Map.Load(key); ok {
1714
return v.(V), true

ringlist/ringlist.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ type ListElement[E any] interface {
1414
Prev() E
1515
}
1616

17-
// List is a generic circular doubly linked list.
18-
// The zero value is a ready to use empty list.
19-
type List[T any, E interface {
17+
// PtrListElement is a pointer list element constraint.
18+
type PtrListElement[T any, E any] interface {
2019
*T
2120
ListElement[E]
22-
}] struct {
21+
}
22+
23+
// List is a generic circular doubly linked list.
24+
// The zero value is a ready to use empty list.
25+
type List[T any, E PtrListElement[T, E]] struct {
2326
tail E
2427
len int
2528
}

0 commit comments

Comments
 (0)