| 75 | } |
| 76 | |
| 77 | func (c *imageResponseCache) put(key string, contentType string, etag string, xImgddSI string, body []byte) { |
| 78 | if c == nil || int64(len(body)) > c.maxFileBytes { |
| 79 | return |
| 80 | } |
| 81 | c.mu.Lock() |
| 82 | defer c.mu.Unlock() |
| 83 | |
| 84 | if existing, ok := c.items.Get(key); ok { |
| 85 | c.currentBytes -= int64(len(existing.body)) |
| 86 | } |
| 87 | entry := &imageResponseCacheEntry{ |
| 88 | key: key, |
| 89 | contentType: contentType, |
| 90 | etag: etag, |
| 91 | xImgddSI: xImgddSI, |
| 92 | body: append([]byte(nil), body...), |
| 93 | } |
| 94 | c.items.Add(key, entry) |
| 95 | c.currentBytes += int64(len(entry.body)) |
| 96 | |
| 97 | for c.currentBytes > c.maxBytes { |
| 98 | if _, _, ok := c.items.RemoveOldest(); !ok { |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func writeCachedImageResponse(w http.ResponseWriter, r *http.Request, entry *imageResponseCacheEntry) { |
| 105 | if r.Header.Get("If-None-Match") == entry.etag { |