File: cached.go

package info (click to toggle)
singularity-container 4.1.5%2Bds4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 43,876 kB
  • sloc: asm: 14,840; sh: 3,190; ansic: 1,751; awk: 414; makefile: 413; python: 99
file content (63 lines) | stat: -rw-r--r-- 1,524 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package flightcontrol

import (
	"context"
	"sync"

	"github.com/pkg/errors"
)

// Group is a flightcontrol synchronization group that memoizes the results of a function
// and returns the cached result if the function is called with the same key.
// Don't use with long-running groups as the results are cached indefinitely.
type CachedGroup[T any] struct {
	// CacheError defines if error results should also be cached.
	// It is not safe to change this value after the first call to Do.
	// Context cancellation errors are never cached.
	CacheError bool
	g          Group[T]
	mu         sync.Mutex
	cache      map[string]result[T]
}

type result[T any] struct {
	v   T
	err error
}

// Do executes a context function syncronized by the key or returns the cached result for the key.
func (g *CachedGroup[T]) Do(ctx context.Context, key string, fn func(ctx context.Context) (T, error)) (T, error) {
	return g.g.Do(ctx, key, func(ctx context.Context) (T, error) {
		g.mu.Lock()
		if v, ok := g.cache[key]; ok {
			g.mu.Unlock()
			if v.err != nil {
				if g.CacheError {
					return v.v, v.err
				}
			} else {
				return v.v, nil
			}
		}
		g.mu.Unlock()
		v, err := fn(ctx)
		if err != nil {
			select {
			case <-ctx.Done():
				if errors.Is(err, context.Cause(ctx)) {
					return v, err
				}
			default:
			}
		}
		if err == nil || g.CacheError {
			g.mu.Lock()
			if g.cache == nil {
				g.cache = make(map[string]result[T])
			}
			g.cache[key] = result[T]{v: v, err: err}
			g.mu.Unlock()
		}
		return v, err
	})
}