Skip to content

Commit

Permalink
embed map directly, create new ptr element constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnsk committed Jun 17, 2023
1 parent 974029d commit 5e3f071
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 20 deletions.
10 changes: 1 addition & 9 deletions backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,14 @@ import (
"github.com/mgnsk/evcache/v3/ringlist"
)

type storageMap[K comparable, V any] interface {
Load(key K) (value V, ok bool)
LoadOrStore(key K, value V) (actual V, loaded bool)
Delete(key K)
Range(f func(key K, value V) bool)
}

type recordList[V any] struct {
ringlist.List[record[V], *record[V]]
}

type backend[K comparable, V any] struct {
timer *time.Timer
done chan struct{}
xmap storageMap[K, *record[V]]
xmap syncMapWrapper[K, *record[V]]
list recordList[V]
earliestExpireAt int64
cap int
Expand All @@ -36,7 +29,6 @@ func newBackend[K comparable, V any](capacity int) *backend[K, V] {
return &backend[K, V]{
timer: t,
done: make(chan struct{}),
xmap: newSyncMapWrapper[K, *record[V]](capacity),
cap: capacity,
}
}
Expand Down
11 changes: 4 additions & 7 deletions map.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package evcache

import "sync"
import (
"sync"
)

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

var _ storageMap[string, string] = &syncMapWrapper[string, string]{}

func newSyncMapWrapper[K comparable, V any](capacity int) storageMap[K, V] {
return &syncMapWrapper[K, V]{}
}

func (w *syncMapWrapper[K, V]) Load(key K) (value V, ok bool) {
if v, ok := w.Map.Load(key); ok {
return v.(V), true
Expand Down
11 changes: 7 additions & 4 deletions ringlist/ringlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@ type ListElement[E any] interface {
Prev() E
}

// List is a generic circular doubly linked list.
// The zero value is a ready to use empty list.
type List[T any, E interface {
// PtrListElement is a pointer list element constraint.
type PtrListElement[T any, E any] interface {
*T
ListElement[E]
}] struct {
}

// List is a generic circular doubly linked list.
// The zero value is a ready to use empty list.
type List[T any, E PtrListElement[T, E]] struct {
tail E
len int
}
Expand Down

0 comments on commit 5e3f071

Please sign in to comment.