(ctx context.Context, cvKey IDandCV, legacyKey IDAndRev, docRev DocumentRevision, collectionID uint32)
| 353 | } |
| 354 | |
| 355 | func (rc *LRURevisionCache) upsertDocToCache(ctx context.Context, cvKey IDandCV, legacyKey IDAndRev, docRev DocumentRevision, collectionID uint32) (int64, *revCacheValue) { |
| 356 | rc.lock.Lock() |
| 357 | defer rc.lock.Unlock() |
| 358 | |
| 359 | newItem := true |
| 360 | // lookup for element in hlv lookup map, if not found for some reason try rev lookup map |
| 361 | var existingElem *list.Element |
| 362 | var found bool |
| 363 | existingElem, found = rc.hlvCache[cvKey] |
| 364 | if !found { |
| 365 | existingElem, found = rc.cache[legacyKey] |
| 366 | } |
| 367 | if found { |
| 368 | revItem := existingElem.Value.(*revCacheValue) |
| 369 | // decrement item bytes by the removed item |
| 370 | rc._decrRevCacheMemoryUsage(ctx, -revItem.getItemBytes()) |
| 371 | rc.lruList.Remove(existingElem) |
| 372 | newItem = false |
| 373 | } |
| 374 | |
| 375 | // Add new value and overwrite existing cache key, pushing to front to maintain order |
| 376 | // also ensure we add to rev id lookup map too |
| 377 | value := &revCacheValue{id: docRev.DocID, cv: *docRev.CV, collectionID: collectionID} |
| 378 | elem := rc.lruList.PushFront(value) |
| 379 | rc.hlvCache[cvKey] = elem |
| 380 | rc.cache[legacyKey] = elem |
| 381 | |
| 382 | // only increment if we are inserting new item to cache |
| 383 | if newItem { |
| 384 | rc.cacheNumItems.Add(1) |
| 385 | } |
| 386 | |
| 387 | // Purge oldest item if over number capacity |
| 388 | numItemsRemoved, numBytesEvicted := rc._numberCapacityEviction() |
| 389 | if numBytesEvicted > 0 { |
| 390 | rc._decrRevCacheMemoryUsage(ctx, -numBytesEvicted) |
| 391 | } |
| 392 | return numItemsRemoved, value |
| 393 | } |
| 394 | |
| 395 | func (rc *LRURevisionCache) getValue(ctx context.Context, docID, revID string, collectionID uint32, create bool) (value *revCacheValue) { |
| 396 | if docID == "" || revID == "" { |
no test coverage detected