Lookup attempts to retrieve a cached value by key. It serializes the key to JSON, fetches the corresponding value from the RawBytesDataCacher, and then attempts to deserialize the result back to the Response type. It returns true if the value is found and successfully deserialized. Returns false ot
(ctx context.Context, key Key, value *Response)
| 81 | // found and successfully deserialized. Returns false otherwise. Any errors are logged. |
| 82 | // Errors are logged as this is a cache and shouldn't interrupt the normal flow of the program. |
| 83 | func (c operationResponseCache[Key, Response]) Lookup(ctx context.Context, key Key, value *Response) bool { |
| 84 | jsonBytesKey, err := json.Marshal(key) |
| 85 | if err != nil { |
| 86 | slog.ErrorContext(ctx, "unable to marshal key for cache lookup", |
| 87 | "error", err, "key", key, "operation", c.operationID) |
| 88 | |
| 89 | return false |
| 90 | } |
| 91 | |
| 92 | valueBytes, err := c.cacher.Get(ctx, c.key(jsonBytesKey)) |
| 93 | if err != nil { |
| 94 | if !errors.Is(err, cachetypes.ErrCachedDataNotFound) { |
| 95 | slog.ErrorContext(ctx, "encountered unexpected error from cache", |
| 96 | "error", err, "key", key, "operation", c.operationID) |
| 97 | } |
| 98 | |
| 99 | return false |
| 100 | } |
| 101 | |
| 102 | err = json.Unmarshal(valueBytes, value) |
| 103 | if err != nil { |
| 104 | slog.ErrorContext(ctx, "unable to unmarshal cached data", |
| 105 | "error", err, "key", key, "operation", c.operationID, "value", string(valueBytes)) |
| 106 | |
| 107 | return false |
| 108 | } |
| 109 | |
| 110 | return true |
| 111 | } |
| 112 | |
| 113 | // operationResponseCaches is a struct that holds multiple instances of |
| 114 | // operationResponseCache, each managing caching for a specific API operation. |