Return the item if exists else create it. Create should be invoked only once. recreating the object is not supported.
(id ItemID, item Item)
| 231 | // Return the item if exists else create it. |
| 232 | // Create should be invoked only once. recreating the object is not supported. |
| 233 | func (w *InMemoryAutoRefresh) GetOrCreate(id ItemID, item Item) (Item, error) { |
| 234 | if val, ok := w.lruMap.Get(id); ok { |
| 235 | w.metrics.CacheHit.Inc() |
| 236 | return val.(Item), nil |
| 237 | } |
| 238 | |
| 239 | w.lruMap.Add(id, item) |
| 240 | w.metrics.CacheMiss.Inc() |
| 241 | |
| 242 | // It fixes cold start issue in the AutoRefreshCache by adding the item to the workqueue when it is created. |
| 243 | // This way, the item will be processed without waiting for the next sync cycle (30s by default). |
| 244 | if w.syncOnCreate { |
| 245 | batch := make([]ItemWrapper, 0, 1) |
| 246 | batch = append(batch, itemWrapper{id: id, item: item}) |
| 247 | w.workqueue.AddRateLimited(&batch) |
| 248 | w.processing.Store(id, w.clock.Now()) |
| 249 | } |
| 250 | return item, nil |
| 251 | } |
| 252 | |
| 253 | // DeleteDelayed queues an item for deletion. It Will get deleted as part of the next Sync cycle. Until the next sync |
| 254 | // cycle runs, Get and GetOrCreate will continue to return the Item in its previous state. |