GC 回收多余的缓存
()
| 127 | |
| 128 | // GC 回收多余的缓存 |
| 129 | func (this *UserAgentParser) GC() { |
| 130 | var total = this.Len() |
| 131 | if total > this.maxCacheItems { |
| 132 | for { |
| 133 | var shardingIndex = this.gcIndex |
| 134 | |
| 135 | this.mu.Lock(shardingIndex) |
| 136 | total -= len(this.cacheMaps[shardingIndex]) |
| 137 | this.cacheMaps[shardingIndex] = map[uint64]UserAgentParserResult{} |
| 138 | this.gcIndex = (this.gcIndex + 1) % userAgentShardingCount |
| 139 | this.mu.Unlock(shardingIndex) |
| 140 | |
| 141 | if total <= this.maxCacheItems { |
| 142 | break |
| 143 | } |
| 144 | } |
| 145 | } |
| 146 | } |