| 1 |
package sturdyc |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
) |
| 6 |
|
| 7 |
// Passthrough attempts to retrieve the latest data by calling the provided fetchFn. |
| 8 |
// If fetchFn encounters an error, the cache is used as a fallback. |
| 9 |
// |
| 10 |
// Parameters: |
| 11 |
// |
| 12 |
// ctx - The context to be used for the request. |
| 13 |
// key - The key to be fetched. |
| 14 |
// fetchFn - Used to retrieve the data from the underlying data source. |
| 15 |
// |
| 16 |
// Returns: |
| 17 |
// |
| 18 |
// The value and an error if one occurred and the key was not found in the cache. |
| 19 |
func (c *Client[T]) Passthrough(ctx context.Context, key string, fetchFn FetchFn[T]) (T, error) { |
| 20 |
res, err := callAndCache(ctx, c, key, fetchFn) |
| 21 |
if err == nil { |
| 22 |
return res, nil |
| 23 |
} |
| 24 |
|
| 25 |
if value, ok := c.Get(key); ok { |
| 26 |
return value, nil |
| 27 |
} |
| 28 |
|
| 29 |
return res, err |
| 30 |
} |
| 31 |
|
| 32 |
// Passthrough is a convenience function that performs type assertion on the |
| 33 |
// result of client.PassthroughBatch. |
| 34 |
// |
| 35 |
// Parameters: |
| 36 |
// |
| 37 |
// ctx - The context to be used for the request. |
| 38 |
// c - The cache client. |
| 39 |
// key - The key to be fetched. |
| 40 |
// fetchFn - Used to retrieve the data from the underlying data source. |
| 41 |
// |
| 42 |
// Returns: |
| 43 |
// |
| 44 |
// The value and an error if one occurred and the key was not found in the cache. |
| 45 |
// |
| 46 |
// Type Parameters: |
| 47 |
// |
| 48 |
// V - The type returned by the fetchFn. Must be assignable to T. |
| 49 |
// T - The type stored in the cache. |
| 50 |
func Passthrough[T, V any](ctx context.Context, c *Client[T], key string, fetchFn FetchFn[V]) (V, error) { |
| 51 |
value, err := c.Passthrough(ctx, key, wrap[T](fetchFn)) |
| 52 |
return unwrap[V](value, err) |
| 53 |
} |
| 54 |
|
| 55 |
// PassthroughBatch attempts to retrieve the latest data by calling the provided fetchFn. |
| 56 |
// If fetchFn encounters an error, the cache is used as a fallback. |
| 57 |
// |
| 58 |
// Parameters: |
| 59 |
// |
| 60 |
// ctx - The context to be used for the request. |
| 61 |
// ids - The list of IDs to be fetched. |
| 62 |
// keyFn - Used to prefix each ID in order to create a unique cache key. |
| 63 |
// fetchFn - Used to retrieve the data from the underlying data source. |
| 64 |
// |
| 65 |
// Returns: |
| 66 |
// |
| 67 |
// A map of IDs to their corresponding values, and an error if one occurred and |
| 68 |
// none of the IDs were found in the cache. |
| 69 |
func (c *Client[T]) PassthroughBatch(ctx context.Context, ids []string, keyFn KeyFn, fetchFn BatchFetchFn[T]) (map[string]T, error) { |
| 70 |
res, err := callAndCacheBatch(ctx, c, callBatchOpts[T]{ids, keyFn, fetchFn}) |
| 71 |
if err == nil { |
| 72 |
return res, nil |
| 73 |
} |
| 74 |
|
| 75 |
values := c.GetManyKeyFn(ids, keyFn) |
| 76 |
if len(values) > 0 { |
| 77 |
return values, nil |
| 78 |
} |
| 79 |
|
| 80 |
return res, err |
| 81 |
} |
| 82 |
|
| 83 |
// PassthroughBatch is a convenience function that performs type assertion on the |
| 84 |
// result of client.PassthroughBatch. |
| 85 |
// |
| 86 |
// Parameters: |
| 87 |
// |
| 88 |
// ctx - The context to be used for the request. |
| 89 |
// c - The cache client. |
| 90 |
// ids - The list of IDs to be fetched. |
| 91 |
// keyFn - Used to prefix each ID in order to create a unique cache key. |
| 92 |
// fetchFn - Used to retrieve the data from the underlying data source. |
| 93 |
// |
| 94 |
// Returns: |
| 95 |
// |
| 96 |
// A map of ids to their corresponding values and an error if one occurred. |
| 97 |
// |
| 98 |
// Type Parameters: |
| 99 |
// |
| 100 |
// V - The type returned by the fetchFn. Must be assignable to T. |
| 101 |
// T - The type stored in the cache. |
| 102 |
func PassthroughBatch[V, T any](ctx context.Context, c *Client[T], ids []string, keyFn KeyFn, fetchFn BatchFetchFn[V]) (map[string]V, error) { |
| 103 |
res, err := c.PassthroughBatch(ctx, ids, keyFn, wrapBatch[T](fetchFn)) |
| 104 |
return unwrapBatch[V](res, err) |
| 105 |
} |