热点数据任务
()
| 1243 | |
| 1244 | // 热点数据任务 |
| 1245 | func (this *FileStorage) hotLoop() { |
| 1246 | var memoryStorage = this.memoryStorage // copy |
| 1247 | if memoryStorage == nil { |
| 1248 | return |
| 1249 | } |
| 1250 | |
| 1251 | // check memory space size |
| 1252 | if !memoryStorage.HasFreeSpaceForHotItems() { |
| 1253 | return |
| 1254 | } |
| 1255 | |
| 1256 | this.hotMapLocker.Lock() |
| 1257 | if len(this.hotMap) == 0 { |
| 1258 | this.hotMapLocker.Unlock() |
| 1259 | this.lastHotSize = 0 |
| 1260 | return |
| 1261 | } |
| 1262 | |
| 1263 | this.lastHotSize = len(this.hotMap) |
| 1264 | |
| 1265 | var result = []*HotItem{} // [ {key: ..., hits: ...}, ... ] |
| 1266 | for _, v := range this.hotMap { |
| 1267 | if v.Hits <= 1 { |
| 1268 | continue |
| 1269 | } |
| 1270 | result = append(result, v) |
| 1271 | } |
| 1272 | |
| 1273 | this.hotMap = map[string]*HotItem{} |
| 1274 | this.hotMapLocker.Unlock() |
| 1275 | |
| 1276 | // 取Top10%写入内存 |
| 1277 | if len(result) > 0 { |
| 1278 | sort.Slice(result, func(i, j int) bool { |
| 1279 | return result[i].Hits > result[j].Hits |
| 1280 | }) |
| 1281 | var size = 1 |
| 1282 | if len(result) < 10 { |
| 1283 | size = 1 |
| 1284 | } else { |
| 1285 | size = len(result) / 10 |
| 1286 | } |
| 1287 | |
| 1288 | var buf = utils.BytePool16k.Get() |
| 1289 | |
| 1290 | defer utils.BytePool16k.Put(buf) |
| 1291 | for _, item := range result[:size] { |
| 1292 | reader, err := this.openReader(item.Key, false, false, false) |
| 1293 | if err != nil { |
| 1294 | continue |
| 1295 | } |
| 1296 | if reader == nil { |
| 1297 | continue |
| 1298 | } |
| 1299 | |
| 1300 | // 如果即将过期,则忽略 |
| 1301 | var nowUnixTime = time.Now().Unix() |
| 1302 | if reader.ExpiresAt() <= nowUnixTime+600 { |
no test coverage detected