(ctx context.Context, key string, r *http.Request)
| 87 | } |
| 88 | |
| 89 | func (c *MemoryCache) Get(ctx context.Context, key string, r *http.Request) (*Response, error) { |
| 90 | log := logger.FromContext(ctx) |
| 91 | cacheKey := c.keyProvider.CacheKey(ctx, key) |
| 92 | |
| 93 | // If key is in cache, return value |
| 94 | if value, found := kvCache.Get(cacheKey); found && value != nil { |
| 95 | log.Debugw("Memory Get cache hit", "cacheKey", cacheKey) |
| 96 | if response, ok := value.(Response); ok { |
| 97 | return &response, nil |
| 98 | } |
| 99 | |
| 100 | return nil, errors.New("error getting stuff from kvcache") |
| 101 | } |
| 102 | |
| 103 | responseChannel := make(chan Response) |
| 104 | |
| 105 | c.requestsMutex.Lock() |
| 106 | |
| 107 | c.requests[key] = append(c.requests[key], responseChannel) |
| 108 | |
| 109 | first := len(c.requests[key]) == 1 |
| 110 | |
| 111 | c.requestsMutex.Unlock() |
| 112 | |
| 113 | if first { |
| 114 | log.Debugw("Memory Get cache miss", "cacheKey", cacheKey) |
| 115 | go c.load(ctx, key, r) |
| 116 | } |
| 117 | |
| 118 | // If key is not in cache, sign up as a listener and ensure loader is only called once |
| 119 | // Wait for loader to complete, then return value from loader |
| 120 | response := <-responseChannel |
| 121 | return &response, nil |
| 122 | } |
| 123 | |
| 124 | func (c *MemoryCache) GetOnly(ctx context.Context, key string) *Response { |
| 125 | log := logger.FromContext(ctx) |
nothing calls this directly
no test coverage detected