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

Method Get

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

Source from the content-addressed store, hash-verified

87}
88
89func (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
124func (c *MemoryCache) GetOnly(ctx context.Context, key string) *Response {
125 log := logger.FromContext(ctx)

Callers

nothing calls this directly

Calls 5

loadMethod · 0.95
FromContextFunction · 0.92
CacheKeyMethod · 0.65
GetMethod · 0.65
DebugwMethod · 0.65

Tested by

no test coverage detected