Documentation
¶
Overview ¶
Package concurrency provides state that parallel callbacks can safely share.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[K comparable, V any] struct { // contains filtered or unexported fields }
Cache synchronizes access to entries. Callers own the safety of native Go keys and values. Its zero value is ready to use. Do not copy a Cache after first use.
func NewCache ¶
func NewCache[K comparable, V any]() *Cache[K, V]
NewCache returns an empty cache ready for concurrent use.
func (*Cache[K, V]) GetOrLoad ¶
func (c *Cache[K, V]) GetOrLoad(ctx context.Context, key K, load func() (V, time.Time, error)) (Entry[V], error)
GetOrLoad runs load synchronously on a miss. Concurrent callers for the same key can cancel their wait independently. A failed or panicking load releases waiters to retry with their own loader. A loader must not wait on its own pending load.
func (*Cache[K, V]) InvalidateIfCurrent ¶
InvalidateIfCurrent deletes key only if entry is still its current publication. An older loader cannot remove a replacement.
Example ¶
package main
import (
"fmt"
"time"
"github.com/andreyvit/evm/evmruntime/concurrency"
)
func main() {
var cache concurrency.Cache[string, int]
first := cache.Set("price", 10, time.Time{})
cache.Set("price", 12, time.Time{})
cache.InvalidateIfCurrent("price", first)
entry, ok := cache.Get("price")
fmt.Println(entry.Value, ok)
}
Output: 12 true