MCPcopy Create free account
hub / github.com/Chatterino/api / load

Method load

pkg/cache/memory.go:48–87  ·  view source on GitHub ↗
(ctx context.Context, key string, r *http.Request)

Source from the content-addressed store, hash-verified

46}
47
48func (c *MemoryCache) load(ctx context.Context, key string, r *http.Request) {
49 log := logger.FromContext(ctx)
50
51 payload, statusCode, contentType, overrideDuration, err := c.loader.Load(ctx, key, r)
52
53 if statusCode == nil {
54 log.Warnw("Missing status code, setting to 200 default")
55 statusCode = &defaultStatusCode
56 }
57 if contentType == nil {
58 log.Warnw("Missing content type, setting to application/json default")
59 contentType = &defaultContentType
60 }
61
62 dur := c.cacheDuration
63 if overrideDuration != 0 {
64 dur = overrideDuration
65 }
66
67 response := Response{
68 Payload: payload,
69 StatusCode: *statusCode,
70 ContentType: *contentType,
71 }
72
73 // Cache it
74 if err == nil {
75 cacheKey := c.keyProvider.CacheKey(ctx, key)
76 kvCache.Set(cacheKey, response, dur)
77 } else {
78 fmt.Println("Error when some load function was called:", err)
79 }
80
81 c.requestsMutex.Lock()
82 for _, ch := range c.requests[key] {
83 ch <- response
84 }
85 delete(c.requests, key)
86 c.requestsMutex.Unlock()
87}
88
89func (c *MemoryCache) Get(ctx context.Context, key string, r *http.Request) (*Response, error) {
90 log := logger.FromContext(ctx)

Callers 1

GetMethod · 0.95

Calls 4

FromContextFunction · 0.92
LoadMethod · 0.65
WarnwMethod · 0.65
CacheKeyMethod · 0.65

Tested by

no test coverage detected