MCPcopy Create free account
hub / github.com/GoEdgeLab/EdgeNode / purgeLoop

Method purgeLoop

internal/caches/storage_memory.go:436–497  ·  view source on GitHub ↗

清理任务

()

Source from the content-addressed store, hash-verified

434
435// 清理任务
436func (this *MemoryStorage) purgeLoop() {
437 // 清理过期
438 var purgeCount = this.policy.MemoryAutoPurgeCount
439 if purgeCount <= 0 {
440 purgeCount = 2000
441 }
442 _, _ = this.list.Purge(purgeCount, func(hash string) error {
443 uintHash, err := strconv.ParseUint(hash, 10, 64)
444 if err == nil {
445 this.locker.Lock()
446 delete(this.valuesMap, uintHash)
447 this.locker.Unlock()
448 }
449 return nil
450 })
451
452 // LFU
453 // 计算是否应该开启LFU清理
454 var capacityBytes = this.policy.CapacityBytes()
455 var startLFU = false
456
457 var usedPercent = float32(this.TotalMemorySize()*100) / float32(capacityBytes)
458 var lfuFreePercent = this.policy.MemoryLFUFreePercent
459 if lfuFreePercent <= 0 {
460 lfuFreePercent = 5
461 }
462 if capacityBytes > 0 {
463 if lfuFreePercent < 100 {
464 if usedPercent >= 100-lfuFreePercent {
465 startLFU = true
466 }
467 }
468 }
469
470 if startLFU {
471 var total, _ = this.list.Count()
472 if total > 0 {
473 var count = types.Int(math.Ceil(float64(total) * float64(lfuFreePercent*2) / 100))
474 if count > 0 {
475 // 限制单次清理的条数,防止占用太多系统资源
476 if count > 2000 {
477 count = 2000
478 }
479
480 // 这里不提示LFU,因为此事件将会非常频繁
481
482 err := this.list.PurgeLFU(count, func(hash string) error {
483 uintHash, err := strconv.ParseUint(hash, 10, 64)
484 if err == nil {
485 this.locker.Lock()
486 delete(this.valuesMap, uintHash)
487 this.locker.Unlock()
488 }
489 return nil
490 })
491 if err != nil {
492 remotelogs.Warn("CACHE", "purge memory storage in LFU failed: "+err.Error())
493 }

Callers 1

initPurgeTickerMethod · 0.95

Calls 8

TotalMemorySizeMethod · 0.95
WarnFunction · 0.92
PurgeMethod · 0.65
CountMethod · 0.65
PurgeLFUMethod · 0.65
LockMethod · 0.45
UnlockMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected