| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "container/list" |
| 5 | "sync" |
| 6 | ) |
| 7 | |
| 8 | // The small-image cache (columns and icons spec §2.3). A sidebar, All |
| 9 | // pages and ⌘K can ask for dozens of icons at once, and every GET |
| 10 | // /i/{id} would otherwise read the bucket and unseal under the blob |
| 11 | // slots. So the opened bytes of any image within the icon's limits are |
| 12 | // kept here, keyed by public id. The handler cannot tell an icon from a |
| 13 | // small body picture, and need not. |
| 14 | // |
| 15 | // Scope comes first, always: GET /i/{id} runs the read rule against |
| 16 | // the database (ReadableImage) on every request before ImageBytes is |
| 17 | // asked, so a hit is only ever served to someone who may read the row. |
| 18 | // The cache replaces the bucket read and the unseal, never the rule. |
| 19 | // Eviction on removal is tidiness, not correctness. Memory is not rest: |
| 20 | // the seal's rule is unchanged, as the index holds bodies in memory. |
| 21 | |
| 22 | // smallImageBudget is the cache's bound, in bytes of pictures. |
| 23 | const smallImageBudget = 16 << 20 |
| 24 | |
| 25 | type smallImages struct { |
| 26 | mutex sync.Mutex |
| 27 | order *list.List // front is the most recently used |
| 28 | items map[string]*list.Element |
| 29 | bytes int |
| 30 | } |
| 31 | |
| 32 | type smallImage struct { |
| 33 | id, kind string |
| 34 | data []byte |
| 35 | } |
| 36 | |
| 37 | func (cache *smallImages) get(id string) ([]byte, string, bool) { |
| 38 | cache.mutex.Lock() |
| 39 | defer cache.mutex.Unlock() |
| 40 | element, ok := cache.items[id] |
| 41 | if !ok { |
| 42 | return nil, "", false |
| 43 | } |
| 44 | cache.order.MoveToFront(element) |
| 45 | item := element.Value.(smallImage) |
| 46 | return item.data, item.kind, true |
| 47 | } |
| 48 | |
| 49 | // put keeps data when it is within the icon's limits. |
| 50 | func (cache *smallImages) put(id, kind string, data []byte, |
| 51 | record imageRecord) { |
| 52 | if len(data) > MaxIconBytes || record.Width > MaxIconSide || |
| 53 | record.Height > MaxIconSide { |
| 54 | return |
| 55 | } |
| 56 | cache.mutex.Lock() |
| 57 | defer cache.mutex.Unlock() |
| 58 | if cache.items == nil { |
| 59 | cache.order, cache.items = list.New(), map[string]*list.Element{} |
| 60 | } |
| 61 | if _, ok := cache.items[id]; ok { |
| 62 | return |
| 63 | } |
| 64 | cache.items[id] = cache.order.PushFront(smallImage{id: id, kind: kind, |
| 65 | data: data}) |
| 66 | cache.bytes += len(data) |
| 67 | for cache.bytes > smallImageBudget { |
| 68 | cache.dropLocked(cache.order.Back()) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // drop forgets an image: on removal, and by the sweep. |
| 73 | func (cache *smallImages) drop(id string) { |
| 74 | cache.mutex.Lock() |
| 75 | defer cache.mutex.Unlock() |
| 76 | if element, ok := cache.items[id]; ok { |
| 77 | cache.dropLocked(element) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func (cache *smallImages) dropLocked(element *list.Element) { |
| 82 | item := cache.order.Remove(element).(smallImage) |
| 83 | delete(cache.items, item.id) |
| 84 | cache.bytes -= len(item.data) |
| 85 | } |
| 86 | |