提交话题展示次数到数据库(定时提交)
()
| 100 | * 提交话题展示次数到数据库(定时提交) |
| 101 | */ |
| 102 | @Scheduled(fixedDelay=600000)//10分钟 |
| 103 | public void commitCount(){ |
| 104 | //计数 |
| 105 | int i = 0; |
| 106 | |
| 107 | Map<Long,Long> batchCount = new HashMap<Long,Long>(); |
| 108 | |
| 109 | for (Map.Entry<Long, Long> entry : countMap.asMap().entrySet()) { |
| 110 | Long topicId = entry.getKey(); |
| 111 | Long count = entry.getValue(); |
| 112 | |
| 113 | i++; |
| 114 | if(count > 0L){ |
| 115 | batchCount.put(topicId, count); |
| 116 | countMap.addAndGet(topicId, count * -1L);//减少 |
| 117 | } |
| 118 | if(i== 100){//每100条提交一次到数据库 |
| 119 | if(batchCount.size() >0){ |
| 120 | topicRepository.addViewCount(batchCount); |
| 121 | batchCount.clear(); |
| 122 | } |
| 123 | i = 0; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | //将剩余的展示次数提交到数据库 |
| 128 | if(batchCount.size() >0){ |
| 129 | topicRepository.addViewCount(batchCount); |
| 130 | } |
| 131 | |
| 132 | //删除所有计数器值为零的映射 |
| 133 | countMap.removeAllZeros(); |
| 134 | } |
| 135 | |
| 136 | |
| 137 |
nothing calls this directly
no test coverage detected