(ctx context.Context, key string, r *http.Request)
| 46 | } |
| 47 | |
| 48 | func (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 | |
| 89 | func (c *MemoryCache) Get(ctx context.Context, key string, r *http.Request) (*Response, error) { |
| 90 | log := logger.FromContext(ctx) |
no test coverage detected