cnblogs/dcrenl/Springboot 在项目启动时将数据缓存到全局变量.html
2024-09-24 12:43:01 +08:00

58 lines
3.7 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>有写字典数据不会频繁更新,但是会频繁查询,想要减少数据库链接次数,把内容缓存到项目的全局变量中,提高方法查询速度</p>
<p>&nbsp;</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&lt;String, TownNameDto&gt; codeMap = <span style="color: #0000ff;">new</span> HashMap&lt;String, TownNameDto&gt;<span style="color: #000000;">();
</span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> Map&lt;String, CompanyModel&gt; companyMap = <span style="color: #0000ff;">new</span> HashMap&lt;String, CompanyModel&gt;<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&lt;TownNameDto&gt; 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>&lt;CompanyModel&gt; 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>&nbsp;</p>
<p>使用:</p>
<div class="cnblogs_code">
<pre><span style="color: #008000;">//</span><span style="color: #008000;">从缓存中获取城市;</span>
Map&lt;String, TownNameDto&gt; cityModelMap =<span style="color: #000000;"> CodeCache.codeMap;
Map</span>&lt;String, CompanyModel&gt; companyModelMap = CodeCache.companyMap;</pre>
</div>
<p>&nbsp;</p>
<p>转自https://blog.csdn.net/Alice_qixin/article/details/101547611</p>