MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / AttemptCache

Method AttemptCache

backend/pkg/httpserver/cache.go:48–76  ·  view source on GitHub ↗

AttemptCache attempts to cache the given value, associated with the given key, within the underlying RawBytesDataCacher. It marshals both the key and value to JSON bytes before attempting to cache them. If any error occurs during the marshaling or caching process, it logs the error and does nothing

(ctx context.Context, key Key, value *Response)

Source from the content-addressed store, hash-verified

46// Note: This method does not return an error. This is intentional because
47// caching failures should not prevent the main operation from completing.
48func (c operationResponseCache[Key, Response]) AttemptCache(ctx context.Context, key Key, value *Response) {
49 if value == nil {
50 // Should never reach here
51 slog.ErrorContext(ctx, "unable to cache nil value")
52
53 return
54 }
55
56 jsonBytesKey, err := json.Marshal(key)
57 if err != nil {
58 slog.ErrorContext(ctx, "unable to marshal key for cache store",
59 "key", key, "error", err, "operation", c.operationID)
60
61 return
62 }
63 jsonBytesValue, err := json.Marshal(*value)
64 if err != nil {
65 slog.ErrorContext(ctx, "unable to marshal value for cache store",
66 "value", value, "error", err, "operation", c.operationID)
67
68 return
69 }
70
71 err = c.cacher.Cache(ctx, c.key(jsonBytesKey), jsonBytesValue, c.overrideCacheOptions...)
72 if err != nil {
73 slog.ErrorContext(ctx, "encountered unexpected error when caching",
74 "error", err, "key", key, "operation", c.operationID)
75 }
76}
77
78// Lookup attempts to retrieve a cached value by key. It serializes the key to JSON,
79// fetches the corresponding value from the RawBytesDataCacher, and then attempts to

Calls 2

keyMethod · 0.95
CacheMethod · 0.65

Tested by 1