Documentation ¶
Index ¶
Examples ¶
Constants ¶
View Source
const OK = "ok"
Variables ¶
View Source
var ( ErrTimeout = errors.New("group: timeout waiting for result") ErrSubscriptionClosed = errors.New("group: subscription closed") )
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache[T any] struct { Client *redis.Client Group *Group LockTTL time.Duration WaitTTL time.Duration Suffix string }
Example ¶
// Cache example. // When the cache is stale, only once worker will populate // the cache, and the rest will wait. stop := redistest.Init() defer stop() client := redis.NewClient(&redis.Options{ Addr: redistest.Addr(), }) defer client.Close() cache := singleflight.NewCache[string](client) ctx := context.Background() hit := new(atomic.Int64) exec := new(atomic.Int64) n := 10 var wg sync.WaitGroup wg.Add(n) for range n { go func() { defer wg.Done() v, hitOrMiss, err := cache.LoadOrStore(ctx, "foo", func(context.Context) (string, error) { exec.Add(1) return "bar", nil }, 10*time.Second) if err != nil { panic(err) } if v != "bar" { panic("unexpected value: " + v) } if hitOrMiss { hit.Add(1) } }() } wg.Wait() fmt.Println("hit:", hit.Load()) fmt.Println("exec:", exec.Load())
Output: hit: 9 exec: 1
type ExponentialBackOff ¶
func NewExponentialBackOff ¶
func NewExponentialBackOff(base, cap time.Duration) *ExponentialBackOff
Click to show internal directories.
Click to hide internal directories.