58 lines
3.7 KiB
HTML
58 lines
3.7 KiB
HTML
<p>有写字典数据不会频繁更新,但是会频繁查询,想要减少数据库链接次数,把内容缓存到项目的全局变量中,提高方法查询速度</p>
|
||
<p> </p>
|
||
<div class="cnblogs_code">
|
||
<pre>
|
||
<span style="color: #0000ff;">import</span><span style="color: #000000;"> javax.annotation.PostConstruct;
|
||
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> javax.annotation.PreDestroy;
|
||
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.HashMap;
|
||
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.List;
|
||
</span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.util.Map;
|
||
|
||
@Component
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> CodeCache {
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Map<String, TownNameDto> codeMap = <span style="color: #0000ff;">new</span> HashMap<String, TownNameDto><span style="color: #000000;">();
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Map<String, CompanyModel> companyMap = <span style="color: #0000ff;">new</span> HashMap<String, CompanyModel><span style="color: #000000;">();
|
||
|
||
@Autowired
|
||
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> CityDao cityDao;
|
||
@Autowired
|
||
</span><span style="color: #0000ff;">private</span><span style="color: #000000;"> CompanyDao companyDao;
|
||
|
||
|
||
@PostConstruct
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> init() {
|
||
</span><span style="color: #008000;">//</span><span style="color: #008000;">系统启动中。。。加载codeMap</span>
|
||
List<TownNameDto> codeList =<span style="color: #000000;"> cityDao.selectCityNameAndCodeALL();
|
||
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (TownNameDto code : codeList) {
|
||
codeMap.put(code.getTownCode() </span>+<span style="color: #000000;"> code.getValue(), code);
|
||
}
|
||
List</span><CompanyModel> companyModels =<span style="color: #000000;"> companyDao.selectCompanies();
|
||
</span><span style="color: #0000ff;">for</span><span style="color: #000000;"> (CompanyModel company : companyModels) {
|
||
companyMap.put(company.getCode(), company);
|
||
}
|
||
|
||
|
||
}
|
||
|
||
@PreDestroy
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> destroy() {
|
||
</span><span style="color: #008000;">//</span><span style="color: #008000;">系统运行结束</span>
|
||
<span style="color: #000000;"> }
|
||
|
||
@Scheduled(cron </span>= "0 0 0/2 * * ?"<span style="color: #000000;">)
|
||
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> testOne() {
|
||
</span><span style="color: #008000;">//</span><span style="color: #008000;">每2小时执行一次缓存</span>
|
||
<span style="color: #000000;"> init();
|
||
}
|
||
|
||
}</span></pre>
|
||
</div>
|
||
<p> </p>
|
||
<p>使用:</p>
|
||
<div class="cnblogs_code">
|
||
<pre><span style="color: #008000;">//</span><span style="color: #008000;">从缓存中获取城市;</span>
|
||
Map<String, TownNameDto> cityModelMap =<span style="color: #000000;"> CodeCache.codeMap;
|
||
Map</span><String, CompanyModel> companyModelMap = CodeCache.companyMap;</pre>
|
||
</div>
|
||
<p> </p>
|
||
<p>转自:https://blog.csdn.net/Alice_qixin/article/details/101547611</p> |