清理任务
()
| 434 | |
| 435 | // 清理任务 |
| 436 | func (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 | } |
no test coverage detected