优化计数器

This commit is contained in:
GoEdgeLab
2023-11-15 15:17:03 +08:00
parent 81c863635a
commit 6c4ea694f9

View File

@@ -66,13 +66,10 @@ func (this *Counter) Increase(key uint64, lifeSeconds int) uint64 {
var index = int(key % this.countMaps)
this.locker.RLock(index)
var item = this.itemMaps[index][key]
var l = len(this.itemMaps[index])
this.locker.RUnlock(index)
if item == nil { // no need to care about duplication
if l > maxItemsPerGroup {
return 1
}
if item == nil {
// no need to care about duplication
// always insert new item even when itemMap is full
item = NewItem(lifeSeconds)
this.locker.Lock(index)
this.itemMaps[index][key] = item