mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-12-15 23:26:36 +08:00
优化代码
This commit is contained in:
@@ -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()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user