| 508 | } |
| 509 | |
| 510 | func initMemoryLayer(cacheSize int64, removeOnUpdate bool) *MemoryLayer { |
| 511 | ml := &MemoryLayer{} |
| 512 | ml.removeOnUpdate = removeOnUpdate |
| 513 | ml.statsHolder = NewStatsHolder() |
| 514 | if cacheSize > 0 { |
| 515 | cache, err := ristretto.NewCache(&ristretto.Config[[]byte, *CachePL]{ |
| 516 | // Use 5% of cache memory for storing counters. |
| 517 | NumCounters: int64(float64(cacheSize) * 0.05 * 2), |
| 518 | MaxCost: int64(float64(cacheSize) * 0.95), |
| 519 | BufferItems: 16, |
| 520 | Metrics: true, |
| 521 | Cost: func(val *CachePL) int64 { |
| 522 | itemSize := int64(8) |
| 523 | if val.list != nil { |
| 524 | itemSize += int64(val.list.ApproximateSize()) |
| 525 | } |
| 526 | return itemSize |
| 527 | }, |
| 528 | ShouldUpdate: func(cur, prev *CachePL) bool { |
| 529 | return !(cur.list != nil && prev.list != nil && prev.list.maxTs > cur.list.maxTs) |
| 530 | }, |
| 531 | }) |
| 532 | x.Check(err) |
| 533 | go func() { |
| 534 | m := cache.Metrics |
| 535 | ticker := time.Tick(10 * time.Second) |
| 536 | |
| 537 | for range ticker { |
| 538 | // Record the posting list cache hit ratio |
| 539 | ostats.Record(context.Background(), x.PLCacheHitRatio.M(m.Ratio())) |
| 540 | |
| 541 | if EnableDetailedMetrics { |
| 542 | x.NumPostingListCacheSave.M(ml.cache.numCacheRead.Load()) |
| 543 | x.NumPostingListCacheRead.M(ml.cache.numCacheRead.Load()) |
| 544 | x.NumPostingListCacheReadFail.M(ml.cache.numCacheReadFails.Load()) |
| 545 | } |
| 546 | |
| 547 | ml.cache.numCacheSave.Store(0) |
| 548 | ml.cache.numCacheRead.Store(0) |
| 549 | ml.cache.numCacheReadFails.Store(0) |
| 550 | } |
| 551 | }() |
| 552 | |
| 553 | ml.cache = &Cache{data: cache} |
| 554 | } |
| 555 | return ml |
| 556 | } |
| 557 | |
| 558 | func NewCachePL() *CachePL { |
| 559 | return &CachePL{ |