-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #268 from yyforyongyu/lru-methods
cache: add deletion and iteration methods
- Loading branch information
Showing
4 changed files
with
224 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,6 @@ breakpoints.txt | |
|
||
# coverage output | ||
coverage.txt | ||
|
||
# go workspace | ||
go.work |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package lru | ||
|
||
import "sync" | ||
|
||
// syncMap wraps a sync.Map with type parameters such that it's easier to | ||
// access the items stored in the map since no type assertion is needed. It | ||
// also requires explicit type definition when declaring and initiating the | ||
// variables, which helps us understanding what's stored in a given map. | ||
// | ||
// NOTE: this is unexported to avoid confusion with `lnd`'s `SyncMap`. | ||
type syncMap[K comparable, V any] struct { | ||
sync.Map | ||
} | ||
|
||
// Store puts an item in the map. | ||
func (m *syncMap[K, V]) Store(key K, value V) { | ||
m.Map.Store(key, value) | ||
} | ||
|
||
// Load queries an item from the map using the specified key. If the item | ||
// cannot be found, an empty value and false will be returned. If the stored | ||
// item fails the type assertion, a nil value and false will be returned. | ||
func (m *syncMap[K, V]) Load(key K) (V, bool) { | ||
result, ok := m.Map.Load(key) | ||
if !ok { | ||
return *new(V), false // nolint: gocritic | ||
} | ||
|
||
item, ok := result.(V) | ||
return item, ok | ||
} | ||
|
||
// Delete removes an item from the map specified by the key. | ||
func (m *syncMap[K, V]) Delete(key K) { | ||
m.Map.Delete(key) | ||
} | ||
|
||
// LoadAndDelete queries an item and deletes it from the map using the | ||
// specified key. | ||
func (m *syncMap[K, V]) LoadAndDelete(key K) (V, bool) { | ||
result, loaded := m.Map.LoadAndDelete(key) | ||
if !loaded { | ||
return *new(V), loaded // nolint: gocritic | ||
} | ||
|
||
item, ok := result.(V) | ||
return item, ok | ||
} | ||
|
||
// Range iterates the map. | ||
func (m *syncMap[K, V]) Range(visitor func(K, V) bool) { | ||
m.Map.Range(func(k any, v any) bool { | ||
return visitor(k.(K), v.(V)) | ||
}) | ||
} | ||
|
||
// Len returns the number of items in the map. | ||
func (m *syncMap[K, V]) Len() int { | ||
var count int | ||
m.Range(func(K, V) bool { | ||
count++ | ||
return true | ||
}) | ||
|
||
return count | ||
} |