GC garbage expired items
()
| 146 | |
| 147 | // GC garbage expired items |
| 148 | func (this *Counter[T]) GC() { |
| 149 | this.gcLocker.Lock() |
| 150 | var gcIndex = this.gcIndex |
| 151 | |
| 152 | this.gcIndex++ |
| 153 | if this.gcIndex >= int(this.countMaps) { |
| 154 | this.gcIndex = 0 |
| 155 | } |
| 156 | |
| 157 | this.gcLocker.Unlock() |
| 158 | |
| 159 | var currentTime = fasttime.Now().Unix() |
| 160 | |
| 161 | this.locker.RLock(gcIndex) |
| 162 | var itemMap = this.itemMaps[gcIndex] |
| 163 | var expiredKeys = []uint64{} |
| 164 | for key, item := range itemMap { |
| 165 | if item.IsExpired(currentTime) { |
| 166 | expiredKeys = append(expiredKeys, key) |
| 167 | } |
| 168 | } |
| 169 | var tooManyItems = len(itemMap) > maxItemsPerGroup // prevent too many items |
| 170 | this.locker.RUnlock(gcIndex) |
| 171 | |
| 172 | if len(expiredKeys) > 0 { |
| 173 | this.locker.Lock(gcIndex) |
| 174 | for _, key := range expiredKeys { |
| 175 | delete(itemMap, key) |
| 176 | } |
| 177 | this.locker.Unlock(gcIndex) |
| 178 | } |
| 179 | |
| 180 | if tooManyItems { |
| 181 | this.locker.Lock(gcIndex) |
| 182 | var count = len(itemMap) - maxItemsPerGroup |
| 183 | if count > 0 { |
| 184 | itemMap = this.itemMaps[gcIndex] |
| 185 | for key := range itemMap { |
| 186 | delete(itemMap, key) |
| 187 | count-- |
| 188 | if count < 0 { |
| 189 | break |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | this.locker.Unlock(gcIndex) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | func (this *Counter[T]) CountMaps() int { |
| 198 | return int(this.countMaps) |