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

Method PurgeLFU

internal/caches/list_memory.go:277–335  ·  view source on GitHub ↗
(count int, callback func(hash string) error)

Source from the content-addressed store, hash-verified

275}
276
277func (this *MemoryList) PurgeLFU(count int, callback func(hash string) error) error {
278 if count <= 0 {
279 return nil
280 }
281
282 var deletedHashList = []string{}
283
284 var week = currentWeek()
285 var round = 0
286
287 this.locker.Lock()
288
289Loop:
290 for {
291 var found = false
292 round++
293 for _, itemMap := range this.itemMaps {
294 for hash, item := range itemMap {
295 found = true
296
297 if week-item.Week <= 1 /** 最近有在使用 **/ && round <= 3 /** 查找轮数过多还不满足数量要求的就不再限制 **/ {
298 continue
299 }
300
301 if this.onRemove != nil {
302 this.onRemove(item)
303 }
304
305 atomic.AddInt64(&this.count, -1)
306 delete(itemMap, hash)
307 deletedHashList = append(deletedHashList, hash)
308
309 count--
310 if count <= 0 {
311 break Loop
312 }
313
314 break
315 }
316 }
317 if !found {
318 break
319 }
320 }
321
322 this.locker.Unlock()
323
324 // 执行外部操作
325 for _, hash := range deletedHashList {
326 if callback != nil {
327 err := callback(hash)
328 if err != nil {
329 return err
330 }
331 }
332 }
333
334 return nil

Callers

nothing calls this directly

Calls 3

currentWeekFunction · 0.85
LockMethod · 0.45
UnlockMethod · 0.45

Tested by

no test coverage detected