components/resolver: Use database instead of file approach
This commit is contained in:
parent
f6b38f055d
commit
32107265d4
10 changed files with 165 additions and 103 deletions
|
|
@ -1,6 +1,9 @@
|
|||
package lazy
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Lazy is an object that a lazily-initialized value of type T.
|
||||
//
|
||||
|
|
@ -8,6 +11,9 @@ import "sync"
|
|||
type Lazy[T any] struct {
|
||||
once sync.Once
|
||||
value T
|
||||
|
||||
m sync.RWMutex // m protects resetting this lazy
|
||||
lastReset time.Time // last time this mutex was reset
|
||||
}
|
||||
|
||||
// Get returns the value associated with this Lazy.
|
||||
|
|
@ -20,8 +26,53 @@ type Lazy[T any] struct {
|
|||
//
|
||||
// Get may safely be called concurrently.
|
||||
func (lazy *Lazy[T]) Get(init func() T) T {
|
||||
lazy.m.RLock()
|
||||
defer lazy.m.RUnlock()
|
||||
|
||||
lazy.once.Do(func() {
|
||||
lazy.value = init()
|
||||
})
|
||||
return lazy.value
|
||||
}
|
||||
|
||||
// Reset resets this Lazy, deleting any previously associated value.
|
||||
//
|
||||
// May be called concurrently with [Get].
|
||||
// Future calls to [Get] will invoke init.
|
||||
func (lazy *Lazy[T]) Reset() {
|
||||
lazy.m.Lock()
|
||||
defer lazy.m.Unlock()
|
||||
|
||||
lazy.reset()
|
||||
}
|
||||
|
||||
// ResetAfter resets this lazy if more than d time has passed since the last reset.
|
||||
// If ResetAfter cannot lock, then it does not reset.
|
||||
//
|
||||
// May be called concurrently with other functions on this lazy.
|
||||
func (lazy *Lazy[T]) ResetAfter(d time.Duration) {
|
||||
if !lazy.m.TryLock() {
|
||||
return
|
||||
}
|
||||
defer lazy.m.Unlock()
|
||||
|
||||
if time.Since(lazy.lastReset) < d {
|
||||
return
|
||||
}
|
||||
|
||||
lazy.reset()
|
||||
}
|
||||
|
||||
// reset implements resetting this lazy.
|
||||
// m myst be held for writing.
|
||||
func (lazy *Lazy[T]) reset() {
|
||||
// reset the once
|
||||
lazy.once = sync.Once{}
|
||||
|
||||
// reset the value
|
||||
var t T
|
||||
lazy.value = t
|
||||
|
||||
// time of the last reset
|
||||
lazy.lastReset = time.Now()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,15 @@ func Filter[T any](values []T, filter func(T) bool) []T {
|
|||
return results
|
||||
}
|
||||
|
||||
// AsAny returns the same slice as values, but as any
|
||||
func AsAny[T any](values []T) []any {
|
||||
results := make([]any, len(values))
|
||||
for i, v := range values {
|
||||
results[i] = v
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
// Partition partitions values in T by the given functions.
|
||||
func Partition[T any, P comparable](values []T, partition func(value T) P) map[P][]T {
|
||||
result := make(map[P][]T)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue