This function is called internally by its own timer. Roughly, it will list keys and for each of the keys, call syncCb, which tells us if the item has been updated. If it has, then do a remove followed by an add. We can get away with this because it is guaranteed that this loop will run to completion
(ctx context.Context)
| 131 | // * Plugin asks for update on item 2 - cache evicts item 1, stores 2 and returns it unchanged |
| 132 | // * Sync loop updates item 2, repeat |
| 133 | func (w *autoRefreshCache) sync(ctx context.Context) { |
| 134 | keys := w.lruMap.Keys() |
| 135 | for _, k := range keys { |
| 136 | // If not ok, it means evicted between the item was evicted between getting the keys and this update loop |
| 137 | // which is fine, we can just ignore. |
| 138 | if value, ok := w.lruMap.Peek(k); ok { |
| 139 | newItem, result, err := w.syncCb(ctx, value.(CacheItem)) |
| 140 | if err != nil { |
| 141 | logger.Errorf(ctx, "failed to get latest copy of the item %v, error: %s", k, err) |
| 142 | } |
| 143 | |
| 144 | switch result { |
| 145 | case Update: |
| 146 | w.lruMap.Add(k, newItem) |
| 147 | case Delete: |
| 148 | w.lruMap.Remove(k) |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |