优化代码

This commit is contained in:
GoEdgeLab
2023-10-11 14:07:13 +08:00
parent bf68c29fa5
commit 0e45e75167
4 changed files with 45 additions and 3 deletions

View File

@@ -24,11 +24,19 @@ func NewPiece[T any](maxItems int) *Piece[T] {
}
func (this *Piece[T]) Add(key uint64, item *Item[T]) (ok bool) {
this.locker.Lock()
this.locker.RLock()
if this.maxItems > 0 && len(this.m) >= this.maxItems {
this.locker.Unlock()
this.locker.RUnlock()
return
}
this.locker.RUnlock()
this.locker.Lock()
oldItem, exists := this.m[key]
if exists && oldItem.expiredAt == item.expiredAt {
this.locker.Unlock()
return true
}
this.m[key] = item
this.locker.Unlock()