-
Notifications
You must be signed in to change notification settings - Fork 11
/
passthrough.go
105 lines (96 loc) · 3.18 KB
/
passthrough.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sturdyc
import (
"context"
)
// Passthrough attempts to retrieve the latest data by calling the provided fetchFn.
// If fetchFn encounters an error, the cache is used as a fallback.
//
// Parameters:
//
// ctx - The context to be used for the request.
// key - The key to be fetched.
// fetchFn - Used to retrieve the data from the underlying data source.
//
// Returns:
//
// The value and an error if one occurred and the key was not found in the cache.
func (c *Client[T]) Passthrough(ctx context.Context, key string, fetchFn FetchFn[T]) (T, error) {
res, err := callAndCache(ctx, c, key, fetchFn)
if err == nil {
return res, nil
}
if value, ok := c.Get(key); ok {
return value, nil
}
return res, err
}
// Passthrough is a convenience function that performs type assertion on the
// result of client.PassthroughBatch.
//
// Parameters:
//
// ctx - The context to be used for the request.
// c - The cache client.
// key - The key to be fetched.
// fetchFn - Used to retrieve the data from the underlying data source.
//
// Returns:
//
// The value and an error if one occurred and the key was not found in the cache.
//
// Type Parameters:
//
// V - The type returned by the fetchFn. Must be assignable to T.
// T - The type stored in the cache.
func Passthrough[T, V any](ctx context.Context, c *Client[T], key string, fetchFn FetchFn[V]) (V, error) {
value, err := c.Passthrough(ctx, key, wrap[T](fetchFn))
return unwrap[V](value, err)
}
// PassthroughBatch attempts to retrieve the latest data by calling the provided fetchFn.
// If fetchFn encounters an error, the cache is used as a fallback.
//
// Parameters:
//
// ctx - The context to be used for the request.
// ids - The list of IDs to be fetched.
// keyFn - Used to prefix each ID in order to create a unique cache key.
// fetchFn - Used to retrieve the data from the underlying data source.
//
// Returns:
//
// A map of IDs to their corresponding values, and an error if one occurred and
// none of the IDs were found in the cache.
func (c *Client[T]) PassthroughBatch(ctx context.Context, ids []string, keyFn KeyFn, fetchFn BatchFetchFn[T]) (map[string]T, error) {
res, err := callAndCacheBatch(ctx, c, callBatchOpts[T, T]{ids, keyFn, fetchFn})
if err == nil {
return res, nil
}
values := c.GetManyKeyFn(ids, keyFn)
if len(values) > 0 {
return values, nil
}
return res, err
}
// PassthroughBatch is a convenience function that performs type assertion on the
// result of client.PassthroughBatch.
//
// Parameters:
//
// ctx - The context to be used for the request.
// c - The cache client.
// ids - The list of IDs to be fetched.
// keyFn - Used to prefix each ID in order to create a unique cache key.
// fetchFn - Used to retrieve the data from the underlying data source.
//
// Returns:
//
// A map of ids to their corresponding values and an error if one occurred.
//
// Type Parameters:
//
// V - The type returned by the fetchFn. Must be assignable to T.
// T - The type stored in the cache.
func PassthroughBatch[V, T any](ctx context.Context, c *Client[T], ids []string, keyFn KeyFn, fetchFn BatchFetchFn[V]) (map[string]V, error) {
res, err := c.PassthroughBatch(ctx, ids, keyFn, wrapBatch[T](fetchFn))
return unwrapBatch[V](res, err)
}